I think I'd structure things a little differently. First, I'd make outputting a Date
a friend function of the Date
class and next, I'd overload the >
operator for comparing Date
class instances.
struct Date
{
int day;
int month;
int year;
friend std::ostream &operator<<(std::ostream& out, const Date &d) {
return out << d.day << "/" << d.month << "/" << d.year;
}
bool operator>(const Date &t) const;
};
The implementation of the >
operator would look like this:
bool Date::operator>(const Date &t) const
{
if (year > t.year) return true;
if (year < t.year) return false;
if (month > t.month) return true;
if (month < t.month) return false;
return day > t.day;
}
And then your main
routine would be this:
int main()
{
Date one;
Date two;
std::cout << "Enter a Date starting with the day, then month, then year:\n";
std::cin >> one.day >> one.month >> one.year;
std::cout << "\n\nEnter another date in the same fashion as above:\n";
std::cin >> two.day >> two.month >> two.year;
std::cout << "\n\nThank you: " << (one > two ? one : two)
<< " is the later date\n";
}
Note, too, that one can chain the input operator parts as shown here. In fact, I'd be inclined to declare an input operator operator>>
and promote the Date
struct to a full-fledged class.