Question: Part 3 : Friend Function Consider the following Employee class: / / Employee . h class Employee { private: string name; double salary; public: Employee

Part 3: Friend Function
Consider the following Employee class:
//Employee.h
class Employee
{
private:
string name;
double salary;
public:
Employee(string n, double s);
string getName();
double getSalary();
friend bool operator==(Employee&, Employee&);
friend void operator+=(Employee& e1, Employee& e2);
};
bool operator==(Employee& e1, Employee& e2)
{
return (e1.name == e2.name);
}
void operator+=(Employee& e1, Employee& e2)
{
e1.salary += e2.salary;
}
//main.cpp
#include Employee.h
int main()
{
Employee e1("Bob",50);
Employee e2("Bill",50);
cout <<(e1== e2)<< endl;
e1+= e2;
cout << e1.getSalary()<<""<< e2.getSalary()<< endl; //10050
return 0;
}
Compile and run the programs.
Your Turn:
(2 pts) Declare a private static member variable named count in the Employee class, whenever the new object has created, increment the count variable. In addition, add a method named getCount() that returns the value of this static variable.
(5 pts) Overload the + operator as a friend function that needs two Employee objects as parameters, adds two employees names and salaries, assign them to new employee object, and then return new employee object.
(3 pts) The main function declares three objects of Employee. Add the first two employee objects and then display the third employees name and salary to the screen. In addition, display the value of static variable count.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!