Overloaded operators in Complex class

11,956

Solution 1

Istream and Ostream operators need to be declared as friend public members in the Complex class in order for them to access private members of the Complex class.

friend ostream &operator<<(ostream&output, const Complex &complex){//Print as Complex object as (a,b) with overloaded version

    output << '(' << complex.real << "," << complex.imaginary << ')';//it allows usage as cout<<a<<b<<c
    return output;

};
friend istream &operator>>(istream&input, Complex &complex){//Get input from the user.

    input.ignore();//ignore '('
    input >> complex.real;
    input.ignore();//ignore ","
    input >> complex.imaginary;
    input.ignore();//ignore ')'
    return input;//it allows usage as cin>>a>>b>>c
}

On the other hand, you are declaring two types at the same time. The comparisons shall return bool so I remove Complex&:

bool Complex::operator==(const Complex &right) const{

    if ((real == right.real) && (imaginary == right.imaginary))
        return true;
    else
        return false;

}

I see a bug in operator !=, replaced && by || as different complex numbers can have same real or imaginary parts.

bool Complex::operator!=(const Complex &right) const{

    if ((real != right.real) || (imaginary != right.imaginary))
        return true;
    else
        return false;

}

That's all I see, try and tell us...

Solution 2

you can see below all code:

#include <iostream>
using std::cout;
using std::ostream;
using std::istream;
using namespace std;

class Complex
{
    private:
        friend ostream &operator<<(ostream&, const Complex&);
        friend istream &operator>>(istream&, Complex&);
        double real;
        double imaginary;
    public:
        Complex(double = 0.0, double = 0.0);
        Complex operator+(const Complex&) const;
        Complex operator-(const Complex&) const;
        Complex operator*(const Complex&) const;
        const Complex &operator=(const Complex &);
        const bool operator==(const Complex&) const;
        const bool  operator!=(const Complex&) const;
};

Complex::Complex(double r, double i)
{
    this->real = r;
    this->imaginary = i;
}

Complex Complex::operator+(const Complex& operando2) const
{
    return Complex(real + operando2.real,imaginary + operando2.imaginary);
}

Complex Complex::operator-(const Complex& operando2) const
{
    return Complex(real - operando2.real,imaginary - operando2.imaginary);
}

const Complex& Complex::operator=(const Complex &right)
{
    real = right.real;
    imaginary = right.imaginary;
    return *this;
}

Complex Complex::operator*(const Complex &operando2) const
{
    return ((real * operando2.real) - (real * operando2.imaginary), (real * operando2.imaginary) - (imaginary*operando2.real));
}

const bool  Complex::operator==(const Complex &right) const
{
    if((real == right.real) && (imaginary == right.imaginary))
        return true;
    else
        return false;
}

const bool Complex::operator!=(const Complex &right) const
{
    if((real != right.real) || (imaginary != right.imaginary))
        return true;
    else
        return false;
}

ostream &operator<<(ostream&output, const Complex &complex) 
{
    output << '(' << complex.real << "," << complex.imaginary << ')';
    return output;
}

istream &operator>>(istream&input, Complex &complex)
{
    input.ignore();
    input >> complex.real;
    input.ignore();
    input >> complex.imaginary;
    input.ignore();
    return input;
}

main(void)
{
    Complex c1(4,5);
    Complex c2(8,9);
    Complex c3 = c1 + c2;
    Complex c4 = c2 - c3;
    Complex c5 = c1 * c3;
    Complex c6(4,5);
    bool varBool = c6 == c1;
    varBool = c6 != c2;

    c1 = c2 = c3;

    cout << c3 << endl;

    cin >> c3;
    cout << c3 << endl;
}
Share:
11,956
user2970691
Author by

user2970691

Updated on June 04, 2022

Comments

  • user2970691
    user2970691 almost 2 years

    I implement complex class with overloaded operators. It gave me some errors after I compiled, I don't get how I solve them.The errors are generally about that "complex.cpp" does not recognize the Complex type, even there is no error on "complex.h".

    complex.h

        /*Definition of Complex Class. This class contains overloading operators.*/
    
        #ifndef COMPLEX_H
        #define COMPLEX_H
    
        using std::ostream;
        using std::istream;
    
        class Complex{
    
            friend ostream &operator<<(ostream&, const Complex &);
            friend istream &operator>>(istream&, Complex &);
    
        public: 
            Complex(double = 0.0, double = 0.0);//constructor
            Complex operator+(const Complex &) const;//addition
            Complex operator-(const Complex &) const;//subtraction
            Complex operator*(const Complex &) const;//multiplication
            const Complex &operator=(const Complex &);//assignment
            bool const &operator==(const Complex &) const;//equivalent
            bool const &operator!=(const Complex &) const;//not equivalent
        private:
            double real;//real part
            double imaginary;//imaginary part
        }; 
    
        #endif
    

    complex.cpp:

     //Definition of Member Functions of the Complex Class
    
        #include <iostream>
    
        using std::cout;
        using std::ostream;
        using std::istream;
    
        #include "complex.h"
    
        //Constructor
        Complex::Complex(double r, double i)
        :real(r), imaginary(i){};
    
        //Overloaded addition operator
        Complex Complex::operator+(const Complex &operand2) const
        {
            return Complex(real + operand2.real, imaginary +operand2.imaginary);
    
        };
        //Overloaded subtraction operator
        Complex Complex::operator-(const Complex &operand2) const
        {
            return Complex(real - operand2.real, imaginary - operand2.imaginary);
    
        };
        //Overloaded assignment operator
        const Complex& Complex::operator=(const Complex &right) 
        {
            real = right.real;
            imaginary = right.imaginary;
            return *this;
        };
        //Overloaded multiplication operator
        Complex Complex::operator*(const Complex &operand2) const{
    
            return((real *operand2.real)-(real*operand2.imaginary), (real*operand2.imaginary)-(imaginary*operand2.real));
        }
    
        bool Complex& Complex::operator==(const Complex &right) const{
    
            if ((real == right.real) && (imaginary == right.imaginary))
                return true;
            else
                return false;
    
        }
        bool Complex& Complex::operator!=(const Complex &right) const{
    
            if ((real != right.real) && (imaginary != right.imaginary))
                return true;
            else
                return false;
    
        }
        ostream &operator<<(ostream&output, const Complex &complex){//Print as Complex object as (a,b) with overloaded version
    
            output << '(' << complex.real << "," << complex.imaginary << ')';//it allows usage as cout<<a<<b<<c
            return output;
    
        };
        istream &operator>>(istream&input, Complex &complex){//Get input from the user.
    
            input.ignore();//ignore '('
            input >> complex.real;
            input.ignore();//ignore ","
            input >> complex.imaginary;
            input.ignore();//ignore ')'
            return input;//it allows usage as cin>>a>>b>>c
        }
    

    Error Lists:

    1- syntax error : identifier 'Complex' 62 1

    2- error C2065:'complex' : undeclared identifier 58 1

    3- error C2065: 'complex' : undeclared identifier 65 1

    4- error C2065: 'complex' : undeclared identifier 67 1

    5- error C2086: 'bool Complex' : redefinition 48 1

    6- error C2143: syntax error : missing ',' before '&' 56 1

    7- error C2143: syntax error : missing ';' before '&' 40 1 8- error C2143: syntax error : missing ';' before '&' 48 1

    9- error C2228: left of '.imaginary' must have class/struct/union 58 1

    10- error C2228: left of '.imaginary' must have class/struct/union 67 1

    11- error C2228: left of '.real' must have class/struct/union 58 1

    12- error C2228: left of '.real' must have class/struct/union 65 1

    13- error C2373: 'Complex::operator !=' :redefinition; different type modifiers 48 1

    14- error C2373: 'Complex::operator ==' : redefinition; different type modifiers 40 1

    15- error C2556: 'int &Complex::operator !=(const Complex &) const' : overloaded function differs only by return type from 'const bool &Complex::operator !=(const Complex &) const' 48 1

    16- error C2556: 'int &Complex::operator ==(const Complex &) const' : overloaded function differs only by return type from 'const bool &Complex::operator ==(const Complex &) const' 40 1

    17- error C2805: binary 'operator >>' has too few parameters 62 1

    18- error C4430: missing type specifier -int assumed. Note: C++ does not support default-int 40 1

    19-error C4430: missing type specifier - int assumed. Note: C++ does not support default-int 48 1

    20-error C4430: missing type specifier - int assumed. Note: C++ does not support default-int 56 1

  • user2970691
    user2970691 about 10 years
    Thank you so much. Removing complex& has solved all errors suddenly, and also thanks for the bug warning.