In: Computer Science
A complex number is a number with two components, namely, the real part (rel) and the imaginary part (img). For example the complex number (a+bi) has a as the real part and b as the imaginary part. In developing such a class, assume that both of the real and imaginary parts to be floats. The symbol i represent . A declaration for the Complex class is presented next.
class Complex {
public:
Complex (float r = 0.0, float g= 0.0);
Complex (const Complex & c); //The copy
constructor
void print( ) const; // Print out the class data
Complex & negate ( ); // Multiplies the data members of a
complex object by a
// negative one.
Complex addComplex (const Complex & b);
// add two complex numbers, the calling
//object and b and return a new complex //one
Complex subComplex (const Complex &b);
// subtract two complex numbers, the
// calling object and b and return a new
// complex one
Complex multipComplex ( const Complex &c) const; // multiply
two complex
// numbers and return a complex one
Complex assginComplex ( const Complex & c); // assign one
complex number to
// another one and return the result in a complex object.
bool isEqual (const Complex & c) const; //compares one complex
number to
// another and returns a Boolean value
private:
float rel;
float img;
};
Implement and test the class Complex. Write a driver program to
test your Complex class. Instantiate several Complex objects. Test
that all your member functions work properly.
Hint
• Addition of two complex numbers: (a+bi) +(c+di) =
(a+c) + (b+d) i
• Subtraction: (a+bi) (c+di) = (a-c) + (b-d) i
• Multiplication: (a+bi) * (c+di) = (ac-bd) + (ad+bc)
i
A sample run is given below:
Please enter the first complex number C1(r,img) ; 1 -5
Please enter the second complex number C2(r,img) ; 2 11
Testing the copy constructor C3= C2 = 2+11i
Testing the negate function C4 = C3.negate() = -2 -11i
Testing the assign function C5 = C3.assignComplex(C4) = -2 -11i
Testing the isEqual function C4.isEqual(C5) = 1
Testing the Add Complex function C6 = C1.addComplex(C2) = 3+6i
Testing the Sub Complex function C7 = C1.subComplex(C2) = -1 -16i
Testing the multiply Complex function C8 = C1.multipComplex(C2) = 57+1i
#include <iostream>
#include<complex>
using namespace std;
class Complex{
private:
float rel;
float img;
public:
Complex (float r, float g){
r=rel;
g=img;
}
Complex (const Complex &c)
{
a=c.rel;
b=c.img;
}
//Print out class data
void print() {
cout<<rel<<"+"<<img<<"i"<<endl
}
//Multiplies data members of a complex object by negative 1
Complex negate(complex<float> &c1){
c1.real(rel);
c1.imag(img);
}
Complex addComplex(complex<float> &c1,
complex<float> &c2){
cout<<"The sum
of"<<c1<<"and"<<c2<<"complex
numbers="<<c1+c2;
}
Complex subComplex(complex<float> &c1,
complex<float> &c2){
cout<<"The subtraction
of"<<c1<<"and"<<c2<<"complex
numbers="<<c1-c2;
}
Complex multipComplex(complex<float> &c1,
complex<float> &c2){
cout<<"The multiplication
of"<<c1<<"and"<<c2<<"complex
numbers="<<c1*c2;
}
Complex assignComplex(complex<float> &c1){
float new_r,new_i;
cout<<"Enter the new real value of complex number";
cin>>new_r;
cout<<"Enter the imaginary value of complex number";
cin>>new_i;
c1.rel=new_r;
c1.img=new_i;
cout<<"New complex number assignment is";
print();
}
bool isEqual(const Complex &c) const;
};