Saturday, August 6, 2011

Implementation of Unary & Binary operator overloading using member function.


#include<iostream.h>
#include<conio.h>

class point     //class for unary operator overloading
{
private:
int x,y;
public:
point(int i,int j)
{
x=i;
y=j;
}
void show()
{
cout<<"point="<<"("<<x<<","<<y<<")"<<endl;
}
void operator++()
{
x++;
y++;
}
};

class complex        //class for binary operator overloading
{
private:
double x,y;
public:
void input()
{
double p,q;
cout<<"enter the real part"<<endl;
cin>>p;
cout<<"enter the imaginary part"<<endl;
cin>>q;
x=p;
y=q;
}
void show()
{
cout<<"complex no. is"<<x<<"+i"<<y<<endl;
}
complex operator+(complex &c)
{
complex z;
z.x=x+(c.x);
z.y=y+(c.y);
return(z);
}
complex operator*(complex &c)
{
complex z;
z.x=x*c.x-y*c.y;
z.y=x*c.y+y*c.x;
return(z);
}
};

void main()
{
clrscr();
point p1(2,2);
p1.show();
 cout<<"after increment"<<endl;
++p1;
p1.show();
complex c1,c2,c3;
c1.input();
c1.show();
c2.input();
c2.show();
c3=c1+c2;
cout<<"after addition"<<endl;
c3.show();
c3=c1*c2;
cout<<"after multiplication"<<endl;
c3.show();
getch();
}


//For any errors pls post comment.....

1 comment:

  1. Trading Software That Automatically Makes You Money. Mobile Forex Trading Is The First And Only Trading Platform That Automatically Makes You Money With Little To No Effort On Your Part. Developed By A Team Of Mathematicians, Stock Market Professionals, And Computer Scientists, Mobile Forex Trading Is Guaranteed To Make You A Consistent Income, Even While You Sleep.

    ReplyDelete