Need a project done?

C++ Programming Developer

Search This Blog

Complex number Classes C++


Question:
Complex numbers are of the form where x and y are real numbers and i is the imaginary x + iy, unit equal to
√− 1 and i2 =− 1 . Very often a complex number is represented by a single letter, z = x + iy.
The complex conjugate of a complex number z = x + iy is defined to be: z = x − iy.
Operations on Complex Numbers:
Consider, z1 = x1 + iy1 and
z iy . 2 = x2 + 2
Addition: z1 + z2 = (x1 + x2) + i(y1 + y2)
Subtraction: z1 − z2 = (x1 + x2) − i(y1 + y2)
Multiplication: z1. z2 = (x1x2 − y1y2) + i(x1y2 + x2y1)
Division:          
The Complex Class:
Implement the class named Complex. The class should contain two data members, real and imag, both of
which are doubles. It should also have the following member functions:
1. Complex() : the default constructor that sets both real and imag to zero.
2. Complex( double r ) : an overloaded constructor that sets real to r and imag to zero.
3. Complex( double r, double i ) : another overloaded constructor that sets real to r and imag to i.
4. Complex( Complex c ) : the copy constructor
5. Complex add( Complex c ) : this function adds the complex number invoking the function to c (the
parameter to this function), and returns a new complex number that represents their sum.
6. Complex subtract( Complex c ) : this function subtracts the complex number c from the complex
number invoking the function, and returns a new complex number that represents their difference.
7. Complex multiply( Complex c ) : this function multiplies the complex number invoking the function
to c, and returns a new complex number that represents their product.
8. Complex divide( Complex c ) : this function divides the complex number invoking the function to c,
and returns a new complex number that represents the quotient.
9. void print() : the function that prints the complex number object. For eg., if, for the given object, the
value of real is 2.4 and that of imag is 3.7, this function should print 2.4 + 3.7i on the screen.
Apart from these functions, you should also define the getter and setter functions: getReal, getImag, setReal
and setImag.
Test the program by writing the main() function. Inside this function, you should prompt the user to enter real
and imaginary parts of two complex numbers ( and ). Use the operations defined in the z1 z2 class to perform the addition ( z1 + z2 ), subtraction ( z1 − z2 ),multiplication ( z1. z2 ), and division z2.

Solution:
//Complex.h

class Complex{
public:
Complex();
Complex(double r);
Complex(double r, double i);
Complex(Complex &obj);
Complex add(Complex c);
Complex sub(Complex c);
Complex mult(Complex c);//Multiplication: z1. z2 = (x1x2 - y1y2) + i(x1y2 + x2y1)
Complex div(Complex c);
void print();
double getReal() const;
double getImag() const;
void setReal(double r);
void setImag(double i);
private:
double real, imag;
};

void reset(double,double, Complex&, Complex&);

//------------------------------------------------------------------------------------------------------

//Main.cpp:


#include <iostream>
#include "Complex.h"
using namespace std;

int main(){
cout << "Enter Real and imaginary parts of z1 followed by space" << endl;
double r1,i1,r2,i2;
cin >> r1 >> i1;
Complex a(r1,i1);
cout << "Do you want to print it? Y for yes, N for No. "; char asd; cin >> asd;
if (asd == 'Y'){
a.print();
}
cout << "Enter Real and imaginary parts of z2 followed by space" << endl;
cin >> r2 >> i2;
Complex b(r2,i2),c(a.add(b));
cout << "Do you want to print it? Y for yes, N for No. "; cin >> asd;
if (asd == 'Y'){
b.print();
}
cout << "\nSUM: \t\t(" << c.getReal() << ") + (" << c.getImag() << ")i" << endl;

reset(r1,i1,a,c);
c = a.sub(b);
cout << "Difference: \t(" << c.getReal() << ") + (" << c.getImag() << ")i" << endl;

reset(r1,i1,a,c);
c = a.mult(b);
cout << "Product: \t(" << c.getReal() << ") + (" << c.getImag() << ")i" << endl;

reset(r1,i1,a,c);
c = a.div(b);
cout << "Division: \t(" << c.getReal() << ") + (" << c.getImag() << ")i" << endl;
return 0;
}
//------------------------------------------------------------------------------------------------------

//Complex.cpp:

#include <iostream>
#include "Complex.h"
using namespace std;

Complex::Complex(){
real = imag = 0;
}

Complex::Complex(double r){
real = r;
imag = 0;
}

Complex::Complex(double r, double i){
real = r;
imag = i;
}

Complex::Complex(Complex &obj){
real = obj.real;
imag = obj.imag;
}

Complex Complex::add(Complex c){
Complex sum;
sum.real = real + c.real;
sum.imag = imag + c.imag;
return sum;
}
Complex Complex::sub(Complex c){
Complex sub;
sub.real = real - c.real;
sub.imag = imag - c.imag;
return sub;
}
Complex Complex::mult(Complex c){//real = x1, y1 = imag c.real = x2, c.imag = y2
Complex mult;//Multiplication: z1. z2 = (x1x2 - y1y2) + i(x1y2 + x2y1)
mult.real = real*c.real - imag*c.imag;
mult.imag = real*c.imag + c.real*imag;
return mult;
}
Complex Complex::div(Complex c){
Complex div;//a=real,b=imag,c=c.real,d=c.imag
div.real = (real*c.real+imag*c.imag)/(c.real*c.real + c.imag*c.imag);
div.imag = (imag*c.real-real*c.imag)/(c.real*c.real + c.imag*c.imag);
return div;
}

void Complex::print(){
cout << '(' << real << ") + (" << imag << ")i" << endl;
}

void reset(double r1, double i1,Complex&a, Complex&c){
a.setReal(r1);
a.setImag(i1);
c.setReal(0);
c.setImag(0);
}

double Complex::getReal() const{
return real;
}
double Complex::getImag() const{
return imag;
}
void Complex::setReal(double r){
real = r;
}
void Complex::setImag(double i){
imag = i;
}


No comments:

Post a Comment

"Don't let anyone ever make you feel like you don't deserve what you want."