CS105: Introduction to Computer Programming: C++

Assignment #4 Objects: Date class


Tuesday, February 23th at NOON


For this project you will implement a Date class similar to the date class found in the C++ Standard Library. Your date must be able to represent any valid date from January 1, 1900 into the future accepting only valid input (if invalid, set to default Jan 1, 1900). For example, February 29th is a valid date for any leap year. Leap years are years that are divisible by 4, with one exception. If the year is divisible by 100 and not by 400 (like 2100), then it is not a leap year. (e.g. 2004 was, 2008 was, 1900 was not, 2100 will not be, 2400 will be, and so on).


  1. Write a file Date.h that defines the Date class. It must have 3 private integers to hold the month, day, and year. It also needs 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(Date const & d)
      Copy constructor that makes a copy of the parameter date class.
    4. ~Date()
      Default destructor. Does it need to do anything?
    5. int getDay() const, int getMonth() const, int getYear() const
      Accessor functions for each of the data members.
    6. 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.
    7. 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.
    8. bool equals(Date const & date) const
      Return true if the current object and the parameter object represent the same date.
    9. void displayDate() const
      Output function which displays the date to standard out. Output it in your choice of standard date representations (e.g. 09/06/2006, 06/09/2006, September 6, 2006, etc.).
    10. void add(int n)
      Adds n number of days to the current date.
    11. void sub(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.
  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. You can use a Makefile to compile all 3 files, but this time you do not have to turn it in.
  4. Compile and run your code:
      computer% g++ -Wall -Werror -c Date.cpp
      computer% g++ -Wall -Werror -c testDate.cpp
      computer% g++ -Wall -Werror -o testDate Date.o testDate.o
      computer% ./testDate
  5. I recommend using the CS department linux machines. Make sure to test your code on the CS department machines before turning the assignment in!
  6. When you're happy with your code use the turnin program to submit your 3 files: Date.h, Date.cpp, and testDate.cpp. MAKE SURE YOUR NAME IS ON THE TOP OF EACH FILE! Use jbsartor as the grader and assign4 as the assignment name (if you choose to take mulligans, use assign4Mulligan).
      computer% turnin --submit jbsartor assign4 Date.h Date.cpp testDate.cpp
    You can turn in your files as many times as you want - I will only take the last one submitted.

Submission Checklist

Extra Credit (+7%)

Implement the following extra methods in your Date class.