C++ program to implement friend function

/* Use friend function for deducing tax from salary of manager & employee */


#include<iostream.h>
#include<conio.h>
class employee;
class manager
{
private:
   char name[15];
   int age;
   char address[30];
   float salary;
public:
   void read()
   {
    cout<<"Enter Manager Information"<<endl<<endl;
    cout<<"Enter name:- ";
    cin>>name;
    cout<<"Enter age:- ";
    cin>>age;
    cout<<"Enter address:- ";
    cin>>address;
    cout<<"Enter salary:- ";
    cin>>salary;
    cout<<endl<<endl;
   }


   void show()
   {
    cout<<"Manager name:- "<<name<<endl;
    cout<<"Age:- "<<age<<endl;
    cout<<"Address:- "<<address<<endl;
    cout<<"Salary:- "<<salary<<endl;
    cout<<endl<<endl;
   }


   friend float deduction(manager M,employee E)
};


class employee
{
private:
   char emp_name[15];
   int emp_age;
   char emp_address[30];
   float emp_salary;
public:
   void accept()
   {
    cout<<"\n Enter Employee details"<<endl<<endl;
    cout<<"Enter employee name:- ";
    cin>>emp_name;
    cout<<"Enter age:- ";
    cin>>emp_age;
    cout<<"Enter address:- ";
    cin>>emp_address;
    cout<<"Enter salary:- ";
    cin>>emp_salary;
    cout<<endl<<endl;
   }


   void display()
   {
    cout<<"Employee name:- "<<emp_name<<endl;
    cout<<"Age:- "<<emp_age<<endl;
    cout<<"Address:- "<<emp_address<<endl;
    cout<<"Salary:- "<<emp_salary<<endl;
    cout<<endl<<endl;
   }


   friend float deduction(manager M ,employee E)
};


float deduction(manager M,employee E)
{
float msal,esal;
M.salary;
E.emp_salary;
if(M.salary>15000)
{
 cout<<"\n Manager salary deducted by 10%";
 msal=M.salary * (0.1);
 M.salary=M.salary-msal;
 cout<<"\n Manager salary:- "<<M.salary<<endl;
}


if(E.emp_salary>15000)
{
 cout<<"\n Employee salary deducted by 10%";
 esal=E.emp_salary * (0.1);
 E.emp_salary=E.emp_salary-esal;
 cout<<"\n Employee Salary:- "<<E.emp_salary<<endl;
}
 return 0;
}


int main()
{
clrscr();
manager M;
employee E;
M.read();
E.accept();
M.show();
E.display();
deduction(M,E);
getch();
return 0;
}


Show your appreciation by liking or sharing the post:
Previous
Next Post »