Question #224833

Design a class named Employee with following properties: name, employee ID and salary. Add

necessary constructors to initialize the data members. Add two methods, (1) Update(salary) to modify

the salary for the employee object and (2) Display() to print the employee details on screen. In main,

read an employee’s details from user, construct employee object from the user input, update

employee’s salary by calling appropriate function and display the employee’s details.


Expert's answer

#include <iostream>
#include <string>






using namespace std;




class Employee {
private:
	string name;
	int employeeID;
	float salary;
public:
	// A public default constructor 
	Employee (){
		this->name="";
		this->employeeID=1;
		this->salary=0;
	}
	//constructor to initialize the data members
	Employee (string name,int employeeID,float salary){
		this->name=name;
		this->employeeID=employeeID;
		this->salary=salary;
	}
	//(1) Update(salary) to modify the salary for the employee object
	void Update(float salary){
		this->salary=salary;
	}
	//(2) Display()
	void Display(){
		cout<<"Employee name: "<<this->name<<"\n";
		cout<<"Employee ID: "<<this->employeeID<<"\n";
		cout<<"Employee salary: "<<this->salary<<"\n";
	}
};


int main()
{
	string name;
	int employeeID;
	float salary;
	//In main, read an employee’s details from user, construct employee object from the user input, 
	
	cout<<"Enter employee name: ";
	getline(cin,name);
	cout<<"Enter employee ID: ";
	cin>>employeeID;
	cout<<"Enter employee salary: ";
	cin>>salary;
	Employee employee(name,employeeID,salary);
	employee.Display();
	cout<<"\n";
	//update employee’s salary by calling appropriate function and display the employee’s details.
	cout<<"Enter a new employee salary: ";
	cin>>salary;
	employee.Update(salary);


	employee.Display();


	int k;
	cin>>k;
	return 0;
}







LATEST TUTORIALS
APPROVED BY CLIENTS