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;
}