Share
Programming Project 10.13.3: •••. Implement a base class Appointment and derived classes Onetime, Daily, Weekly, and Monthly. An appointment has a description and a date and time. Write a virtual function occurs_on that checks whether the appointment occurs on that date. For example, for a monthly appointment, you must check whether the day of the month matches. Then fill a vector of Appointment* with a mixture of appointments. Have the user enter a date and print out all appointments that happen on that date.
Report
Question
Please briefly explain why you feel this question should be reported.
Programming Project 10.13.3: •••.
Implement a base class Appointment
and derived classes Onetime
, Daily
, Weekly
, and Monthly
. An appointment has a description occurs_on(int year, int month, int day)
that checks whether the appointment occurs on that date. For example, for a monthly appointment, you must check whether the day of the month matches. Then fill a vector of Appointment*
with a mixture of appointments. Have the user enter a date and print out all appointments that happen on that date.
Answer ( 1 )
Please briefly explain why you feel this answer should be reported.
We are going to create the base class Appointment with a basic constructor, accessor/mutator member functions, print member function and the virtual member function occurs_on , which we are going to declare a pure virtual so that every class that is derived from this class has to define its own definition of the funciton .
Then, we’ll define each of the indicated derived classes and create a simple program out of these functions.
Before we get onto defining the base class, we’re going to construct two additional classes – Date and Time. We are going to use these classes to simplify constructing a class instance of an appointment type by allowing passing the constructor that were about to define.
These classes will be simple. They’ll have a basic constructor and accessor member functions for the data members that they hold.
Class Date definition:
The constructor definition:
The accessor member function definitions:
Class Time definition:
The constructor definition:
The accessor member function definitions:
Now that we have the Date and Time classes, we can construct an efficient and complete Appointment class.
The class has a basic constructor, accessor/mutator member functions, print member function and the pure virtual member function occurs_on .
Class definition:
The constructor initializes the data members:
The mutator member function definitions:
The accessor member function definitions:
The print member function utilizes the accessor member functions of Date and Time classes to get the needed data.
The function outputs the information stored for the appointment in format “<description>”, mm/dd/yyyy, hh:mm as follows:
Now, let’s start with the derived classes. They’re all going to inherit public properties of the base class Appointment and define the occurs_on member function.
Each occurs_on member function definition is going to utilize the Appointment and Date class accessor member functions to access the year, month and day.
Firstly, the Onetime class:
The Oneime::occurs_on member function should check if the date matches with the passed one as follows:
Then, the Daily class:
The Daily::occurs_on member function should only check if the passed date is equal to or occurs after the date of the appointment date.
The Weekly class:
The Weekly::occurs_on member function should first check if the year and month is equal to or before the passed date. Then, we’ll utilize a for loop to check the each day of the month for which the appointment occurs, and compare it to the passed day as follows:
Lastly, the Monthly class:
The Monthly::occurs_on member function should check if the day matches the date, and that the appointment month and year are equal to or before the passed date:
The main function utilizes a vector<Appointment*> variable to hold the list of the appointments.
We’ll create some appointments and push them onto the vector. Then, we’ll ask the user to enter a date. By utilizing the occurs_on member function to check if the appointment occurs on the inputted date, and print member function to output the appointment details, we’ll print out all appointments on the inputted date.
Make sure to delete the newly allocated memory at the end.