Saturday, 4 February 2012

1. Write a C++ program to implement a complex class that contains two data members to store the real and imaginary parts of a complex number. Include member functions to initialize the data members to display the complex objects in the form ‘a+ib’. Use constructor and destructor for data members. Create a menu for the operations. Perform the following operations 1. Addition (use operator overloading) 2. Subtraction(use operator overloading) 3. Multiplication (use operator overloading) 4. Division Store the result in third object in the form of complex number. 2.

// Jayachandra
// addition,substraction,division and multiplication of two complex numbers
#include
using namespace std;

class Complex
{
public:

double getVolume()
{
cout<real + b.real;
com.imag = this->imag + b.imag;

return com;
}
// substraction
Complex operator-(const Complex& b)
{
Complex com;
com.real = this->real - b.real;
com.imag = this->imag - b.imag;

return com;
}
// multiplication
Complex operator*(const Complex& b)
{
Complex com;
com.real = this->real * b.real;
com.imag = this->imag * b.imag;

return com;
}
// division
Complex operator/(const Complex& b)
{
Complex com;
com.real = this->real / b.real;
com.imag = this->imag / b.imag;

return com;
}
private:
double real;
double imag;

};
// Main function for the program
int main( )
{
int n;
double i,r;
Complex Complex1;
Complex Complex2;
Complex Complex3;
cout<<"Plase enter real number for Complex1"<>r;
cout<<"Plase enter imaginary number Complex1"<>i;

Complex1.real_image(r,i);
cout<<"Plase enter real number for Complex2"<>r;
cout<<"Plase enter imaginary number Complex2"<>i;
Complex2.real_image(r,i);
Complex3 = Complex1 + Complex2;
for(;;)
{
cout<<"*****************************************\n*\t\t\t\t\t*"<>n;
switch(n)
{
case 1:
Complex3 = Complex1 + Complex2;
Complex3.getVolume();
break;
case 2:
Complex3 = Complex1 - Complex2;
Complex3.getVolume();
break;
case 3:
Complex3 = Complex1 * Complex2;
Complex3.getVolume();
break;
case 4:
Complex3 = Complex1 / Complex2;
Complex3.getVolume();

break;
case 5:
exit(0);
default:
cout<<"please choose correct option"<