(Pure Virtual Function) Write the above program by modifying by making display() as pure virtual function.
#include<iostream>
#include<string>
using namespace std;
class Person
{
int ID;
string name;
public:
Person(int _ID, string _name):ID(_ID),name(_name){}
//Pure virtual function
virtual void Display() = 0;
//Getters
int GetID() { return ID;}
string GetName() { return name;}
};
class Worker:public Person
{
double salary;
string prof;
public:
Worker(int _ID, string _name, double _salary,string _prof)
:Person(_ID,_name),salary(_salary),prof(_prof){}
virtual void Display()
{
cout << "\nWorker`s name is\t" << GetName()
<< "\nWorker`s ID is\t\t" << GetID()
<< "\nWorker`s profession is\t" << prof
<< "\nWorker`s salary is\t" << salary;
}
};
int main()
{
Worker w(1234, "Jack", 3000, "Mechanic");
Person* p;
p = &w;
p->Display();
}
Comments
Leave a comment