Create a C++ program using Visual Studio where you design a “DateTime” class tha
ID: 3785609 • Letter: C
Question
Create a C++ program using Visual Studio where you design a “DateTime” class that holds a month, a day, a year, an hour, and a minute. Overload extraction and insertion operators for the class. In this program, overload the == operator to compare two DateTimes. Consider one DateTime to be equal to another if both have the same day, month, and year. Also, create a main() function that allows you to enter ten DateTimes into an array. If a DateTime has already been entered, do not allow a second DateTime to the same date.
Explanation / Answer
#include <iostream>
using namespace std;
class DateTime {
int day;
int month;
int year;
int hour;
int minute;
public:
bool operator==(const DateTime dt)const {
return (this->day==dt.day and this->month==dt.month and this->year==dt.year);
}
friend ostream & operator << (ostream &out, const DateTime &dt);
friend istream & operator >> (istream &in, DateTime &dt);
};
ostream & operator << (ostream &out, const DateTime &dt)
{
cout << "Day : ";
out << dt.day << endl;
cout << "Month : ";
out << dt.month << endl;
cout << "Year : ";
out << dt.year << endl;
cout << "Hour : ";
out << dt.hour << endl;
cout << "Minute : ";
out << dt.minute << endl;
return out;
}
istream & operator >> (istream &in, DateTime &dt)
{
cout << "Enter Day ";
in >> dt.day;
cout << "Enter Month ";
in >> dt.month;
cout << "Enter Year ";
in >> dt.year;
cout << "Enter Hour ";
in >> dt.hour;
cout << "Enter Minute ";
in >> dt.minute;
return in;
}
int main() {
DateTime dt[10];
for (int i = 0; i < 10; ++i)
{
cin >> dt[i];
}
for (int i = 0; i < 10; ++i)
{
cout << "DateTime : " << i << endl;
cout << dt[i];
}
// Compare Two Date
bool x = (dt[0] == dt[1]);
cout << x;
}