Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Concepts

Authors: David Kroenke, David Auer, Scott Vandenberg, Robert Yoder

8th Edition

013460153X, 978-0134601533

More Books

Students also viewed these Databases questions