Question: Using C++, specify, design, and implement a class called date. Use integers to represent a dates month, day, and year. Write a member function to
Using C++, specify, design, and implement a class called date. Use integers to represent a dates month, day, and year. Write a member function to increment the date to the next day. Include friend functions to display a date in both number and word format. Create date.cpp with the header file date.h and the test program test_date.cpp.
date.h
#ifndef _DATE_H_ #define _DATE_H_ #includeclass date { // class invariant: values of year, month, day are in the correct ranges public: date (int d, int m, int y); // precondition: 1 <= m <= 12, 1 <= d <= days in m // postcondition: date with given attributes has been created void increment(); // postcondition: date has been changed to the next date int get_year () const; // returned: year of date int get_month() const; // returned: month of date int get_day() const; // returned: day of date private: int year; int month; int day; }; std::ostream& operator << (std::ostream& out, const date& dt); #endif
test_date.cpp
#include#include #include #include "date.h" using namespace std; int main () { date d1(24, 3, 1987); assert (d1.get_day() == 24); assert (d1.get_month() == 3); assert (d1.get_year() == 1987); d1.increment(); assert (d1.get_day() == 25); assert (d1.get_month() == 3); assert (d1.get_year() == 1987); date d2(28, 2, 1987); assert (d2.get_day() == 28); assert (d2.get_month() == 2); assert (d2.get_year() == 1987); d2.increment(); assert (d2.get_day() == 1); assert (d2.get_month() == 3); assert (d2.get_year() == 1987); date d3(28, 2, 1988); assert (d3.get_day() == 28); assert (d3.get_month() == 2); assert (d3.get_year() == 1988); d3.increment(); assert (d3.get_day() == 29); assert (d3.get_month() == 2); assert (d3.get_year() == 1988); date d4(31, 12, 1988); assert (d4.get_day() == 31); assert (d4.get_month() == 12); assert (d4.get_year() == 1988); d4.increment(); assert (d4.get_day() == 1); assert (d4.get_month() == 1); assert (d4.get_year() == 1989); cout << "all tests passed - do endzone celebration dance!" << endl; return EXIT_SUCCESS; }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
