Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

File View Edit Insert Review Pen COSC 1437 870 Lab 3-10% Add the following overl

ID: 3722809 • Letter: F

Question


File View Edit Insert Review Pen COSC 1437 870 Lab 3-10% Add the following overloaded operators to the Employee class (developed in Lab2) Overload the assignment operator () It should copy each of data members of the Employee object on the right hand side to the Employee object on the left hand side of the assignment operator Overload the addition operator (+) It should add the Salary and Retirement contribution of two Employee objects and create another Employee object with Employee Name as "Aggregate' with a dummy employee ID as 111. Overload the operator () Output the Name, Employee ID, Salary and Retirement Contributions in separate lines Test the overloaded operators bv creating three different emplovee obiects. The proaram should promot the user to enter the data for creatina these emplovee obiects. The proaram should outout the result of these operations ( and +)- use the overloaded oDerator

Explanation / Answer

//Code is explained in comments:

#include "stdafx.h"
#include <cstdio>
#include <iostream>
#include <cstdlib>
#include <string>
#include <cstring>

using namespace std;

class Employee{ //employee class

string Name;
int Emp_Id;
int Gross_Salary;
double Retire_amt;


public:

Employee(string name="",int emp_id=0,int salary=0){ //constructor

Name=name;
Emp_Id=emp_id;
Gross_Salary=salary;
Retire_amt=0.06 *Gross_Salary;

}

void DisplayEmp(){ //displayin the empployee object

cout<<"Name:"<<Name<<endl;
cout<<"EMp_id:"<<Emp_Id<<endl;
cout<<"Gross_Annual_Salary:"<<Gross_Salary<<endl;
cout<<"Annual Retirement COntribution:"<<0.06 *Gross_Salary<<endl;
cout<<"Annual Net Salary :"<<Gross_Salary-(0.06 *Gross_Salary)<<endl;
cout<<"Monthly Salary:"<<Gross_Salary/12<<endl;

}

void setValues(){ //this function prompts the user to enter details

cout<<"Enter Employee Name:";
cin>>Name;
cout<<" Enter Emp:";

cin>>Emp_Id;
cout<<" Enter Annual Gross Salary:";
cin>>Gross_Salary;


}
int GetSalary(){
return Gross_Salary;
}

double GetRet()
{
return Retire_amt;
}


Employee operator + (Employee const & Emp){ //overoading of + operator

Employee Aggregate;
Aggregate.Name="Aggregate";
Aggregate.Emp_Id=111;
Aggregate.Gross_Salary=Gross_Salary+Emp.Gross_Salary;
Aggregate.Retire_amt=0.06 *Gross_Salary+(0.06*Emp.Gross_Salary);

return Aggregate;

}

void operator =(Employee const &Emp){ //overloadin of assignment operator
this->Name=Emp.Name;
this->Emp_Id=Emp.Emp_Id;
this->Gross_Salary=Emp.Gross_Salary;
this->Retire_amt=Emp.Gross_Salary*0.06;
}
friend ostream &operator <<(ostream &out,const Employee & emp);
};


/*

operators << and >> must be overloaded as a global function. And if we want to allow them to access private data members of class, we must make them friend.

Why these operators must be overloaded as global?
In operator overloading, if an operator is overloaded as member, then it must be a member of the object on left side of the operator. For example, consider the statement “ob1 + ob2” (let ob1 and ob2 be objects of two different classes). To make this statement compile, we must overload ‘+’ in class of ‘ob1’ or make ‘+’ a global function.
The operators ‘<<' and '<<' are called like 'cout << ob1' and 'cin << ob1'. So if we want to make them a member method, then they must be made members of ostream and istream classes, which is not a good option most of the time. Therefore, these operators are overloaded as global functions with two parameters, cout and object of user defined class

*/
ostream &operator <<(ostream & out,const Employee &emp){

out<<"Name:"<<emp.Name<<endl;
out<<"EMp_id:"<<emp.Emp_Id<<endl;
out<<"Gross_Annual_Salary:"<<emp.Gross_Salary<<endl;
out<<"Annual Retirement COntribution"<<emp.Gross_Salary*0.06<<endl;
out<<"Annual Net Salary :"<<emp.Gross_Salary-(emp.Gross_Salary*0.06)<<endl;
out<<"Monthly Salary:"<<emp.Gross_Salary/12<<endl;
}
int main(){

Employee emp[5];

for(int i=0;i<5;i++){ //setting the values of objects

emp[i].setValues();
}

for(int i=0;i<5;i++){//Displaying all the 5 objects

emp[i].DisplayEmp();
}


Employee Aggregate=emp[0]+emp[1]; // adding two objects by +
cout<<"After addition of two objects and assinging to Aggregate Object"<<endl;
cout<<Aggregate;
Aggregate=emp[2]; //Assigning one object to another by assignment operator
cout<<"------------After Assignment operator operation"<<endl;
cout<<Aggregate; //Displaying Aggregate object by << operator
return 0;
}

//Output:


Enter Employee Name:RAM

Enter Emp:001

Enter Annual Gross Salary:65473
Enter Employee Name:CHaran

Enter Emp:002

Enter Annual Gross Salary:646345  
Enter Employee Name:Yeni

Enter Emp:003

Enter Annual Gross Salary:84563
Enter Employee Name:Halow

Enter Emp:004

Enter Annual Gross Salary:845454
Enter Employee Name:Krish

Enter Emp:005

Enter Annual Gross Salary:6742897
Name:RAM
EMp_id:1
Gross_Annual_Salary:65473
Annual Retirement COntribution:3928.38
Annual Net Salary :61544.6
Monthly Salary:5456
Name:CHaran
EMp_id:2
Gross_Annual_Salary:646345
Annual Retirement COntribution:38780.7
Annual Net Salary :607564
Monthly Salary:53862
Name:Yeni
EMp_id:3
Gross_Annual_Salary:84563
Annual Retirement COntribution:5073.78
Annual Net Salary :79489.2
Monthly Salary:7046
Name:Halow
EMp_id:4
Gross_Annual_Salary:845454
Annual Retirement COntribution:50727.2
Annual Net Salary :794727
Monthly Salary:70454
Name:Krish
EMp_id:5
Gross_Annual_Salary:6742897
Annual Retirement COntribution:404574
Annual Net Salary :6.33832e+06
Monthly Salary:561908
After addition of two objects and assinging to Aggregate Object
Name:Aggregate
EMp_id:111
Gross_Annual_Salary:711818
Annual Retirement COntribution42709.1
Annual Net Salary :669109
Monthly Salary:59318
------------After Assignment operator operation
Name:Yeni
EMp_id:3
Gross_Annual_Salary:84563
Annual Retirement COntribution5073.78
Annual Net Salary :79489.2
Monthly Salary:7046

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote