Help please (Friend function & operator overloading) Write a friend function cal
ID: 3839452 • Letter: H
Question
Help please
(Friend function & operator overloading) Write a friend function called 'equal' for the class 'Enterprize' that was created above. The function takes two objects of this class and compares their categories. It returns a (true) Boolean value if they are equal. Write only the function declaration and the function definition (do not write the class definition again). In the main function, write statements to compare the categories of Salvation_army and HEB using the friend function 'equal' and print appropriate messages. Overload the 'mm' operator for the class 'Enterprize' that was created above. It should carry out exactly the same steps as that of the 'equal' function above. Write only the function declaration and the function definition. In the main function, write statements to compare the categories of Salvation_army and HEB using the overloaded operator 'mm' and to print appropriate messages.Explanation / Answer
#include<iostream>
using namespace std;
class Enterprize //base class
{
private:
char category;
public:
Enterprize(char category) // argument constructor
{
this->category = category;
}
char getCategory() //accessor method
{
return category;
}
friend bool equal(Enterprize obj1,Enterprize obj2);
bool operator ==(Enterprize obj)
{
if(this->category == obj.category)
return true;
else
return false;
}
};
bool equal(Enterprize obj1,Enterprize obj2)
{
if(obj1.category == obj2.category)
return true;
else
return false;
}
class not_for_profit: public Enterprize //derived class
{
private:
char sponsor;
public:
//passing argument to base class throught constructor
not_for_profit(char sponsor,char category):Enterprize(category)
{
this->sponsor = sponsor;
}
char getSponsor()
{
return sponsor;
}
};
class for_profit: public Enterprize
{
private:
int revenue;
public:
//passing argument to base class throught constructor
for_profit(int revenue,char category):Enterprize(category)
{
this->revenue = revenue;
}
char getRevenue()
{
return revenue;
}
};
int main()
{
not_for_profit Salvation_army('P','C');
for_profit HEB(1000000,'F');
if(Salvation_army == HEB)//compare categories using operator overloading
cout<<" Salvation_army has same category as that of HEB";
else
cout<<" Salvation_army does not have same category as that of HEB";
return 0;
}
Output:
Salvation_army does not have same category as that of HEB
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.