Question

In: Computer Science

c++ programing. Objectives Further practice with classes Use of inheritance Use compositions Define a class called...

c++ programing.

Objectives

  • Further practice with classes
  • Use of inheritance
  • Use compositions

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

Solutions

Expert Solution

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;

}


Related Solutions

Objectives: Use class inheritance to create new classes. Separate class definition and implementation in different files....
Objectives: Use class inheritance to create new classes. Separate class definition and implementation in different files. Use include guard in class header files to avoid multiple inclusion of a header. Tasks: In our lecture, we wrote a program that defines and implements a class Rectangle. The source code can be found on Blackboard > Course Content > Classes and Objects > Demo Program 2: class Rectangle in three files. In this lab, we will use class inheritance to write a...
Developing a set of classes demonstrating inheritance - in class work
Developing a set of classes demonstrating inheritance - in class work
Written in C++ Define a class for a type called Fraction. This class is used to...
Written in C++ Define a class for a type called Fraction. This class is used to represent a ratio of two integers. Include mutator functions that allow the user to set the numerator and denominator. Also include a member function that returns the values of the numerator divided by the denominator as a double. Include an additional member function that outputs the value of the fraction reduced to lowest terms. This will require finding the greatest common divisor for the...
C++ HW Aim of the assignment is to write classes. Create a class called Student. This...
C++ HW Aim of the assignment is to write classes. Create a class called Student. This class should contain information of a single student. last name, first name, credits, gpa, date of birth, matriculation date, ** you need accessor and mutator functions. You need a constructor that initializes a student by accepting all parameters. You need a default constructor that initializes everything to default values. write the entire program.
Define a class called Goals that has the following requirements in c++: a function called set...
Define a class called Goals that has the following requirements in c++: a function called set that takes 3 int parameters that are goals for "fame", "happiness" and "money". The function returns true and saves the values if they add up to exactly 60, and returns false otherwise (you may assume the parameters are not negative) a functions satisfies that takes 3 int parameters and returns true if each parameter is at least as large as the saved goal, false...
Objectives:  Write classes in C++  Use dynamic arrays  Write and read from files...
Objectives:  Write classes in C++  Use dynamic arrays  Write and read from files 1. WriteaclassGradeBookcontainingthefollowing: Private attributes: - courseName: a string representing the name of the course. - nbOfStudents: an integer representing the number of students enrolled in the course. The number of students is greater than or equal to 5. - grades: a double dimensional array of integers representing the grades of Test1, Test2 and Final of every student. It should be a dynamic array. Public...
In C++ Define a base class called Person. The class should have two data members to...
In C++ Define a base class called Person. The class should have two data members to hold the first name and last name of a person, both of type string. The Person class will have a default constructor to initialize both data members to empty strings, a constructor to accept two string parameters and use them to initialize the first and last name, and a copy constructor. Also include appropriate accessor and mutator member functions. Overload the operators == and...
Use inheritance to implement the following classes: A: A Car that is a Vehicle and has...
Use inheritance to implement the following classes: A: A Car that is a Vehicle and has a name, a max_speed value and an instance variable called the number_of_cylinders in its engine. Add public methods to set and get the values of these variables. When a car is printed (using the toString method), its name, max_speed and number_of_cylinders are shown. B: An Airplane that is also a vehicle and has a name, a max_speed value and an instance variable called the...
C++ Define a base class called Person. The class should have two data members to hold...
C++ Define a base class called Person. The class should have two data members to hold the first name and last name of a person, both of type string. The Person class will have a default constructor to initialize both data members to empty strings, a constructor to accept two string parameters and use them to initialize the first and last name, and a copy constructor. Also include appropriate accessor and mutator member functions. Overload the operators == and !=...
The purpose of this lab is to practice using inheritance and polymorphism. We need a set of classes for an Insurance company.
The purpose of this lab is to practice using inheritance and polymorphism.    We need a set of classes for an Insurance company.   Insurance companies offer many different types of policies: car, homeowners, flood, earthquake, health, etc.    Insurance policies have a lot of data and behavior in common.   But they also have differences.   You want to build each insurance policy class so that you can reuse as much code as possible.   Avoid duplicating code.   You’ll save time, have less code to...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT