Question: Write a C++ program: Q1) In a3.cpp, write a class called Date that inherits from the Date_base class in Date_base.h: // ... #include Date_base.h class

Write a C++ program:

Q1) In a3.cpp, write a class called Date that inherits from the Date_base class in Date_base.h:

// ... #include "Date_base.h" class Date : public Date_base { private: // ... public: // ... };

In the private part of Date, add variables for representing a day (1 to 31), month (1 to 12), and year (0 or higher). Then add all the getters and setters listed in Date_base (they're all public).

In the public part of Date, add a constructor that takes a day, month, and year as input, and uses an initializer list to initialize the private variables with the passed-in values.

Test what you've done by adding this function to a3.cpp:

void step_2_1_test() { Date xmas(25, 12, 2018); cout << xmas.get_day() << " " << xmas.get_month() << " " << xmas.get_year() << " "; assert(xmas.get_day() == 25); assert(xmas.get_month() == 12); assert(xmas.get_year() == 2018); xmas.set_year(2020); assert(xmas.get_day() == 25); assert(xmas.get_month() == 12); assert(xmas.get_year() == 2020); cout << "All step_2_1 tests passed! "; }

Call it in main() and compile and run your program to check the results:

// ... int main() { cout << "Welcome to assignment 3! "; step_2_1_test(); }

Q2) Uncomment the operator= method in Date_base.h. In it's body, add code to assign the day, month, and year from the other object to the current object. Finally, return the current object using *this.

Test what you've done by adding this function to a3.cpp:

void step_2_2_test() { Date a(25, 12, 2018); Date b = a; assert(a.get_day() == 25); assert(a.get_month() == 12); assert(a.get_year() == 2018); assert(a.get_day() == b.get_day()); assert(a.get_month() == b.get_month()); assert(a.get_year() == b.get_year()); b = Date(1, 1, 2019); a = b; assert(a.get_day() == 1); assert(a.get_month() == 1); assert(a.get_year() == 2019); assert(a.get_day() == b.get_day()); assert(a.get_month() == b.get_month()); assert(a.get_year() == b.get_year()); cout << "All step_2_2 tests passed! "; }

Call it in main() and compile and run your program to check the results:

// ... int main() { cout << "Welcome to assignment 3! "; step_2_1_test(); step_2_2_test(); }

Q3) Uncomment the to_string() method in Date_base.h. The string returned by to_string has this exact format: "dd/mm/yyyy". For example, if your date object represents May 19 2001, then to_string returns "19/05/2001". The returned string will always be exactly 10 characters long. The day and month will always be exactly two digits, and the year will always be exactly four digits.

Test what you've done by adding this function to a3.cpp:

void step_2_3_test() { Date a(25, 12, 2018); Date b(4, 19, 1999); Date c(1, 1, 0); assert(a.to_string() == "25/12/2018"); assert(b.to_string() == "04/19/1999"); assert(c.to_string() == "01/01/0000"); cout << "All step_2_3 tests passed! "; }

Call it in main() and compile and run your program to check the results:

int main() { cout << "Welcome to assignment 3! "; step_2_1_test(); step_2_2_test(); step_2_3_test(); }

Q4)

Add another constructor to your Date that takes a string as input. The string is a date that has exactly the same format as the output of to_string. For example, Date("25/12/2018") creates a date object with day 25, month 12, and year 2018.

Test what you've done by adding this function to a3.cpp:

void step_2_4_test() { Date a("25/12/2018"); Date b("04/19/1999"); Date c("01/01/0000"); assert(a.get_day() == 25); assert(a.get_month() == 12); assert(a.get_year() == 2018); assert(b.get_day() == 4); assert(b.get_month() == 19); assert(b.get_year() == 1999); assert(c.get_day() == 1); assert(c.get_month() == 1); assert(c.get_year() == 0); cout << "All step_2_4 tests passed! "; }

Call it in main() and compile and run your program to check the results:

int main() { cout << "Welcome to assignment 3! "; step_2_1_test(); step_2_2_test(); step_2_3_test(); step_2_4_test(); }

Q5)

Add a Comparison Operator

Note Don't start this step until you've finished the previous one.

Sorting dates from earliest to latest is a useful feature that we want to support. The standard C++ sort function can sort Date objects if we implement operator< for Date:

bool operator<(const Date& a, const Date& b) { // ... }

This function should return true if the date a is earlier than the date b, and false otherwise.

Test what you've done by adding this function to a3.cpp:

void step_2_5_test() { Date a("01/01/0000"); Date b("01/01/2018"); Date c("02/01/2018"); Date d("01/02/2018"); assert(a < b); assert(a < c); assert(a < d); assert(b < c); assert(b < d); assert(c < d); assert(!(a < a)); assert(!(b < a)); cout << "All step_2_5 tests passed! "; }

Call it in main() and compile and run your program to check the results: int main()

{ cout << "Welcome to assignment 3! ";

step_2_1_test();

step_2_2_test();

step_2_3_test();

step_2_4_test();

step_2_5_test();

}

The date_base.h has been provided below:

// Date_base.h

//////////////////////////////////////////////////////////////////////////// // // Date_base class // ////////////////////////////////////////////////////////////////////////////

#include

using namespace std;

// tell the compiler that Date is a class class Date;

class Date_base { public: // // virtual destructor // virtual ~Date_base() {}

// // getters // // return the year, month, and day virtual int get_year() const = 0; virtual int get_month() const = 0; virtual int get_day() const = 0;

// // setters // // set the year, month, and day virtual void set_year(int new_year) = 0; virtual void set_month(int new_month) = 0; virtual void set_day(int new_day) = 0;

// // assignment operator // // virtual Date &operator=(const Date &other) = 0;

// // printing // // returns a string representation of the date, as specified in the // assignment // virtual string to_string() const = 0; }; // class Date_base

// Date comparison operators bool operator<(const Date& d1, const Date& d2);

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!