In: Computer Science
c++ programing.
Objectives
Define a class called Person with private members name of type string, and money of integer type. The class has the public member functions set(), getMoney(), print(), and a parameterized constructor with default values.
Define a class called Patient with private data members name of type string, durationOfTreatment, and TreatmentCost of type integer. The class has the public member functions set(), getCost(), getDuration(), print(), and a parameterized constructor with default values.
Define a class called Doctor that publicly inherits the class Person and has the private members numberOfPatients,totalCost, list[] (an array of type Patient), and calculateTotalCost(). The class has the public member functions set(), print(), and a parameterized constructor with default values.
Implement all member functions, enforcing the principle of least privileged.
The following driver produces the given sample input / output.
int main()
{
Doctor Hakeem;
Hakeem.set("Ali Omar", 3000, 3);
Hakeem.print();
return 0;
}
Sample input / output:
Enter the name, duration, and cost of treatment number 1: Allergies 23 150
Enter the name, duration, and cost of treatment number 2: Bronchiectasis 15 280
Enter the name, duration, and cost of treatment number 3: Gallstones 10 50
Ali Omar has 3000 dirham and treated the following patients:
Allergies was treated for 23 minutes, at a cost 150 dirham per minute
Bronchiectasis was treated for 15 minutes, at a cost 280 dirham per minute
Gallstones was treated for 10 minutes, at a cost 50 dirham per minute
The total cost of the 3 patients is 8150 dirham
The overall total of money is 11150 dirham
Please find the answer below.
Please do comments in case of any issue. Also, don't forget to rate
the question. Thank You So Much.
main.cpp
#include <iostream>
using namespace std;
class Person{
private:
string name;
int money;
public:
Person(){
name = "";
money = 0;
}
int getMoney(){
return money;
}
void print(){
cout<<name<<" has
"<<money<<" dirham and treated the following
patients\n";
}
void set(string n,int m){
name = n;
money = m;
}
};
class Patient {
private:
string name;
int durationOfTreatment;
float treatmentCost;
public:
Patient(){
name = "";
durationOfTreatment = 0;
treatmentCost = 0;
}
void set(){
cin>>name>>durationOfTreatment>>treatmentCost;
}
void setTreatmentCost(float c){
treatmentCost = c;
}
float getCost(){
return treatmentCost;
}
int getDuration(){
return durationOfTreatment;
}
void print(){
cout<<"Allergies was treated
for "<<durationOfTreatment<<" minutes, at a cost
"<<treatmentCost<<" dirham per
minute"<<endl;
}
};
class Doctor : public Person {
private:
int numberOfPatients;
float totalCost;
Patient *list;
public:
Doctor():Person(){
numberOfPatients = 0;
}
void calculateTotalCost(){
totalCost = 0;
for(int
i=0;i<numberOfPatients;i++){
totalCost +=
list[i].getCost()*list[i].getDuration();
}
}
void set(string n,int money,int num){
numberOfPatients = num;
list = new Patient[num];
Person::set(n,money);
for(int
i=0;i<numberOfPatients;i++){
cout<<"Enter the name, duration, and cost of treatment number
"<<i+1<<" : ";
list[i].set();
}
}
void print(){
Person::print();
for(int
i=0;i<numberOfPatients;i++){
list[i].print();
}
calculateTotalCost();
cout<<"The total cost of the
"<<numberOfPatients<<" patients is
"<<totalCost<<" dirham\n";
cout<<"The overall total of
money is "<<totalCost+getMoney()<<"
dirham"<<endl;
}
};
int main(){
Doctor Hakeem;
Hakeem.set("Ali Omar", 3000, 3);
Hakeem.print();
return 0;
}