Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The following is the C++ codes for complex number for int data type. i want to r

ID: 3535159 • Letter: T

Question

The following is the C++ codes for complex number for int data type.


i want to revise the codes to create template for complex class to make the main program work. How would i do that please? Thanks alot! expect full ratings from me!

class Complex {

public:

Complex<T>( T = 0 , T = 0 ); // constructor

Complex<T> operator+( const Complex<T>& ) const; // addition

void print();

private:

int real; // real part

int imaginary; // imaginary part

};

// Constructor

Complex::Complex(int r, int i )

{

real = r;

imaginary = i;

}

// Overloaded addition operator

Complex Complex::operator+( const Complex &operand2 ) const

{

Complex sum;

sum.real = real + operand2.real;

sum.imaginary = imaginary + operand2.imaginary;

return sum;

}

void Complex::print()

{

cout << real << " + " << imaginary << 'i' << endl;

}

//Use the following main function to test your program

int main()

{

Complex<double> x, y( 4.3, 8.2 ), z( 3.3, 1.1 );

x = y + z;

x.print();

Complex<int> a, b( 4, 8 ), c( 3, 2 );

a = b + c;

a.print();

return 0;

}

Explanation / Answer

template class Complex { public: T real_; T img_; Complex(T real, T img) : real_(real), img_(img) { } }; template T add(T num) { return num + num; } template Complex add(Complex num) { T temp_real = num.real_+ num.real_ ; T temp_img = num.img_ + num.real_; return Complex(temp_real, temp_img); }template T sub(T num) { return num - num; } template Complex add(Complex num) { T temp_real = num.real_- num.real_ ; T temp_img = num.img_ - num.real_; return Complex(temp_real, temp_img); } }