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