Date Class: Using a class

The overloaded operator + lets us add a number of days to a date. That may mean that the new date is in the next month or even in the next year.

1
2
3
4
5
6
7
8
9
10
#include "c:/cpp/classes/Date.cpp"
#include <iostream>
using namespace std;
int main() {
   Date today;
   Date dueDate=today+14;
   cout<<"Books checked out today are due "<<dueDate<<endl;
   system("pause");
   return 0;
}
CODE

You may have noticed that I made a few arbitrary decisions while writing the class. I want any instance of the date to always be valid. I decided what the default date should be, I decided what I want the overloaded operator + to do and what the toString method should return.

These are examples of decisions that a programmer must make when designing a class. Consider carefully how other programmers might use your class.

Because this was written for educationally purposes, I only overloaded the < and == operators for comparing dates. Feel free to add the overloaded > operator. Make sure that you add it to both Date.h and Date.cpp.

NEXT: The day of the week