OVERLOADING BINARY OPERATORS USING FRIEND FUNCTION
SOURCE CODE
#include “iostream.h”
#include
#include “conio.h”
class sample
{
int weight,height;
public:
void show();
sample()
{
};
sample(int w,int h);
friend sample operator+(sample &s1,sample &s2);
friend sample operator-(sample &s1,sample &s2);
friend sample operator*(sample &s1,sample &s2);
friend sample operator/(sample &s1,sample &s2);
friend sample operator%(sample &s1,sample &s2);
};
sample::sample(int w,int h)
{
weight=w;
height=h;
}
void sample::show()
{
cout << "\n The Weight Is " << weight="s1.weight+s2.weight;" height="s1.height+s2.height;" weight="s1.weight-s2.weight;" height="s1.height-s2.height;" weight="s1.weight*s2.weight;" height="s1.height*s2.height;" weight="s1.weight/s2.weight;" height="s1.height/s2.height;" weight="s1.weight%s2.weight;" height="s1.height%s2.height;" s3="s1+s2;" s4="s1-s2;" s5="s1*s2;" s6="s1/s2;" s7="s1%s2;" size="5">OUTPUT
Overloading All Binary Operators
Overloading + Operator
The Weight Is 150
The Height Is 150
Overloading - Operator
The Weight Is 50
The Height Is 50
Overloading * Operator
The Weight Is 5000
The Height Is 5000
Overloading / Operator
The Weight Is 2
The Height Is 2
Overloading % Operator
The Weight Is 0
The Height Is 0