In: Computer Science
// Date.h
#ifndef DATE_H
#define DATE_H
class Date
{
public:
// Function declarations
Date();
Date(int month, int day, int year);
int getMonth();
void setMonth(int month);
int getDay();
void setDay(int day);
int getYear();
void setYear(int year);
void printDate();
bool sameMonth(Date d);
private:
// Declaring variables
int month;
int day;
int year;
};
#endif
/****************************************************/
/****************************************************/
// Date.cpp
#include <assert.h>
#include <iostream>
using namespace std;
#include "Date.h"
Date::Date()
{
this->day = 1;
this->month = 1;
this->year = 1;
}
Date::Date(int month, int day, int year)
{
setMonth(month);
setDay(day);
this->year = year;
}
int Date::getMonth()
{
return month;
}
void Date::setMonth(int month)
{
assert(month >= 1 && month <= 12);
this->month = month;
}
int Date::getDay()
{
return day;
}
void Date::setDay(int day)
{
assert(day >= 1 && day <= 31);
this->day = day;
}
int Date::getYear()
{
return year;
}
void Date::setYear(int year)
{
this->year = year;
}
void Date::printDate()
{
cout << "he date is (M-D-Y): " << month << "-"
<< day << "-" << year << endl;
}
bool Date::sameMonth(Date d)
{
if (this->month == d.getMonth())
return true;
else
return false;
}
/****************************************************/
/****************************************************/
// main.cpp
#include <iostream>
using namespace std;
#include "Date.h"
int main()
{
int month,day,year;
Date date1;
cout<<"The initialized date is
(M-D-Y):"<<date1.getMonth()<<"-"<<date1.getDay()<<"-"<<date1.getYear()<<endl;
cout<<"Please enter a date : (Month Day Year):";
cin>>month>>day>>year;
date1.setDay(day);
date1.setMonth(month);
date1.setYear(year);
cout<<"Please enter a second date : (Month Day Year):";
cin>>month>>day>>year;
Date date2(month,day,year);
cout<<"\nPrinting the two days:"<<endl;
date1.printDate();
date2.printDate();
bool b=date1.sameMonth(date2);
if(b)
{
cout<<"The months are the same"<<endl;
}
else{
cout<<"The months are not same"<<endl;
}
return 0;
}
/***********************************************/
/***********************************************/
Output:
/***********************************************/