Hi all,
I want the program below to calculate the doctor fee. I am doing it to learn class and inheritance concept. "patient1.output(cout);" works correctly it gets doctor_hour_fee from doct1 object. The problem is at patient2.output(cout);. When I enter patient information it asks me to enter all member variables of doctor object and it creates a new doctor, so that doct1 and new doctor are different and normally I get wrong answer. Can you tell me how can I get doctor_hour_fee of doct1 for patient2? During input of patient2 I just want to enter name of doctor, which is doctorfirst in this case, and calculate doctor fee.
Thanks for helps,

The code listing:

#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
class Person
{
public:
	Person();
	Person(string name_of_person);
	Person(const Person &p);
	~Person();
	string get_name()const;	
	void set_name(string name_ofp);	
	void input(istream &inp);
	void output(ostream &outp);
protected:
	string name;	
};
class Doctor: public Person
{
public:
	Doctor();
	Doctor(string name_of_doctor, double hour_fee_of_doc);
	Doctor(const Doctor &d);
	~Doctor();
	double get_fee_of_doc() const;	
	void set_fee_of_doc(double hour_fee);	
	void input(istream &ind);
	friend istream& operator>>(istream &ind_try, Doctor &d);
	void output(ostream &outd);
protected:
	double doctor_hour_fee;	
};
class Patient: public Person
{
public:
	Patient();
	Patient(string name_of_patient, double visit_duration_ofp, Doctor &doc);
	Patient(const Patient &p);
	~Patient();
	double get_visit_duration() const;	
	void set_visit_duration(double visit_duration_ofp);
	void setDoctor(Doctor &doc);
	virtual double billing() const;
	void input(istream &inpa);
	void output(ostream &outpa);	
protected:
	double visit_duration;
	Doctor doctor;
};
int main()
{
	Person person1("first last");
	person1.output(cout);
	Person person2;
	person2.input(cin);
	person2.output(cout);
	Doctor doct1("doctorfirst",250);
	doct1.output(cout);
	Patient patient1("firstpatient",0.1,doct1);
	patient1.output(cout);
	Patient patient2;
	patient2.input(cin);
	patient2.output(cout);
}
Person::Person()
{
	name  = "No name yet";	
}
Person::Person(string name_of_person)
{
	name = name_of_person;	
}
Person::Person(const Person &p)
{
	name = p.name;	
}
Person::~Person()
{}
string Person::get_name() const
{
	return name;
}
void Person::set_name(string name_ofp)
{
	name = name_ofp;
}
void Person::input(istream &inp)
{
	cout << "Please enter information of person (name)" << endl;
	inp >> name;
}
void Person::output(ostream &outp)
{
	outp << "Name:  " << get_name() << endl;
}
Doctor::Doctor():Person(),doctor_hour_fee(0.0)
{}
Doctor::Doctor(string name_of_doctor, double hour_fee_of_doc):Person(name_of_doctor),doctor_hour_fee(hour_fee_of_doc)
{}
Doctor::Doctor(const Doctor &d):Person(d),doctor_hour_fee(d.doctor_hour_fee)
{}
Doctor::~Doctor()
{}
double Doctor::get_fee_of_doc()const
{
	return doctor_hour_fee;
}
void Doctor::set_fee_of_doc(double hour_fee)
{
	doctor_hour_fee = hour_fee;
}
istream& operator>>(istream &ind_try,Doctor &d)
	{
		ind_try>>d.name>>d.doctor_hour_fee;
		return ind_try;
	}
void Doctor::input(istream &ind)
{
	cout << "Please enter information of doctor  " << endl << " (name--1 hour fee) " << endl;
	ind >> name >> doctor_hour_fee;
}
void Doctor::output(ostream &outd)
{
	outd << "Name of Doctor: " << name << "  1 Hour fee of doctor: " << doctor_hour_fee; 
}
Patient::Patient():Person(),visit_duration(0.0),doctor(Doctor())
{}
Patient::Patient(string name_of_patient, double visit_duration_ofp, Doctor &doc):Person(name_of_patient),visit_duration(visit_duration_ofp),doctor(doc)
{}
Patient::Patient(const Patient &p):Person(p),visit_duration(p.visit_duration),doctor(doctor)
{}
Patient::~Patient()
{}
double Patient::get_visit_duration() const
{
	return visit_duration;
}
void Patient::set_visit_duration(double visit_duration_ofp)
{
	visit_duration = visit_duration_ofp;
}
void Patient::setDoctor(Doctor &doc)
{
	doctor = doc;
}
double Patient::billing() const
{	
	/*Doctor *docto_try;
	docto_try = new Doctor;*/	
	return (doctor.get_fee_of_doc()*get_visit_duration());
}
void Patient::input(istream &inpa)
{
	cout << endl;
	cout << "Please enter information of patient(name--visit duration--Doctor)" << endl;
	inpa >> (name) >> (visit_duration) >> doctor;
}
void Patient::output(ostream &outpa)
{
	outpa << "Name of Patient:  " << get_name() << "  Visit Duration of Patient:  " <<  get_visit_duration() << endl <<
		"Billing of the patient:  " << billing() << endl;
}

Recommended Answers

All 5 Replies

"patient1.output(cout);" works correctly it gets doctor_hour_fee from doct1 object. The problem is at patient2.output(cout);. When I enter patient information it asks me to enter all member variables of doctor object and it creates a new doctor, so that doct1 and new doctor are different and normally I get wrong answer. Can you tell me how can I get doctor_hour_fee of doct1 for patient2? During input of patient2 I just want to enter name of doctor, which is doctorfirst in this case, and calculate doctor fee.

You are inputting text:

inpa >> (name) >> (visit_duration) >> doctor;

But doctor is not text:

Doctor doctor;

I believe you'll need to find the particular doctor based on the text that the user enters.

Note how this might have parallels to what you find successful:

Doctor doct1("doctorfirst",250);
	doct1.output(cout);
	Patient patient1("firstpatient",0.1,doct1);

Here patient1 is not constructed with a 3rd parameter that is text.

Dear Dave,
Thank you very much for your comments. I tried it again but not get it yet. Can you please explain it more explicitly?
Thanks,

SO you have 2 patients with the same doctor.

Though it is not impossible , it is quite tedious to do so especially during runtime, Not only that it requires some over head to do so during runtime .

Your basic approach would be to generate and store a list of all doctors that have been defined, Then once you get the doctors name, search in that list if the doctor is defined and if he is already existing , reference that doctor to the patient.

Else if the doctor is not already defined, you will need to add in a new doctor to the doctors list.

Please keep in mind that there might be other approaches by which it would be easier and this approach is restricted to my experience (Which is very small ).

Hi Sky Diplloma,

It is great idea. However, I am a beginner and I have no idea about how to do that? In this case do I have to have a Dcotor class? If I have to I do not know how to implement it. How can I generate such a list and use it to solve my problem?
Thanks,

Well, to implement this, you will basically need 2 things, A static vector storing pointers to doctors and then you will need to change the constructor of the Doctor class to add itself into the vector,

The only backdrop of this method will be the pointers to temporary objects.

Though this is a basic sketch, you should wait for more replys for some of the other members of the forum whose experience and advice might give out a better solution .

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.