CS105: Introduction to Computer Programming: C++

Assignment #5 Object Overloading: Date class

Due

Tuesday, March 2nd at NOON

Overview

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.

Instructions

  1. Extend your file Date.h that defines the Date class. The Date class will retain the following functions:
    1. Date()
      Default constructor which initializes the date to January 1, 1900.
    2. Date(int m, int d, int y)
      Constructor which takes in values for day, month, and year.
    3. ~Date()
      Destructor - if necessary.
    4. int getDay() const, int getMonth() const, int getYear() const
      Accessor functions for each of the data members.
    5. 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.
    6. 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.
    The following functions will be replaced:
    1. 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.
    2. 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.
    3. 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.
    Your class additionally needs to define the following functions:
    1. Date& operator =(const Date& date)
      Sets the current Date object equal to the parameter Date object and returns the current Date object.
    2. bool operator !=(const Date& date) const
      Returns true if the current Date object and the parameter Date object do not represent the same date.
    3. bool operator < (const Date& date) const
      Returns true if the current Date object is before the parameter Date object.
    4. bool operator <= (const Date& date) const
      Returns true if the current Date object is before or equal to the parameter Date object.
    5. bool operator > (const Date& date) const
      Returns true if the current Date object is after the parameter Date object.
    6. bool operator >= (const Date& date) const
      Returns true if the current Date object is equal to or after the parameter Date object.
    7. 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.
    8. 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.
    9. 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.
    10. 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.
  2. You will need a file called Date.cpp that implements the above functionality.
  3. 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.
  4. 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)
  5. Compile and run your code:
      computer% make
      computer% ./testDate
    
  6. I recommend using the CS department linux machines. Make sure to test your code on the CS department machines before turning the assignment in!
  7. When you're happy with your code use the turnin program to submit your files: Date.h, Date.cpp, testDate.cpp, and Makefile. MAKE SURE YOUR NAME IS ON THE TOP OF EACH FILE! Use jbsartor as the grader and assign5 as the assignment name (if you choose to take mulligans, use assign5Mulligan).
      computer% turnin --submit jbsartor assign5 Date.h Date.cpp testDate.cpp Makefile
    
    You can turn in your files as many times as you want - I will only take the last one submitted.

Submission Checklist

Extra Credit (+8%)

Implement the following extra methods in your Date class.