Write a C++ program to create two objects of a class called company and add their data members using an operator overloaded function for ‘+’ operator and '-'operator


Implementation of the above problem:
#include<iostream.h>
#include<conio.h>
class company
{
int sal;
int nop;
public:
friend void print(company &);
company()
{
sal=0;
nop=0;
}
company operator+(company);
company company:: operator-(company c)
{
company temp;
temp.sal=sal-c.sal;
temp.nop=nop-c.nop;
return temp;
}
void set_data(int s, int n)
{
sal=s;
nop=n;
}
};
void print(company &sum)

{
cout<<"Total Salary is :"<<sum.sal<<endl;
cout<<"Total No: of Parts is:"<<sum.nop<<endl;
}
company company:: operator+(company c)
{
company temp;
temp.sal=sal+c.sal;
temp.nop=nop+c.nop;
return temp;
}
void main()
{
clrscr();
int s,n;
company sum,ram,clif,sub;
cout<<"Enter the salary for Ram";
cin>>s;
cout<<"Enter No:Of Parts for Ram:";
cin>>n;
ram.set_data(s,n);
cout<<"Enter the salary for clif";
cin>>s;
cout<<"Enter No:Of Parts for clif:";
cin>>n;
clif.set_data(s,n);
sum=ram+clif;
print(sum);
sub=sum-ram;
print(sub);
getch();
}



Thanks
Mukesh Rajput
Mukesh Rajput

Mukesh Rajput

I am a Computer Engineer, a small amount of the programming tips as it’s my hobby, I love to travel and meet people so little about travel, a fashion lover and love to eat food, I am investing a good time to keep the body fit so little about fitness also..

Post A Comment:

0 comments: