Saturday, 9 March 2019

CPP-EX26


 Write a C++ program using class to overload the operator Binaryfor an integer number.

#include <iostream.h>
#include<conio.h>
class binary
{
private:
int x;
int y;

public:
binary () { }    //Constructor
binary(int real,int imag){x=real;y=imag;}
binary operator+(binary);
void display(void);
};


binary binary :: operator+(binary b)
//Binary operator overloading for + operator defined
{
binary temp;
temp.x = x+b.x;
temp.y = y+b.y;
return (temp);
}
 void binary::display(void)
 {
 cout<<x<<"+j"<<y<<"\n";
 }

int main( )
{
clrscr();

/*binary e1,e2,e3;             //Objects e1, e2, e3 created
cout<<"\n Enter value for Object e1:-";
e1.display( );
cout<<"\n Enter value for Object e2:-";
e2.display( );
e3= e1+ e2;                  //Binary Overloaded operator used
cout<< "\n Value of e1 is:-"<<e1.display();
cout<< "\n Value of e2 is:-"<<e2.display();
cout<< "\n Value of e3 is:-"<<e1+e2;
*/
binary b1,b2,b3;
b1=binary(1,2);
b2=binary(2,3);
b3=b1+b2;

cout<<"b1=";b1.display();
cout<<"b2=";b2.display();
cout<<"b3=";b3.display();

return 0;
getch();

}