Answer to Question #284305 in C++ for mustafa

Question #284305

Implement the class ComplexNumber, the real and imaginary parts of the complex number can

be int, float or double. Overload operators +,-,*,~

Generalize this class into template, and write appropriate main function to test it.

#include

usingnamespace std;

template

class ComplexNumber

{

public:

void print() const;

/* prints the complex number */

ComplexNumber();

/* constructor */

ComplexNumber(T, T);

/* constructor with parameters */

ComplexNumber operator + (ComplexNumber); /*overload + */

ComplexNumber operator - (ComplexNumber); /*overload - */

ComplexNumber operator * (ComplexNumber); /*overload * */

void operator ~ (); /*overload ~, takes complex conjugate of calling

complexnumber, i.e. a+iba-ib*/

private:

T a; /*real part of complex numeber*/

T b; /* imaginary part of complex numeber */

};


1
Expert's answer
2022-01-03T04:27:57-0500
#include <iostream>
using namespace std;


template <typename T>
class ComplexNumber
{
public:
    void print() const;
    /* prints the complex number */
    ComplexNumber();
    /* constructor */
    ComplexNumber(T, T);
    /* constructor with parameters */


    ComplexNumber operator + (ComplexNumber); /*overload + */
    ComplexNumber operator - (ComplexNumber); /*overload - */
    ComplexNumber operator * (ComplexNumber); /*overload * */
    void operator ~ (); /*overload ~, takes complex conjugate of calling
                          complexnumber, i.e. a+ib -> a-ib*/


private:
    T a; /*real part of complex numeber*/
    T b; /* imaginary part of complex numeber */
};


template <typename T>
void ComplexNumber<T>::print() const
{
    cout << "(" << a;
    if (b >= 0) {
        cout << " + " << b;
    }
    else {
        cout << " - " << -b;
    }
    cout << "i)";
}


template <typename T>
ComplexNumber<T>::ComplexNumber() : a(T()), b(T())
{}


template <typename T>
ComplexNumber<T>::ComplexNumber(T x, T y) : a(x), b(y)
{}
    
template <typename T>
ComplexNumber<T> ComplexNumber<T>::operator+(ComplexNumber<T> z)
{
    return ComplexNumber(a+z.a, b+z.b);
}



template <typename T>
ComplexNumber<T> ComplexNumber<T>::operator-(ComplexNumber z)
{
    return ComplexNumber(a-z.a, b-z.b);
}


template <typename T>
ComplexNumber<T> ComplexNumber<T>::operator*(ComplexNumber z)
{
    return ComplexNumber(a*z.a - b*z.b, a*z.b + b*z.a);
}


template <typename T>
void ComplexNumber<T>::operator~()
{
    b = -b;
}


int main()
{
    ComplexNumber<int> x(1,2), y(3, 4), z;


    z = x + y;
    x.print();
    cout << " + ";
    y.print();
    cout << " = ";
    z.print();
    cout << endl;


    z = x - y;
    x.print();
    cout << " - ";
    y.print();
    cout << " = ";
    z.print();
    cout << endl;


    z = x * y;
    x.print();
    cout << " * ";
    y.print();
    cout << " = ";
    z.print();
    cout << endl;


    cout << "~";
    x.print();
    cout << " = ";
    ~x;
    x.print();
    cout << endl;
    cout << endl;


    ComplexNumber<double> xd(1.1,2.2), yd(3.3, 4.4), zd;


    zd = xd + yd;
    xd.print();
    cout << " + ";
    yd.print();
    cout << " = ";
    zd.print();
    cout << endl;


    zd = xd - yd;
    xd.print();
    cout << " - ";
    yd.print();
    cout << " = ";
    zd.print();
    cout << endl;


    zd = xd * yd;
    xd.print();
    cout << " * ";
    yd.print();
    cout << " = ";
    zd.print();
    cout << endl;


    cout << "~";
    xd.print();
    cout << " = ";
    ~xd;
    xd.print();
    cout << endl;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog