In C++ Design a class called Person with one derived class Employee. Design a de
ID: 3838467 • Letter: I
Question
In C++
Design a class called Person with one derived class Employee.
Design a derived class of Employee called Faculty.
Person class has name and address attributes
Employee class has office location and salary attributes
Faculty has office hours and rank attributes
also
Define a virtual toString function in Person class and override it in each class to display all relevant fields described.
In your test code, create an array of pointers of the base type. Then, randomly populate it with references to instances of Person, Employee, and Faculty objects. Give all fields in each object some relevant and meaningful values.
In a for-loop, invoke toString( ) on these instances to demonstrate that it is polymorphic.
Explanation / Answer
#include <iostream>
#include <string>
using namespace std;
class Person
{
public:
string name;
string address;
virtual void toString(){
}
};
class Employee:public Person
{
public:
string officeLocation;
double slary;
};
class Faculty:public Employee
{
public:
int hours;
int rank;
void toString(){
cout<<"Person Name "<<" "<<name<<"Employee Address "<<address<<" ";
cout<<"OfficeLocation "<<" "<<officeLocation<<"Salary "<<slary<<" ";
cout<<"Office Hours"<<" "<<hours<<" Faculty Rank"<<" "<<rank;
}
};
int main()
{
Person p;
Faculty fp;
cout<<"Enter Person Name ";
cin>>fp.name;
cout<<" Enter Person Address ";
cin>>fp.address;
cout<<" Enter Employee Office Location ";
cin>>fp.officeLocation;
cout<<" Enter Employee Salary ";
cin>>fp.slary;
cout<<" Enter Faculty Hours ";
cin>>fp.hours;
cout<<" Enter Faculty Rank ";
cin>>fp.rank;
fp.toString();
}
Output:
Enter Person Name
ram
Enter Person Address
us
Enter Employee Office Location
us
Enter Employee Salary
1200
Enter Faculty Hours
12
Enter Faculty Rank
2
Person Name
ram
Employee Address
us
OfficeLocation
us
Salary
1200
Office Hours
12
Faculty Rank
2