Monday, April 5, 2021

binary operator overloading (OOP) (34)

                                   /*binary operator overloading

   *****************************/


#include<iostream>

using namespace std;

class binary

{

private:

int a,b;

public:

binary()

{

a=0;

b=0;

}

binary(int x,int y)

{

a=x;

b=y;

}

binary operator +(binary t);

void display();

};

binary binary :: operator +(binary t)//here we define the non static data member to overload the unary operator

{

binary temp;

temp.a=a+t.a;

temp.b=b+t.b;

return temp;

}

void binary::display()

{

cout<<"value of a is:"<<a<<endl;

cout<<"value of b is:"<<b<<endl;

}

int main()

{

binary t1,t2,t3;

t1=binary(30,80);

t2=binary(40,56);

t3=t1+t2;

t1.display();

t2.display();

t3.display();

} 

No comments:

Post a Comment