C++ Date Class Implementation


Using C++ write a Date class with the necessary constructors and overloaded operator functions that makes use of the requisite main() function to produce the indicated output below.

The Required main() function

int main()
{
// Constructors
Date today; // initializes Date with current date
Date feb2815(2,28,15);
Date feb2800(“02-28-00”);
Date NewYearsDay(“01/01/16”);

// Increment and decrement operators
cout << today << endl;
++today;
cout << today << endl;
–today;
cout << today << endl << endl;

cout << feb2815 << endl;
cout << ++feb2815 << endl;
cout << –feb2815 << endl << endl;

cout << feb2800 << endl;
cout << ++feb2800 << endl;
cout << –feb2800 << endl << endl;

cout << NewYearsDay << endl;
–NewYearsDay;
cout << NewYearsDay << endl;
++NewYearsDay;
cout << NewYearsDay << endl << endl;

// Test plus operators
cout << “30 days from now: ” << today + 30 << endl;
cout << “sixty days from now: ” << 60 + today << endl << endl;

// Test minus operators
Date twentyYearsAgo = today – static_cast<int>(round(20 * 365.25));
Date final(“12/12/16”);
cout << “Days until the final = ” << final – today << endl;

// Test the ! operator
cout << “Today is “;
!today;
cout << “Twenty years ago was “;
!twentyYearsAgo;
cout << endl;

// Test logic operators
cout << boolalpha;
cout << feb2800 << ” < ” << NewYearsDay << ” ? ” << (feb2800 < NewYearsDay) << endl;
cout << feb2815 << ” < ” << NewYearsDay << ” ? ” << (feb2815 < NewYearsDay) << endl;
cout << feb2815 << ” > ” << NewYearsDay << ” ? ” << (feb2815 > NewYearsDay) << endl;
}

Requirements

  1. The program must use the main() listed above.
  2. The Date class must contain:

– 3 data members for month, day, and year.

– 3 constructors. The default constuctor should initialize using the current date.

– a static array member to hold the number of days in each month.

– a static const array string member to hold month names.

– necessary overloaded operator functions

– two friend functions:

. an overloaded insertion operator to print a Date using a mm/dd/yyyy format.

. a overloaded binary + operator to added a number of days to a Date (e.g. 60 + today).

Program Output

Output produced 11/12/16

11/12/2016
11/13/2016
11/12/2016

02/28/2015
03/01/2015
02/28/2015

02/28/2000
02/29/2000
02/28/2000

01/01/2016
12/31/2015
01/01/2016

30 days from now: 12/12/2016
sixty days from now: 01/11/2017

Days until the final = 30
Today is November 12, 2016
Twenty years ago was November 12, 1996

02/28/2000 < 01/01/2016 ? true
02/28/2015 < 01/01/2016 ? true
02/28/2015 > 01/01/2016 ? false

Program Steps

– Store the year as a 4-digit integer number

– Use the time() and localtime() functions to get the current date.

– Write a function to test for leap year.

– Write and test one function at a time. Perform thorough testing, not just the the required main().

Leave a Reply

Your email address will not be published. Required fields are marked *



  • File Format: C++ .cpp
  • Version: c++11