Consider the class Complex. The class enables operations on complex numbers. Wri
ID: 3903329 • Letter: C
Question
Consider the class Complex. The class enables operations on complex numbers. Write minimal code for class declaration and to enable input and output of complex numbers through the overloaded << and >> operators and to overload the multiplication * and division / operators. Please write C++ code usings MS Visual Studio.
Exercises 53 Fig. 10.14: Complex.h 1 Complex class definition. 3 #include 5 #ifndef COMPLEX-H 6 #define COMPLEX-H 8 class Complex f 9 public: 10 explicit Complex(double = 0.0, double·0.0); // constructor II Complex operator+(const Complex&) const; // addition 12 Complex operator-(const Complex&) const; // subtraction 13 std::string toStringO const; l4 private: i5 double real; // real part 16 17 18 19 #endif double imaginary;// imaginary part Fg. 10.14 | Complex class definition.Explanation / Answer
The Required code part is as follows:
class Complex
{
Complex operator*(const Complex a2) const
{
Complex a;
a.real = (real * a2.real)-(imaginery * a2.imaginery);
a.imaginery = (real *a2.imaginery)+(imaginery *a2.real);
return a;
}
Complex operator/(const Complex a2) const
{
Complex a;
a.real =((real *a2.real )+(imaginery *a2.imaginery ))/((a2.real *a2.real )+(a2.imaginery *a2.imaginery));
a.imaginery =((imaginery *a2.real )-(real*a2.imaginery))/((a2.real * a2.real)+(a2.imaginery*a2.imaginery));
return a;
}
friend ostream & operator << (ostream &out, const Complex &c);
friend istream & operator >> (istream &in, Complex &c);
};
ostream & operator << (ostream &out, const Complex &c){
out << c.real << " " << c.imaginery << endl;
return out.
}
istream & operator << (istream &in, Complex &c){
in << c.real;
in >> c.imaginery;
return in.
}