Wednesday 12 October 2011

Complex Numbers with Operator Overloading and Type Conversion


Complex Numbers with Operator Overloading and Type Conversion

Aim:
To write a C++ program to implement complex number class with necessary operator overloading and type conversion

Algorithm:
Step 1: Start the program.
Step 2: Create a class Complex.
Step 3: Define the default and two parameterized constructors. One constructor is having two integer arguments and another one will have the two double data type arguments
Step 4: Declare the operator function which are going to be overloaded.
Step 5: Define the overloaded functions such as +,-,*,/,>>,<<.
Step 6: Create the objects and pass the complex values.
Step 7: Invoke the overloaded functions.
Step 8: Display the converted result.
Step 9: Stop the process.

Program:
#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
class complex
{
private:
float real;
float imag;
public:
complex()
{
real=imag=0.0;
}
complex(int r,int i) //conversion constructor
{
real = r;
imag = i;
}
complex(double r, double i)//conversion constructor
{
real = r;
imag = i;
}
friend istream& operator>>(istream &, complex &);
friend ostream& operator<<(ostream &, complex &);
complex operator+(complex);
complex operator-(complex);
complex operator*(complex);
complex operator/(complex);
friend double condou(complex t); //complex–>double
};
double condou(complex t)
{
return t.real+t.imag;
}
istream& operator >>(istream &in, complex &c)
{
cout<<”\nReal Part:”;
in>>c.real;
cout<<”Imag Part:”;
in>>c.imag;
return in;
}
ostream& operator<<(ostream &out, complex &c)
{
if (c.imag<0)
out<<c.real<<c.imag<<”i”;
else
out<<c.real<<”+”<<c.imag<<”i”;
return out;
}
complex complex::operator+(complex c)
{
complex temp;
temp.real = real+c.real;
temp.imag = imag+c.imag;
return temp;
}
complex complex::operator-(complex c)
{
complex temp;
temp.real = real-c.real;
temp.imag = imag-c.imag;
return temp;
}
complex complex::operator*(complex c)
{
complex temp;
float t=c.real;
temp.real = real*c.real-imag*c.imag;
temp.imag = real*c.imag+imag*t;
return temp;
}
complex complex::operator/(complex c)
{
complex temp;
float qt;
float res=(imag*c.real-real*c.imag);
qt = c.real*c.real+c.imag*c.imag;
temp.real = (real*c.real+imag*c.imag)/qt;
temp.imag = res/qt;
return temp;
}
void main()
{
complex c1, c2, c3,c4(4,9),c5(3.23004,4.666304444);
double t;
clrscr();
t=condou(c5);
cout<<”\nEnter complex number 1: “;
cin>>c1;
cout<<”\nEnter complex number 2: “;
cin>>c2;
cout<<”\nEnter complex numbers are:”;
cout<<”\nComplex 1: “<<c1;
cout<<”\nComplex 2: “<<c2;
c3=c1+c2;
cout<<”\nResult of addition is:”<<c3;
c3=c1-c2;
cout<<”\nResult of subtraction is:”<<c3;
c3=c1*c2;
cout<<”\nResult of multiplication is:”<<c3;
c3=c1/c2;
cout<<”\nResult of division is:”<<c3;
cout<<”\nInteger–>complex:”<<c4;
cout<<”\nDouble–>complex:”<<c5;
cout<<”\nConverted to double”<<t;
getch();
}

OUTPUT:
 Enter complex number 1:
Real Part:2
Imag Part:4
Enter complex number 2:
Real Part:3
Imag Part:5
Enter complex numbers are:
Complex 1: 2+4i
Complex 2: 3+5i
Result of addition is:5+9i
Result of subtraction is:-1-1i
Result of multiplication is:-14+22i
Result of division is:0.764706+0.058824i
Integer->complex:4+9i
Double->complex:3.23004+4.666305i
Converted to double7.896345

Result:
Thus the program for operator overloading for complex numbers and their type conversions was executed.

0 comments:

Post a Comment