CS105: Introduction to Computer Programming: C++
Assignment #5 Object Overloading: Date class
Tuesday, March 2nd at NOON
For this project you will extend the Date class that you created in Assignment 4 with operator overloading functions. You are still required to have the same functionality and represent the same dates (from Jan 1, 1900 on and be accurate with leap years) as Assignment 4. You will also create a test program to test your functionality and a Makefile to compile all your C++ files.
- Extend your file
Date.h that defines the Date class. The Date class will retain the following functions:
The following functions will be replaced:
Default constructor which initializes the date to January 1, 1900.
Date(int m, int d, int y)
Constructor which takes in values for day, month, and year.
Destructor - if necessary.
int getDay() const, int getMonth() const, int getYear() const
Accessor functions for each of the data members.
bool setDay(int d), bool setMonth(int m), bool setYear(int y)
Mutator functions for each of the data members. These should return false for invalid inputs.
bool setDate(int m, int d, int y)
Sets the date object if provided with valid date values, otherwise it leaves the date object alone and returns false.
Your class additionally needs to define the following functions:
bool equals(Date const & date) const becomes
bool operator == (const Date &date) const
Return true if the current object and the parameter object represent the same date.
void add(int n) becomes
const Date& operator += (int n)
Adds n number of days to the current date. This function should modify the current object and return it.
void sub(int n) becomes
const Date& operator -= (int n)
Subtracts n number of days from the current date. If the subtraction would return a date before 1/1/1900, print an appropriate error message and set the date to 1/1/1900. This function should modify the current object and return it.
Date& operator =(const Date& date)
Sets the current Date object equal to the parameter Date object and returns the current Date object.
bool operator !=(const Date& date) const
Returns true if the current Date object and the parameter Date object do not represent the same date.
bool operator < (const Date& date) const
Returns true if the current Date object is before the parameter Date object.
bool operator <= (const Date& date) const
Returns true if the current Date object is before or equal to the parameter Date object.
bool operator > (const Date& date) const
Returns true if the current Date object is after the parameter Date object.
bool operator >= (const Date& date) const
Returns true if the current Date object is equal to or after the parameter Date object.
Date operator + (const int n) const
Returns a new Date object that represents the date that is n days after the current Date object. If a negative number is passed in causing a date before 1/1/1900, print an appropriate error message and set the date to 1/1/1900.
Date operator - (const int n) const
Returns a new Date object that represents the date that was n days before the current Date object. If a number is passed that results in a date before 1/1/1900, print an appropriate error message and set the date to 1/1/1900.
friend ostream& operator << (ostream& out, const Date& d)
Output the date in your choice of standard date representations (e.g. 09/06/2006, 06/09/2006, September 6, 2006, etc.). Remember that this function cannot be a member function; instead it is a friend.
friend istream& operator >> (istream& in, Date& d)
Input the date from standard in without prompts in such a way that cin >> date; works. Remember that this function cannot be a member function; instead it is a friend.
- You will need a file called
Date.cpp that implements the above functionality.
- You will write your own tests in a main function inside
testDate.cpp to test all functionality of your Date class. Of course I will provide my own testDate to test your Date class when I grade, but you should convince yourself that your code works before turning it in. Test each function at least once.
- You will need a Makefile that compiles Date.cpp and testDate.cpp and creates an executable "testDate" file to run your program. You can pattern your Makefile after the example one used in class that is available in the cs105/code directory. Make sure the lines after the rule: line starts with a tab! (Not spaces)
- Compile and run your code:
using the CS department linux machines. Make sure to test your
code on the CS department machines before turning the assignment in!
- When you're happy with your code use the
turnin program to submit your files:
Makefile. MAKE SURE YOUR NAME IS ON THE TOP OF EACH FILE! Use
jbsartor as the grader and
the assignment name (if you choose to take mulligans, use
You can turn in your files as many times as you
want - I will only take the last one submitted.
computer% turnin --submit jbsartor assign5 Date.h Date.cpp testDate.cpp Makefile