app[0] = new Onetime("Go to dentist", 3,12,2019, 10, 10, 10);
app[1] = new Daily("Go to college", 3,12,2019,10,10,10);
app[2] = new Weekly ("Go to park",3,12,2019,10,10,10);
app[3] = new Monthly ("Go to church", 3,12,2019,10,10,10);
cout << "Enter your appointment date (month, day, year): ";
int day, month, year;
cin>> day >> month >> year;
for ( Appointment* a : app)
{
if (a -> occur_on(day, month, year))
{
a -> printAppointment();
}
}
return 0;
}
class Appointment
{
public:
Appointment();
Appointment(string dec, int d, int m, int y, int hr, int min, int sec);
Appointment(string dec, int hr, int min, int sec);
string getDescription() const;
int getDay();
int getMonth();
int getYear();
int getHour();
int getMin();
int getSec();
void setDay(int d);
void setYear(int y);
void setMonth(int m);
void setHour(int h);
void setMin(int min);
void setSec(int s);
virtual bool occur_on(int d, int m, int y) const;
virtual void printAppointment() const;
private:
string description;
int day;
int month;
int year;
int hour;
int minute;
int second;
};
#endif
//Appointment.cpp
#include "Appointment.h"
Appointment :: Appointment(){
description = "";
day = 0;
month = 0;
year = 0;
hour = 0;
minute = 0;
second = 0;
};
Appointment :: Appointment(string dec, int d, int m, int y, int hr, int min, int sec){
description = dec;
day = d;
month = m;
year = y;
hour = hr;
minute = min;
second = sec;
}
Appointment :: Appointment(string dec, int hr, int min, int sec)
{
description = dec;
hour = hr;
minute = min;
second = sec;
}
Onetime :: Onetime(string dec, int d, int m, int y, int hr, int min, int sec): Appointment(dec, hr, min, sec)
{
day = d;
month = m;
year = y;
}
void Onetime :: printAppointment() const
{
cout << "\n\nOne time\n"<< Appointment :: getDescription() << " on " << month <<"/" << day << "/" << year << endl;
}
bool Onetime :: occur_on(int d, int m, int y) const
{
if (year == y && month == m && day == d)
return true;
else
return false;
}
Daily :: Daily() : Appointment (){}
Daily :: Daily(string dec, int d, int m, int y, int hr, int min, int sec): Appointment(dec, hr, min, sec)
{
day = d;
month = m;
year = y;
}
void Daily :: printAppointment() const
{
cout << "\n\nDaily appointment\n" << Appointment :: getDescription() << " on " << month <<"/" << day << "/" << year <<endl;
}
bool Daily :: occur_on(int d, int m, int y) const
{
return true;
}
}
Weekly :: Weekly(string dec, int d, int m, int y, int hr, int min, int sec): Appointment(dec, hr, min, sec)
{
day = d;
month = m;
year = y;
}
void Weekly :: printAppointment() const
{
cout << "\n\nWeekly appointment\n"<< Appointment :: getDescription() << " on " << month <<"/" << day << "/" << year << endl;
}
bool Weekly :: occur_on(int d, int m, int y) const
{
return( ((m* 30) + d + (y * 360) % 7 ) == ((month * 30) + day + (year * 360)%7));
}
Monthly :: Monthly() : Appointment(){}
Monthly :: Monthly(string dec, int d, int m, int y, int hr, int min, int sec): Appointment(dec, hr, min, sec)
{
day = d;
month = m;
year = y;
}
void Monthly :: printAppointment() const
{
cout << "\n\nMonthly appointment\n"<< Appointment :: getDescription() << " on " << month <<"/" << day << "/" << year << endl;
}
bool Monthly :: occur_on(int d, int m, int y) const
{
Welcome to the forum! Yes, someone can help you. First, we need help from you, though. Please do 2 things... edit your post, highlight all of it, and click the <> button in the side editor, and resubmit it. Second, tell us what exactly the problems ARE. What doe it do that it should not, or what does it not do that it should?
One problem that I see is that Onetime, Daily, Weekly, Monthly all have private instances of day, month and year. Those are already defined in your base class. You want to make day, month, year protected in your base class so that your derived classes have access to them and eliminate them from your derived classes.
Second problem: You declare 5 pointers, but only assign four instances of an appointment. Your for loop accesses all 5 instances and throws an exception when trying to access the fifth instance app[4].
Third problem: Your prompt for appointment date says month, day, year; but your cin statement is day, month, year.
Problem four: Your arguments to your constructors are d, m y order, but when you instantiate your appointments, you're passing them in m, d, y order.
PLEASE ALWAYS USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post. http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.
I think you need to step away from the code for a few minutes and think about the data.
1 2 3
app[1] = new Daily("Go to college", 3,12,2019,10,10,10);
app[2] = new Weekly ("Go to park",3,12,2019,10,10,10);
app[3] = new Monthly ("Go to church", 3,12,2019,10,10,10);
Why does the constructor for a daily appointment include a specific day?
Why does the constructor for a weekly appointment include a specific day? Shouldn't it include a day of the week when it occurs?
Why does the constructor for a monthly appointment include a specific day? Shouldn't it just include the day of the month?
Think about what these types of appointments really mean. Think about the data that you need to encode each type individually. Then, if there is common data to all of them, put that data in the base class. All other data belongs in each class individually.
Figure out what data goes in what class. Then the needs of the constructors and virtual methods will be much clearer.