WELCOME

Welcome to KoDes4U

Pages

Saturday, March 5, 2011

operator overlaoding (C++ program)

/*Design a class Complex with data members for real and imaginary part.
Providedefault parameterized constructors. Write a program to perform
arithmetic operations of two complex numbers using operator overloading
(using either member functions or friend functions).
Coded for KoDes4U.blogspot.com*/


#include
#include

class complex
{
int x1,y1,x2,y2,x,y,a;
float x3,y3;
public:
void operator +(complex *c)
{
cout<<"Original Complex:"<
=0)
cout<<"\n1)"<
x1<<"+"<y1<<"i"<x1<y1<<"i"<=0)
cout<<"\n2)"<
x2<<"+"<y2<<"i"<x2<y2<<"i"<x=c->x1+c->x2;
c->y=c->y1+c->y2;
cout<
x<<"+"<<"i"<y;
}
void operator -(complex *c)
{
cout<<"Original Complex:"<
=0)
cout<<"\n1)"<
x1<<"+"<y1<<"i"<x1<y1<<"i"<=0)
cout<<"\n2)"<
x2<<"+"<y2<<"i"<x2<y2<<"i"<x=c->x1-c->x2;
c->y=c->y1-c->y2;
cout<<"\nAfter subtraction:"; if(y>0)
cout<<"\nResulted Complex:"<
x<<"+"<y<<"i"; else if(y==0) { cout<<"n\Resulted Complex:"<x<<"+"<<"0i"; cout<<"\nMore specifically:"<x;
}
else if(y<0) cout<<"\nResulted Complex is:"<
x<y<<"i"; else if(x==0&&y==0) { cout<<"\nResulted Complex is:"<x<<"+"<<"0i"; cout<<"\nMore specifically:"<<"0"; } } void operator *(complex *c) { cout<<"Original Complex:"<=0)
cout<<"\n1)"<
x1<<"+"<y1<<"i"<x1<y1<<"i"<=0)
cout<<"\n2)"<
x2<<"+"<y2<<"i"<x2<y2<<"i"<x=(c->x1*c->x2);
c->y=(c->y1*c->y2);
cout<
x<<"+"<y<<"i"; } else { c->x=(c->x1*c->x2)-(c->y1*c->y2);
c->y=(c->x1*c->y2)+(c->y1*c->x2);
cout<
x<<"+"<y<<"i"; } } void operator =(complex *c) { cout<<"Entering of two complex nos:"; cout<<"\nEnter the 1st complex number, real part first and then imaginary part:"; cin>>c->x1>>c->y1;
cout<<"\nEnter the 2nd complex number, real part first and then imaginary part:"; cin>>c->x2>>c->y2;
}
void operator /(complex *c)
{

cout<<"Original Complex:"<
=0)
cout<<"\n1)"<
x1<<"+"<y1<<"i"<x1<y1<<"i"<=0)
cout<<"\n2)"<
x2<<"+"<y2<<"i"<x2<y2<<"i"<x2*c->x2)+(c->y2*c->y2);
c->x3=(float)(c->x1*c->x2)/a+(c->y1*c->y2)/a;
c->y3=(float)(c->y1*c->x2)/a-(c->y2*c->x1)/a;
cout<<"\nAfter division:\n"; if(c->y3>=0.0)
cout<
x3<<"+"<y3<<"i"<y3<0.0) cout<x3<y3<<"i"<>ch;
switch(ch)
{
case 1:c=(&c);
break;
case 2:c+(&c);
break;
case 3:c-(&c);
break;
case 4:c*(&c);
break;
case 5:c/(&c);
default:cout<<"Press any key to exit:";break;
}
}
while(ch!=6);
getch();
}

No comments:

Post a Comment