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.....

No comments:

Post a Comment