2.Recall that a complex number is defined as a number of the form z=a+bi where a
ID: 3680313 • Letter: 2
Question
2.Recall that a complex number is defined as a number of the form
z=a+bi
where a is called the real part of z,b is called the imaginary part of z and i=sqrt(-1) is called the imaginary unit.write a class called complexnumber
that consits of at least two instance fields real and imag which are of type double and represent the real and imaginary parts of a complex number. your
class should also contain the following methods:
1.A parameterless constructor that creats the complex number 0 ( with real and imaginary parts both being zero).
2.A constructor that takes two arguments : a real part and an imaginary part. the constructor should create a new complex number whose real and imaginary
parts are that which are specified in the arguments.
3.A non-static method called add which accepts a complex number reference and returns the sum of the calling object and the object referenced by the
parameter.the sum should be returned as new complexNumber object.Hint( sum z1=a+biand z2=c+di).
z1+z2=(a+c)+(b+d)i
4.A static method called mulZ which returns the preduct of two complex number objects passed as two arguments to the method.The product should be returned
as new ComplexNumber object .
Hint (the product of two numbers z1=a+bi and z2=c+di is:
z1*z2 = (ac+bd)+(ad+bc)i
Explanation / Answer
/*
* C++ Program to Implement Complex Numbers using Classes
*/
#include <iostream>
using namespace std;
class Complex
{
private:
int real;
int imag;
public:
Complex(){
real = 0;
imag = 0;
};
Complex(int r , int i){
real = r;
imag = i;
};
void setComplex(void)
{
cout << "Enter the real and imaginary parts : ";
cin >> this->real;
cin >> this->imag;
}
Complex add(const Complex& c)
{
Complex comp;
comp.real = this->real + c.real;
comp.imag = this->imag + c.imag;
return comp;
}
Complex subtract(const Complex& c)
{
Complex comp;
comp.real = this->real - c.real;
comp.imag = this->imag - c.imag;
return comp;
}
void printComplex(void)
{
cout << "Real : " << this->real << endl
<< "Imaginary : " << this->imag << endl;
}
Complex mulZ(const Complex& c)
{
Complex comp;
comp.real = this->real * c.real- (this->imag*c.imag);
comp.imag = this->imag*c.real + c.imag*this->real;
return comp;
}
};
int main()
{
Complex a, b, c, d;
cout << "Setting first complex number " << endl;
a.setComplex();
cout << "Setting second complex number " << endl;
b.setComplex();
/* Adding two complex numbers */
cout << "Addition of a and b : " << endl;
c = a.add(b);
c.printComplex();
/* Multipy two complex numbers */
cout << "Multipication of a and b : " << endl;
d = a.mulZ(b);
d.printComplex();
}
/*
Output:
Setting first complex number
Enter the real and imaginary parts : 1
2
Setting second complex number
Enter the real and imaginary parts : 2
-1
Addition of a and b :
Real : 3
Imaginary : 1
Multipication of a and b :
Real : 4
Imaginary : 3
*/
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.