Create a class named Person, which contains
•A pure virtual function named print()
•Two data fields i.e. personName and age
A class named Patient inherits Person class, which contains
•Two data fields i.e. diseaseType and recommendedMedicine
•Overridden function print() to display all details relevant to a patient
A class named MedicarePatient inherited from class Patient, which holds
•A data field representing the name of the hospital
•A data filed representing the name of the ward
•A data field representing room number
•Overridden function print() to display all details relevant to a patient
In the main function, create instances of derived classes to access respective print() function using dynamic binding.
#include <iostream>
#include <string>
using namespace std;
class Person{
private:
string personName;
int age;
public:
Person(){}
Person(string personName,int age){
this->personName=personName;
this->age=age;
}
virtual void print()=0{
cout<<"Person name: "<<personName<<"\n";
cout<<"Person age: "<<age<<"\n";
}
};
//A class named Patient inherits Person class, which contains
class Patient:public Person{
private:
//Two data fields i.e. diseaseType and recommendedMedicine
string diseaseType;
string recommendedMedicine;
public:
Patient(){}
Patient(string personName,int age,string diseaseType,string recommendedMedicine):Person(personName,age){
this->diseaseType=diseaseType;
this->recommendedMedicine=recommendedMedicine;
}
///Overridden function print() to display all details relevant to a patient
void print(){
Person::print();
cout<<"Patient disease type: "<<diseaseType<<"\n";
cout<<"Patient recommended medicine: "<<recommendedMedicine<<"\n";
}
};
//A class named MedicarePatient inherited from class Patient, which holds
class MedicarePatient:public Patient{
private:
//A data field representing the name of the hospital
string nameHospital;
//A data filed representing the name of the ward
string nameWard;
//A data field representing room number
int roomNumber;
public:
MedicarePatient(){}
MedicarePatient(string personName,int age,string diseaseType,string recommendedMedicine,string nameHospital,string nameWard,int roomNumber):
Patient(personName,age,diseaseType,recommendedMedicine){
this->nameHospital=nameHospital;
this->nameWard=nameWard;
this->roomNumber=roomNumber;
}
//Overridden function print() to display all details relevant to a patient
void print(){
Patient::print();
cout<<"Medicare Patient name hospital: "<<nameHospital<<"\n";
cout<<"Medicare Patient name ward: "<<nameWard<<"\n";
cout<<"Medicare Patient room number: "<<roomNumber<<"\n";
}
};
int main(void){
//In the main function, create instances of derived classes to access respective print() function using dynamic binding.
Person** person=new Person*[2];
person[0]= new Patient("Peter",25,"disease type 1","Recommended medicine 1");
person[1]= new MedicarePatient("Mike",45,"disease type 2","Recommended medicine 2","name Hospital","nameWard 45",5);
for(int i=0;i<2;i++){
person[i]->print();
}
system("pause");
return 0;
}
Comments
Leave a comment