We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,263 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Help with C++ program

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 & );
public:
    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

private:
    double real; // real part
    double imaginary; // imaginary part

}; //end class Complex


#endif

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

//Constructor
Complex::Complex( double realPart, double imaginaryPart)
    :real( realPart),
    imaginary(imaginaryPart)
{
    //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;
    input.ignore();
    input>>d.imaginary;
    input.ignore();
    return input;
}

Any help would be greatly appreciated.

2
Contributors
1
Reply
4 Hours
Discussion Span
8 Months Ago
Last Updated
2
Views
nyfan68
Light Poster
30 posts since Nov 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

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

np complete
Posting Whiz
385 posts since Sep 2010
Reputation Points: 18
Solved Threads: 36
Skill Endorsements: 0

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.0624 seconds using 2.68MB