Design a C class to represent complex numbers A complex numb
Design a C++ class to represent complex numbers. A complex number contains to separate parts: real and imaginary. For instance, “5+4i” is a complex number, where 5 is the real part and 4 is the imaginary part.
Your class will have 2 double attributes for 2 parts of a complex number: real and imaginary. You will also implement following member functions for the class:
Getter and Setter Functions for 2 attributes
2 constructor functions:
one without parameters
one with parameters
C3 = C1 + C2
Overload operator + for this operation
C3 = C1 - C2
Overload operator – for this operation
C3 = C1 * C2
Overload operator * for this operation
C3 = C1 / C2
Overload operator / for this operation
C1 == C2
Overload operator == for this operation
| Getter and Setter Functions for 2 attributes | |
| 2 constructor functions: one without parameters one with parameters | |
| C3 = C1 + C2 | Overload operator + for this operation |
| C3 = C1 - C2 | Overload operator – for this operation |
| C3 = C1 * C2 | Overload operator * for this operation |
| C3 = C1 / C2 | Overload operator / for this operation |
| C1 == C2 | Overload operator == for this operation |
Solution
#include <iostream>
using namespace std;
class Complex
{
private:
int real;
int imag;
public:
Complex() //default constrctor
{
}
Complex(int r,int i) //parameterized constructor
{
real = r;
imag = i;
}
void setReal(int r)
{
real = r;
}
int getReal()
{
return real;
}
void setImag(int i)
{
imag = i;
}
int getImag()
{
return imag;
}
Complex operator +(const Complex& c1) //overloaded operator function for +
{
Complex comp;
comp.real = this->real + c1.real;
comp.imag = this->imag + c1.imag;
return comp;
}
Complex operator -(const Complex& c1) //overloaded operator function for -
{
Complex comp;
comp.real = this->real - c1.real;
comp.imag = this->imag - c1.imag;
return comp;
}
Complex operator *(const Complex& c1) //overloaded operator function for *
{
Complex comp;
comp.real = this->real * c1.real;
comp.imag = this->imag * c1.imag;
return comp;
}
Complex operator /(const Complex& c1) //overloaded operator function for /
{
Complex comp;
comp.real = this->real / c1.real;
comp.imag = this->imag / c1.imag;
return comp;
}
bool operator ==(const Complex& c1) //overloaded operator function for == returns boolean value
{
if(this->real == c1.real && this->imag == c1.imag)
return true;
else
return false;
}
void displayComplex(void)
{
cout << this->real <<\" + \"<< this->imag <<\"i\"<< endl;
}
};
int main()
{
Complex a, b, c, d;
cout << \"Setting first complex number \" << endl;
a.setReal(4);
a.setImag(5);
a.displayComplex();
cout << \"Setting second complex number \" << endl;
b.setReal(2);
b.setImag(6);
b.displayComplex();
/* Adding two complex numbers */
cout << \"Addition of a and b : \" << endl;
c = a + b;
c.displayComplex();
/* Subtracting two complex numbers */
cout << \"Subtraction of a and b : \" << endl;
d = a - b;
d.displayComplex();
/* Multiplying two complex numbers */
cout << \"Multiplication of a and b : \" << endl;
c = a * b;
c.displayComplex();
/* Dividing two complex numbers */
cout << \"Division of a and b : \" << endl;
d = a / b;
d.displayComplex();
if( a == b)
cout<<\"Complex numbers a and b are equal\";
else
cout<<\"Complex numbers a and b are not equal\";
}
output:


