Advertisement

set function

Started by April 29, 2023 06:56 PM
1 comment, last by scott8 1 year, 6 months ago

Well I am trying to get the setappointment function to alter the values in int a_year, int amonth and int a_day in the Appointment class. Basically I trying to set the values in the Appointment class. This is an exercise in my book.

#include <iostream>
#include <string>
#include <vector>

using namespace std;

class Appointment
{
public:
	Appointment() {}
	Appointment(int p_year, int p_month, int p_day) 
	{
		a_year = p_year;
		a_month = p_month;
		a_day = p_day;
	}
	int timehh;
	int timemm;
	void set_appointment(int s_year, int s_month, int s_day);
	virtual void occurs_on(int year, int month, int day);
	int a_year;
	int a_month;
	int a_day;
private:

};

void Appointment::set_appointment(int s_year,int s_month,int s_day)
{
	a_year = s_year;
	a_month = s_month;
	a_day = s_day;
}

void Appointment::occurs_on(int year, int month, int day)
{
	if (year == a_year && month == a_month && day == a_day)
	{
		cout << "Booked Apppointment at: " << month << "/" << day << "/" << year << endl;
	}
}

class Onetime : public Appointment
{
public:
	Onetime();
	virtual void occurs_on(int year, int month, int day);
private:

};

Onetime::Onetime()
{
	
}

void Onetime::occurs_on(int year, int month, int day)
{
	if (year == a_year && month == a_month && day == a_day)
	{
		cout << "OneTime Booked Apppointment at: " << month << "/" << day << "/" << year << endl;
	}
}
class Daily : public Appointment
{
public:
	Daily();
	virtual void occurs_on(int day);
private:

};

Daily::Daily()
{
	
}

void Daily::occurs_on(int day)
{
	if (day == a_day)
	{
		cout << "Daily Booked Apppointment at: " << day << endl;
	}
}
class Weekly : public Appointment
{
public:
	Weekly();
	virtual void occurs_on(int month, int day);
private:

};

Weekly::Weekly()
{
	
}

void Weekly::occurs_on(int month, int day)
{
	if (month == a_month && day == a_day)
	{
		cout << "Weekly Booked Apppointment at: " << month << "/" << day << endl;
	}
}
class Monthly : public Appointment
{
public:
	Monthly();
	virtual void occurs_on(int month, int day);
private:

};

Monthly::Monthly()
{
	
}

void Monthly::occurs_on(int month, int day)
{
	if (month == a_month && day == a_day)
	{
		cout << "Monthly Booked Apppointment at: " << month << "/" << day << endl;
	}
}

int main()
{
	Appointment appot;
	appot.set_appointment(1985, 6, 15);
	char quit = NULL;
	int year, month, day;
	vector<Appointment*> appt;
	while (quit != 'y')
	{
		cout << "Enter year: ";
		cin >> year;
		cout << endl;
		cout << "Enter month: ";
		cin >> month;
		cout << endl;
		cout << "Enter day: ";
		cin >> day;
		cout << endl;
		cout << "Quit (y/n): ";
		cin >> quit;
		cout << endl;
		appt.push_back({ new Appointment(year,month,day) });
	}
	Onetime ot;
	Daily dy;
	Weekly wk;
	Monthly mo;
	for (int i = 0; i < appt.size(); i++)
	{
		ot.occurs_on(appt[i]->a_year,appt[i]->a_month,appt[i]->a_day);
		dy.occurs_on(appt[i]->a_day);
		wk.occurs_on(appt[i]->a_month,appt[i]->a_day);
		mo.occurs_on(appt[i]->a_month,appt[i]->a_day);
	}
	return 0;
}

You're just assigning the variables to themselves. Include Appointment:: to reference the class's variable.

Some people would use ‘m_year’ to name a member variable, making this type of mistake impossible.

Using the const keyword would alert you to a problem if you tried to assign s_year to s_year, even if the values are the same.

Also, I would think about passing a struct instead of all those variable separately.

void Appointment::setappointment(int const s_year,int const s_month,int const s_day)

{

Appointment::a_year = s_year;

Appointment::a_month =s_month;

Appointment::a_day = s_day;

s_year = s_year; // This will produce an error because of the const qualifier

}

This topic is closed to new replies.

Advertisement