C++ Program to add weight of two objects using + operator overloading
/* 6. Write a program to add weight of two objects using + operator
overloading*/
#include<iostream.h>
#include<conio.h>
class weight
{
private:
int a;
public:
weight()
{
a=0;
}
void input()
{
cout<<“Enter weight in kg:”<<endl;
cin>>a;
}
weight operator+(weight w)
{
weight x;
x.a=a+w.a;
return x;
}
void display()
{
cout<<“Weight of two objects =\t”<<a<<“kg”<<endl;
}
};
void main()
{
clrscr();
weight o1,o2,tot;
o1.input();
o2.input();
tot=o1+o2;
tot.display();
getch();
}