First of all, I would like to ask if anyone knows a decent site that explains operator overloading pretty well. It's a very confusing topic.. I can't seem to understand. And there seems to be plenty of arguments that overloading makes the program more complicated to read, which is the reason that Java doesn't have it.
Okay, for the actual program..
I was asked to make a program that compares two dates using operator overloading. I'm a mess right now, but I'll post the code anyway.
Code:
#include<iostream.h>
class Date
{
public:
Date();
Date(int m, int d, int y);
void get();
void show();
bool isAfter(Date date1, Date date2); //checks if the date1 is after date2.
bool operator == (const Date &date2); //I have no idea how to use this.. I'm suppose to see if the dates are equal to each other.
private:
int day,month,year;
};
Date::Date() //contains default date
{
day=1;
month=1;
year=1980;
}
Date::Date(int m, int d, int y)
{
day=d;
month=m;
year=y;
}
void Date::get()
{
cin>>day;
cin>>month;
cin>>year;
}
void Date::show()
{
if(isAfter(date1,date2)==true)
cout<<"After"<<endl;
else
cout<<"Before"<<endl;
}
bool Date::isAfter(Date date1, Date date2)
{
if (date2.year > date1.year)
return true;
else if (date2.year < date1.year)
return false;
else
{
if (date2.month > date1.month)
return true;
else if (date2.month < date1.month)
return false;
else
{
if (date2.day > date1.day)
return true;
else
return false;
}
}
}
bool Date::operator == (const Date &date2) //no idea.
{
}
int main()
{
Date date1, date2;
cout<<"Enter the first date"<<endl;
date1.get();
date1.show();
cout<<"Enter the second date"<<endl;
date2.get();
date2.show();
return 0;
}
Its very messy.. and sloppy. It doesn't even compile yet. Please, can anyone give me some helpful hints?