Your Ad Here

Sunday, February 10, 2008

Operator Overloading

/* C++ Program for the IMPLEMENTATION OF OPERATOR OVERLOADING(BINARY). The Given Program performs Basic Arithematic operation : Addition, Subtraction, Multiplication and Division for Two Complex Numbers. */

#include< iostream.h>
#include< conio.h>
#include< process.h>
class complex
{
float real;
float imag;
public:
complex()
{}
complex(float x,float y)
{
real=x;
imag=y;
}
complex operator + (complex);
complex operator - (complex);
complex operator * (complex);
complex operator / (complex);
void display(void)
{
cout< < real< < " +i" < < i mag< < endl;
}

};

complex complex :: operator +(complex c)
{
complex c2;
c2.real=real+c.real;
c2.imag=imag+c.imag;
return (c2);
}

complex complex :: operator -(complex c)
{
complex c2;
c2.real=real-c.real;
c2.imag=imag-c.imag;
return (c2);
}
complex complex :: operator *(complex c)
{
complex c2;
c2.real = ((real * c.real) - (imag * c.imag));
c2.imag = ((real * c.imag) + (imag * c.imag));
return (c2);
}
complex complex :: operator /(complex c)
{
complex c2;
c2.real=((real * c.real) + (imag * c.imag))/((real * c.real) + (imag * c.imag));
c2.imag=((imag * c.real) - (real * c.imag))/((real * c.real) + (imag * c.imag));
return (c2);
}

void main()
{
clrscr();
complex c1,c2,c3;
int op;
char ch,y,Y;

c1 = complex(5.6,2.7);
c2 = complex(3.5,5.6);
cout< < "Two Complex numbers Are :"< < endl;
c1.display();
c2.display();
do
{
cout< < endl< < "******** MENU *********"< < endl;
cout< < "1. Addition\n2. Subtraction\n3. Multiplication\n4. Division\n5. Exit"< < endl;
cout< < "Enter Your Choice : ";
cin>>op;
switch(op)
{
case 1:
c3 = c1 + c2;
cout< < "Addition of Two complex Nos. :";
c3.display();
break;

case 2:
c3 = c1 - c2;
cout< < "Subtraction of Two complex Nos. :";
c3.display();
break;

case 3:
c3 = c1 * c2;
cout< <" Multiplication of Two complex Nos. :";
c3.display();
break;

case 4:
c3 = c1 / c2;
cout< < "division of Two complex Nos. :";
c3.display();
break;

case 5:exit(0);

default: cout< < endl< < "Aborting!!!!!!!INVALID CHOICE"< < endl;
}
cout< < " Do you want to continue(Y/y)";
cin>>ch;
}
while(ch=='y'||ch=='Y');
getch();
}
/******************* OUTPUT ********************
Two Complex numbers Are :
5.6 +i2.7
3.5 +i5.6

******** MENU *********
1. Addition
2. Subtraction
3. Multiplication
4. Division
5. Exit
Enter Your Choice : 1
Addition of Two complex Nos. :9.1 +i8.3
Do you want to continue(Y/y)y

******** MENU *********
1. Addition
2. Subtraction
3. Multiplication
4. Division
5. Exit
Enter Your Choice : 2
Subtraction of Two complex Nos. :2.1 +i-2.9
Do you want to continue(Y/y)y

******** MENU *********
1. Addition
2. Subtraction
3. Multiplication
4. Division
5. Exit
Enter Your Choice : 3
Multiplication of Two complex Nos. :4.48 +i46.48
Do you want to continue(Y/y)y

******** MENU *********
1. Addition
2. Subtraction
3. Multiplication
4. Division
5. Exit
Enter Your Choice : 4
division of Two complex Nos. :1 +i-0.631048
Do you want to continue(Y/y)n
*/

No comments:

Your Ad Here