Answer to Question #269211 in C++ for T\/\/0

Question #269211

(Pure Virtual Function) Write the above program by modifying by making display() as pure virtual function.


1
Expert's answer
2021-11-20T09:49:24-0500
#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();
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS