I was wondering if anyone could assist me with an assignment. The problem deals with operator overloading. In this assignment I have to modify a class called "Complex" to

a)enable input and output of complex numbers via overloaded>> and << operators
b)overload the multiplication operator to enable multiplication of two complex numbers as in algebra.
c)overload the == and != operators to allow comparisons of complex numbers.

I seem to have everything right except in my << overloaded function and my >> overloaded function. The problem is that even though I declared the function as "friend" in the Header file, the compiler is giving me red lines under the private data. Saying that "real" and "imaginary" are inaccessible in the source code.

Here is my code below:

#ifndef COMPLEX_H
#define COMPLEX_H
#include <iostream>
#include <string>

class Complex
    friend ostream &operator<<( ostream &, const Complex &);
    friend istream &operator>>( istream &, Complex & );
    Complex( double = 0.0, double = 0.0); //Constructor
    Complex operator+( const Complex & ) const; //addition
    Complex operator-( const Complex & ) const; //subraction
    Complex operator*( const Complex & ) const; //multiplication
    bool operator==(const Complex & ) const; // equal
    bool operator!=(const Complex & ) const; // not equal

    double real; // real part
    double imaginary; // imaginary part

}; //end class Complex


#include <iostream>
#include <string>
#include <iomanip>
#include "Complex.h"
using namespace std;

Complex::Complex( double realPart, double imaginaryPart)
    :real( realPart),
    //empty body
}   //end Complex constructor

//addition operator
Complex Complex ::operator+( const Complex &operand2) const
{ return Complex ( real + operand2.real,
           imaginary + operand2.imaginary );
}//end function operator+

//subtraction operator
Complex Complex::operator-( const Complex & operand2 ) const
    return Complex( real - operand2.real,
         imaginary - operand2.imaginary );
} //end function operator-

//multiplication operator
Complex Complex::operator*( const Complex & operand2) const
    return Complex(real * operand2.real,
        imaginary * operand2.imaginary );
} //end function operator*

//equals operator
bool Complex::operator==( const Complex &operand2) const
    bool r = false;
    if (real == operand2.real && imaginary == operand2.imaginary)
        r = true;

    return r;

//not equals operator
bool Complex::operator!=( const Complex &operand2) const
    return !(*this == operand2);

//overloaded output operator
ostream &operator<<( ostream &output, const Complex &operand2)
    output << "("<< operand2.real << ", " << operand2.imaginary << ")";
    return output;
}//end function operator<<

//overloaded input operator
istream &operator>>( istream &input, Complex &d)
    input.ignore ();
    input >>d.real;
    return input;

Any help would be greatly appreciated.

Recommended Answers

Your << and >> overloaded functions are friend functions and also in private. Make them public and see if its working or not.

Jump to Post

All 2 Replies

Your << and >> overloaded functions are friend functions and also in private. Make them public and see if its working or not.

Create a complex class
Should take input in x+yi form.
 Your class should be able to manipulate the complex numbers in both the formats i.e. complex and polar.
 Your class should be able to perform addition(+),subtraction(-),multiplication(*),division(/) and conjugation(~) of complex numbers.
 Your class should be able to assign (=) and compare (==,>>,<<) complex numbers.
 Also include suitable input and output functions.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.