Add the following overloaded operators to the Employee class (developed in Lab2)
ID: 3586067 • Letter: A
Question
Add the following overloaded operators to the Employee class (developed in Lab2): Overload the operator = - It should copy each of data members of the Employee object Overload the operator + - It should add the Salary and Retirement contribution of two Employee objects and create another Employee object with Employee Name as “Aggregate” and ID as 111. Overload the operator <<: - Output the Name, Employee ID, Salary and Retirement Contributions in separate lines Test the overloaded operators by creating three different employee objects. The program should prompt the user to enter the data for creating these employee objects. The program should output the result of these operations (= and +) using the overloaded operator <<. `The program should be developed in Visual Studio
lab#2
#include <iostream>
#include<conio.h>
#include<string>
using namespace std;
// Class Declaration
class Employee
{
//Access - Specifier
private:
//Varibale Declaration
string name;
int Emp_ID;
int Salary;
double retirement_cont;
public:
Employee();
Employee(string emp_name,int emp_id,int sal,double ret_count);
int getSal(void);
double getRet(void);
void DisplayEmp(void);
};
Employee::Employee(string emp_name,int emp_id,int salary,double ret_cont=0 ) {
name=emp_name;
Emp_ID=emp_id;
Salary=salary;
retirement_cont=(0.06)*salary;
}
int Employee::getSal(void){
return Salary;
}
double Employee::getRet(void){
return retirement_cont ;
}
void Employee::DisplayEmp(void){
cout<<"-------------Displaying Employee Information-------" << endl;
cout << "Employee Name: " << name << endl;
cout << "Employee ID: " << Emp_ID << endl;
cout << "Employee Salary: " << getSal() << endl;
cout << "Employee Retirement Contributions: " << getRet() << endl;
cout << "monthly gross salary of the employee: " << getSal()/12 << endl;
cout << "Net Salary of the employee: " << getSal()-getRet() << endl;
cout<<"--------------------" << endl;
}
//Main Function
int main()
{
// Object Creation For Class
Employee obj1=Employee("alex",111,12000);
cout << "Employee Salary: " << obj1.getSal() << endl;
cout << "Employee Retirement Contributions: " << obj1.getRet() << endl;
obj1.DisplayEmp();
Employee obj2=Employee("brain",112,100000);
cout << "Employee Salary: " << obj2.getSal() << endl;
cout << "Employee Retirement Contributions: " << obj2.getRet() << endl;
obj2.DisplayEmp();
Employee obj3=Employee("catia",111,12000);
cout << "Employee Salary: " << obj3.getSal() << endl;
cout << "Employee Retirement Contributions: " << obj3.getRet() << endl;
obj3.DisplayEmp();
Employee obj4=Employee("brain",114,40000);
cout << "Employee Salary: " << obj4.getSal() << endl;
cout << "Employee Retirement Contributions: " << obj4.getRet() << endl;
obj4.DisplayEmp();
Employee obj5=Employee("ela",115,15630
);
cout << "Employee Salary: " << obj5.getSal() << endl;
cout << "Employee Retirement Contributions: " << obj5.getRet() << endl;
obj5.DisplayEmp();
}
Explanation / Answer
#include <iostream>
#include<string>
using namespace std;
// Class Declaration
class Employee
{
//Access - Specifier
private:
//Varibale Declaration
string name;
int Emp_ID;
int Salary;
double retirement_cont;
public:
Employee();
Employee(string emp_name,int emp_id,int sal,double ret_count);
int getSal(void);
double getRet(void);
void DisplayEmp(void);
Employee operator+(Employee);
Employee operator=(Employee);
friend ostream &operator<<( ostream &,const Employee & );
};
Employee::Employee()
{
name=" ";
Emp_ID=0;
Salary=0;
retirement_cont = 0;
}
Employee::Employee(string emp_name,int emp_id,int salary,double ret_cont=0 ) {
name=emp_name;
Emp_ID=emp_id;
Salary=salary;
retirement_cont=(0.06)*salary;
}
int Employee::getSal(void){
return Salary;
}
double Employee::getRet(void){
return retirement_cont ;
}
void Employee::DisplayEmp(void){
cout<<"-------------Displaying Employee Information-------" << endl;
cout << "Employee Name: " << name << endl;
cout << "Employee ID: " << Emp_ID << endl;
cout << "Employee Salary: " << getSal() << endl;
cout << "Employee Retirement Contributions: " << getRet() << endl;
cout << "monthly gross salary of the employee: " << getSal()/12 << endl;
cout << "Net Salary of the employee: " << getSal()-getRet() << endl;
cout<<"--------------------" << endl;
}
//overload operators
Employee Employee::operator+(Employee E1)
{
Employee E;
E.name = "aggregate";
E.Emp_ID = 111;
E.Salary = this->Salary + E1.Salary;
E.retirement_cont = this->retirement_cont + E1.retirement_cont;
return E;
}
Employee Employee::operator=(Employee E1)
{
this->name = E1.name;
this->Emp_ID = E1.Emp_ID;
this->Salary = E1.Salary;
this->retirement_cont = E1.retirement_cont;
return *this;
}
ostream &operator<<( ostream &output,const Employee &E )
{
output << " Name : "<<E.name<<" Employee ID : "<<E.Emp_ID<<" Salary : "<<E.Salary <<" Retirement Contribution : "<<E.retirement_cont ;
return output;
}
//Main Function
int main()
{
// Object Creation For Class
Employee obj1=Employee("alex",111,12000);
cout << "Employee Salary: " << obj1.getSal() << endl;
cout << "Employee Retirement Contributions: " << obj1.getRet() << endl;
obj1.DisplayEmp();
Employee obj2=Employee("brain",112,100000);
cout << "Employee Salary: " << obj2.getSal() << endl;
cout << "Employee Retirement Contributions: " << obj2.getRet() << endl;
obj2.DisplayEmp();
Employee obj3=Employee("catia",111,12000);
cout << "Employee Salary: " << obj3.getSal() << endl;
cout << "Employee Retirement Contributions: " << obj3.getRet() << endl;
obj3.DisplayEmp();
Employee obj4=Employee("brain",114,40000);
cout << "Employee Salary: " << obj4.getSal() << endl;
cout << "Employee Retirement Contributions: " << obj4.getRet() << endl;
obj4.DisplayEmp();
Employee obj5=Employee("ela",115,15630
);
cout << "Employee Salary: " << obj5.getSal() << endl;
cout << "Employee Retirement Contributions: " << obj5.getRet() << endl;
obj5.DisplayEmp();
cout<<" operator overloading : ";
obj4 = obj2; // overload = operator
cout<<obj4;
obj5 = obj4 +obj3; //overload + operator
cout<<" obj5 is aggregate object after adding salary and retirement contribution of obj4 and obj3 :";
cout<<obj5; //overload ostream operator
}
Output:
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.