Programming Project 10.13.3: •••. Implement a base class Appointment and derived classes Onetime, Daily, Weekly, and Monthly. An appointment has a description forexample,seethedentist and a date and time. Write a virtual function occurs_onintyear,intmonth,intday 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.

Report
Cancel
Programming Project 10.13.3: •••.

Implement a base class Appointment and derived classes Onetime, Daily, Weekly, and Monthly. An appointment has a description forexample,seethedentist and a date and 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 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.

MathJax Example

Answer ( 1 )

  1. Please briefly explain why you feel this answer should be reported.

    Report
    Cancel

    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 whichwearegoingtocover.

    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:

    class Date
    {
    public:
       Date(int year = 0, int month = 0, int day = 0);
    
       int get_year() const;
       int get_month() const;
       int get_day() const;
    
    private:
       int year;
       int month;
       int day;
    };
    

    The constructor definition:

    Date::Date(int year, int month, int day)
    {
       this->year = year;
       this->month = month;
       this->day = day;
    }
    

    The accessor member function definitions:

    int Date::get_year() const
    {
       return year;
    }
    
    int Date::get_month() const
    {
       return month;
    }
    
    int Date::get_day() const
    {
       return day;
    }
    

    Class Time definition:

    class Time
    {
    public:
       Time(int hours = 0, int minutes = 0);
    
       int get_hours() const;
       int get_minutes() const;
    
    private:
       int hours;
       int minutes;
    };
    

    The constructor definition:

    Time::Time(int hours, int minutes)
    {
       this->hours = hours;
       this->minutes = minutes;
    }
    

    The accessor member function definitions:

    int Time::get_hours() const
    {
       return hours;
    }
    
    int Time::get_minutes() const
    {
       return minutes;
    }
    

    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 the=0attheendofthedeclaration member function occurs_on.

    Class definition:

    class Appointment
    {
    public:
       Appointment() {}
       Appointment(string description, Date date, Time time);
    
       virtual bool occurs_on(int year, 
          int month, int day) const = 0;
          
       void set_description(string description);
       void set_date(Date date);
       void set_time(Time time);
    
       string get_description() const;
       Date get_date() const;
       Time get_time() const;
    
       void print() const;
       
    private:
       string description;
       Date date;
       Time time;
    };
    

    The constructor initializes the data members:

    Appointment::Appointment(string description, 
       Date date, Time time)
    {
       this->description = description;
       this->date = date;
       this->time = time;
    }
    

    The mutator member function definitions:

    void Appointment::set_description(string description)
    {
       this->description = description;
    }
    
    void Appointment::set_date(Date date)
    {
       this->date = date;
    }
    
    void Appointment::set_time(Time time)
    {
       this->time = time;
    }
    

    The accessor member function definitions:

    string Appointment::get_description() const
    {
       return description;
    }
    
    Date Appointment::get_date() const
    {
       return date;
    }
    
    Time Appointment::get_time() const
    {
       return time;
    }
    

    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:

    void Appointment::print() const
    {
       cout << """ << description << "", "
            << setfill('0') << setw(2)
            << date.get_month() << "/"
            << setfill('0') << setw(2)
            << date.get_day() << "/"
            << date.get_year() << ", "
            << setfill('0') << setw(2)
            << time.get_hours() << ":"
            << setfill('0') << setw(2)
            << time.get_minutes() << endl;
    }
    

    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:

    class Onetime : public Appointment
    {
    public:
       Onetime() {}
       Onetime(string description, Date date, Time time)
          : Appointment(description, date, time) {}
    
       virtual bool occurs_on(int year,
          int month, int day) const;
    };
    

    The Oneime::occurs_on member function should check if the date matches with the passed one as follows:

    bool Onetime::occurs_on(int year, 
       int month, int day) const
    {
       if (get_date().get_year() == year &&
          get_date().get_month() == month &&
          get_date().get_day() == day)
          return true;
       return false;
    }
    

    Then, the Daily class:

    class Daily : public Appointment
    {
    public:
       Daily() {}
       Daily(string description, Date date, Time time)
          : Appointment(description, date, time) {}
    
       virtual bool occurs_on(int year,
          int month, int day) const;
    };
    

    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.

    bool Daily::occurs_on(int year, int month, int day) const
    {
       if (get_date().get_year() <= year &&
          get_date().get_month() <= month &&
          get_date().get_day() <= day)
          return true;
       return false;
    }
    

    The Weekly class:

    class Weekly : public Appointment
    {
    public:
       Weekly() {}
       Weekly(string description, Date date, Time time)
          : Appointment(description, date, time) {}
    
       virtual bool occurs_on(int year,
          int month, int day) const;
    };
    

    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:

    bool Weekly::occurs_on(int year, int month, int day) const
    {
       if (get_date().get_year() <= year &&
          get_date().get_month() <= month)
       {
          // weeks after the date (same month)
          for (int i = get_date().get_day(); i <= 30; i += 7)
             if (i == day)
                return true;
    
          // weeks before the date (same month)
          if (get_date().get_month() != month)
             for (int i = get_date().get_day(); i > 0; i -= 7)
                if (i == day)
                   return true;
       }
    
       return false;
    }
    

    Lastly, the Monthly class:

    class Monthly : public Appointment
    {
    public:
       Monthly() {}
       Monthly(string description, Date date, Time time)
          : Appointment(description, date, time) {}
    
       virtual bool occurs_on(int year,
          int month, int day) const;
    };
    

    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:

    bool Monthly::occurs_on(int year, int month, int day) const
    {
       if (get_date().get_year() <= year &&
          get_date().get_month() <= month &&
          get_date().get_day() == day)
          return true;
       return false;
    }
    

    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.

    int main()
    {
       vector<Appointment*> list;
    
       list.push_back(new Onetime("see the dentist", 
          Date(2021, 10, 14), Time(10, 45)));
       list.push_back(new Daily("cook",
          Date(2021, 10, 1), Time(11, 0)));
       list.push_back(new Weekly("clean house",
          Date(2021, 10, 7), Time(9, 15)));
    
       int year, month, day;
       cout << "Enter <year> <month> <day>: ";
       cin >> year >> month >> day;
    
       cout << "The appointments: " << endl;
    
       for (int i = 0; i < list.size(); i++)
          if (list[i]->occurs_on(year, month, day))
             list[i]->print();
    
       for (int i = 0; i < list.size(); i++)
          delete list[i];
    
       return 0;
    }
    

     

    Enter <year> <month> <day>: 2021 10 14
    The appointments:
    "see the dentist", 10/14/2021, 10:45
    "cook", 10/1/2021, 11:00
    "clean house", 10/7/2021, 09:15

Leave an answer

Browse

By answering, you agree to the Terms of Service and Privacy Policy.