Answer to Question #217900 in C++ for Ahmed Ali

Question #217900

1) Create a class Worker with attributes name, age and salary. This class contain default constructor, getData, Display and a pure virtual function named as calculate commission with a virtual destructor.


a) A class Salaried that inherits from Worker contain parameterize constructor and a function that calculates the commission of the worker and display total salary constantly.


b) A class Hourly that inherits from Worker calculates the commission of Worker with of 5% and display total salary constantly. Instantiate Salaried and Hourly classes polymorphically.


Note: Commission of an employee of Salaried class is 10% and for class Hourly employee is 5%.  


1
Expert's answer
2021-07-16T03:17:43-0400
#include <iostream>
#include <string>
using namespace std;


//1) Create a class Worker 
class Worker{
protected:
	//with attributes name, age and salary. 
	string name;
	int age;
	float salary;
public:
	//This class contain default constructor, 
	Worker(){}


	//getData
	void getData(string name,int age,float salary){
		this->name=name;
		this->age=age;
		this->salary=salary;
	}
	//Display 
	void Display(){
		cout<<"Worker name: "<<name<<"\n";
		cout<<"Worker age: "<<age<<"\n";
		cout<<"Worker salary: "<<salary<<"\n";
	}
	//A pure virtual function named as calculate commission 
	virtual void calculateCommission()=0;
	//virtual destructor.
	virtual ~Worker(){
		
	}
};


//A class Salaried that inherits from Worker and 
class Salaried:public Worker{
private:
	//Salaried class is 10% 
	float commission;
public:
	// contain parameterize constructor
	Salaried(string name,int age,float salary){
		this->commission=0.1;
		getData(name,age,salary);
	}


	//a function that calculates the commission of the worker and display total salary constantly.
	void calculateCommission(){
		float commissionAmount=commission*salary;
		float totalSalary=salary+commissionAmount;
		cout<<"Commission 10%: "<<commissionAmount<<"\n";
		cout<<"Total salary: "<<totalSalary<<"\n";
	}
};


// A class Hourly that inherits from Worker calculates the commission of Worker with of 5% and display total salary constantly. 


class Hourly:public Worker{
private:
	//Salaried class is 5% 
	float commission;
public:
	// contain parameterize constructor
	Hourly(string name,int age,float salary){
		this->commission=0.05;
		getData(name,age,salary);
	}


	//a function that calculates the commission of the worker and display total salary constantly.
	void calculateCommission(){
		float commissionAmount=commission*salary;
		float totalSalary=salary+commissionAmount;
		cout<<"Commission 5%: "<<commissionAmount<<"\n";
		cout<<"Total salary: "<<totalSalary<<"\n";
	}
};



int main() {
	//Instantiate Salaried and Hourly classes polymorphically.
	Worker** workers=new Worker*[2];
	workers[0] = new Salaried("Mike Smith",25,1200);
	workers[1] = new Hourly("Peter Clark",30,1000);
	for(int i=0;i<2;i++){
		workers[i]->Display();
		workers[i]->calculateCommission();
		cout<<"\n";
	}
	system("pause");
	return 0;
}







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