Question: Implement a base class Appointment and derived classes Onetime, Daily, and Monthly. An appointment has a description (for example see an advisor) and a date
Implement a base class Appointment and derived classes Onetime, Daily, and Monthly. An appointment has a description (for example "see an advisor") and a date and a time. Write a virtual function 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 a day of the month matches. Then fill a vector of Appointment* with a mixtures of appointments. Have user enter a date and printout all appointments that happens on that date. #include
using namespace std;
const int DAYS_PER_MONTH = 30; /** A class that describes a time of day (between 00:00:00 and 23:59:59) */ class Time { public: /** Constructs a time of day. @param hour the hours @param min the minutes @param sec the seconds */ Time(int hour, int min, int sec); /** Constructs a Time object that is set to the time at which the constructor executes. */ Time();
/** Gets the hours of this time. @return the hours */ int get_hours() const; /** Gets the minutes of this time. @return the minutes */ int get_minutes() const; /** Gets the seconds of this time. @return the seconds */ int get_seconds() const;
/** Computes the seconds between this time and another. @param t the other time @return the number of seconds between this time and t */ int seconds_from(Time t) const; /** Adds a number of seconds to this time. @param s the number of seconds to add */ void add_seconds(int s);
private: int time_in_secs; };
/** Computes the correct remainder for negative dividend @param a - an integer @param n - an integer > 0 @return the mathematically correct remainder r such that a - r is divisible by n and 0 <= r and r < n */ int remainder(int a, int n) { if (a >= 0) { return a % n; } else { return n - 1 - (-a - 1) % n; } }
Time::Time(int hour, int min, int sec) { time_in_secs = 60L * 60 * hour + 60 * min + sec; }
Time::Time() { time_in_secs = 0; }
int Time::get_hours() const { return time_in_secs / (60 * 60); }
int Time::get_minutes() const { return (time_in_secs / 60) % 60; }
int Time::get_seconds() const { return time_in_secs % 60; }
int Time::seconds_from(Time t) const { return time_in_secs - t.time_in_secs; }
void Time::add_seconds(int s) { const int SECONDS_PER_DAY = 60 * 60 * 24; time_in_secs = remainder(time_in_secs + s, SECONDS_PER_DAY); }
//..................................................................
class Date { public: Date(); Date(int y, int m, int d); void print() const; bool equals(Date other) const; private: int day; int month; int year; };
Date::Date() { day = 1; month = 1; year = 1; }
Date::Date(int y, int m, int d) { day = d; month = m; year = y; }
void Date::print() const { cout << year << "/" << month << "/" << day; }
bool Date::equals(Date other) const { return day == other.day && month == other.month && year == other.year; }
//..................................................................
int main() { vector
cout << "Enter year month day: "; int year; int month; int day; cin >> year >> month >> day;
cout << "You have these appointments: " << " "; for (int i = 0; i < schedule.size(); i++) { if (schedule[i]->occurs_on(year, month, day)) { schedule[i]->print(); } } return 0; }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
