appointment code

i have tried an appointment (one time, daily, weekly and monthly) code. can someone help me with the issues. Thank you so much.

//main
#include "Appointment.h"
#include "Onetime.h"
#include "Daily.h"
#include "Weekly.h"
#include "Monthly.h"

using namespace std;

int main() {

Appointment* app[5];


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;
}

//Appointment.h
#ifndef APPOINTMENT_H
#define APPOINTMENT_H
#include <iostream>
#include <string>
using namespace std;

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;
}

string Appointment :: getDescription() const{
return description;
}

//set get day
void Appointment :: setDay(int d){
day = d;
}
int Appointment :: getDay(){
return day;
}

//set get month
void Appointment :: setMonth(int m){
month = m;
}
int Appointment :: getMonth(){
return month;
}

//set get year
void Appointment :: setYear(int y){
year = y;
}
int Appointment :: getYear(){
return year;
}

//set get hour
void Appointment :: setHour(int h){
hour = h;
}
int Appointment :: getHour(){
return hour;
}

//set get min
void Appointment :: setMin(int m){
minute = m;
}
int Appointment :: getMin(){
return minute;
}

//set get sec
void Appointment :: setSec(int sec){
second = sec;
}
int Appointment :: getSec(){
return second;
}

bool Appointment :: occur_on(int d, int m, int y) const{

return false;
}

void Appointment:: printAppointment() const{

}
;


//Onetime.h
#ifndef ONETIME_H
#define ONETIME_H
#include <iostream>
#include "Appointment.h"
#include <string>
using namespace std;

class Onetime : public Appointment
{
public:
//constructor
Onetime();
Onetime(string dec, int d, int m, int y, int hr, int min, int sec);

bool occur_on(int d, int m, int y) const;
void printAppointment() const;

private:
int day;
int month;
int year;
};
#endif

//Onetime.cpp
#include "Onetime.h"
#include "Appointment.h"

Onetime :: Onetime() : Appointment(){}

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.h
#ifndef DAILY_H
#define DAILY_H
#include <iostream>
#include "Appointment.h"
#include <string>
using namespace std;

class Daily : public Appointment
{
public:
//constructor
Daily();
Daily(string dec, int d, int m, int y, int hr, int min,
int sec);

bool occur_on(int d, int m, int y) const;
void printAppointment() const;

private:
int day;
int month;
int year;
};
#endif

//Daily.cpp
#include "Daily.h"
#include "Appointment.h"


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.h
#ifndef WEEKLY_H
#define WEEKLY_H
#include <iostream>
#include "Appointment.h"
#include <string>
using namespace std;

class Weekly : public Appointment
{
public:
//constructor
Weekly();
Weekly(string dec, int d, int m, int y, int hr, int min, int sec);

bool occur_on(int d, int m, int y) const;
void printAppointment() const;

private:
int day;
int month;
int year;
};
#endif

//Weekly.cpp
#include "Weekly.h"
#include "Appointment.h"

Weekly :: Weekly() : Appointment()
{

}
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.h
#ifndef MONTHLY_H
#define MONTHLY_H
#include <iostream>
#include "Appointment.h"
#include <string>
using namespace std;

class Monthly : public Appointment
{
public:
//constructor
Monthly();
Monthly(string dec, int d, int m, int y, int hr, int min, int sec);

bool occur_on(int d, int m, int y) const;
void printAppointment() const;

private:
int day;
int month;
int year;
};
#endif

//Monthly.cpp
#include "Monthly.h"
#include "Appointment.h"

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
{

return (day == d);

}
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.
Last edited on
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.
Topic archived. No new replies allowed.