Introduction to Computer Programming: C++

Assignment #2, Date class

Due

Thursday, 18th October at noon

Overview

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 1 January, 1900 into the future accepting only valid input (if invalid, set to default 1 Jan, 1900). For example, 29 February 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, 2012 is, 1900 was not, 2100 will not be, 2400 will be, and so on).

Instructions

  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 d, int m, 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 d, int m, 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. 06/09/2006, 6 September, 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.
  4. Compile and run your code in Visual Studio. Use Build Solution and Start With Debugging or Start Without Debugging.
  5. When you're happy with your code, submit your 3 files: Date_[yourLastName].h, Date_[yourLastName].cpp, and testDate_[yourLastName].cpp to the DropBox in Minerva. Make sure your name is on the top of each file. You can turn in your files as many times as you want to Minerva- I will only take the last one submitted.

Submission Checklist

Extra Credit (+7%)

Implement the following extra methods in your Date class.