Operator overloading

I

idikoko

I need help overloafing the <,> operators! I wrote a very simple
complex numbers class which basically treats a double as an imaginary
number. I wrote the code to overload the >,< operators but they don't
seem to compile. I'm stuck as to what to do next, please help


class Complex {
public:
Complex( double = 0.0); // constructor
Complex operator+( const Complex & ) const; // addition
Complex operator-( const Complex & ) const; // subtraction
bool operator>( const Complex & ) const; // Comparision
bool operator<( const Complex & ) const; // Comparision
const Complex &operator=( const Complex & ); // assignment
void print() const; // output
friend ostream &operator<<(ostream&, const Complex &);
friend istream &operator>>(istream&, Complex &);
private:
double imaginary; // imaginary part
};
// Member function definitions for class Complex

// Constructor
Complex::Complex( double i )
: imaginary( i ) { }

// Overloaded addition operator
Complex Complex::eek:perator+( const Complex &operand2 ) const
{
return Complex(imaginary + operand2.imaginary );
}

// Overloaded subtraction operator
Complex Complex::eek:perator-( const Complex &operand2 ) const
{
return Complex( imaginary - operand2.imaginary );
}

// Overloaded
bool Complex::eek:perator>( const Complex &right ) const // Comparision
{
return right < *this;
}

// Overloaded
bool Complex::eek:perator<( const Complex &right ) const // Comparision
{
return right > *this;
}

// Overloaded = operator
const Complex& Complex::eek:perator=( const Complex &right )
{
imaginary = right.imaginary;
return *this; // enables cascading
}

// Display a Complex object in the form: (a, b)
void Complex::print() const
{ cout << imaginary << 'i'; }

ostream &operator<<(ostream &output , const Complex &imagine)
{
output<<imagine.imaginary<<"i";
return output;
}

istream &operator>>(istream &input, Complex &i)
{
input>>i.imaginary;
return input;
}


every portion of this code seems to work fine except the <,> parts!
 
N

n2xssvv g02gfr12930

idikoko said:
I need help overloafing the <,> operators! I wrote a very simple
complex numbers class which basically treats a double as an imaginary
number. I wrote the code to overload the >,< operators but they don't
seem to compile. I'm stuck as to what to do next, please help


class Complex {
public:
Complex( double = 0.0); // constructor
Complex operator+( const Complex & ) const; // addition
Complex operator-( const Complex & ) const; // subtraction
bool operator>( const Complex & ) const; // Comparision
bool operator<( const Complex & ) const; // Comparision
const Complex &operator=( const Complex & ); // assignment
void print() const; // output
friend ostream &operator<<(ostream&, const Complex &);
friend istream &operator>>(istream&, Complex &);
private:
double imaginary; // imaginary part
};
// Member function definitions for class Complex

// Constructor
Complex::Complex( double i )
: imaginary( i ) { }

// Overloaded addition operator
Complex Complex::eek:perator+( const Complex &operand2 ) const
{
return Complex(imaginary + operand2.imaginary );
}

// Overloaded subtraction operator
Complex Complex::eek:perator-( const Complex &operand2 ) const
{
return Complex( imaginary - operand2.imaginary );
}

// Overloaded
bool Complex::eek:perator>( const Complex &right ) const // Comparision
{
return right < *this;
}

// Overloaded
bool Complex::eek:perator<( const Complex &right ) const // Comparision
{
return right > *this;
}

// Overloaded = operator
const Complex& Complex::eek:perator=( const Complex &right )
{
imaginary = right.imaginary;
return *this; // enables cascading
}

// Display a Complex object in the form: (a, b)
void Complex::print() const
{ cout << imaginary << 'i'; }

ostream &operator<<(ostream &output , const Complex &imagine)
{
output<<imagine.imaginary<<"i";
return output;
}

istream &operator>>(istream &input, Complex &i)
{
input>>i.imaginary;
return input;
}


every portion of this code seems to work fine except the <,> parts!
Your < and > operators form a circular reference where they would be
endlessly calling each other. This is not acceptable and your compiler
having recognised this has refused to allow it.
IMHO complex numbers can have both real and imaginary parts. No doubt
you haven't got round to the real part, preferring the imaginary world
to begin with.

JB
 
J

Jonathan Mcdougall

idikoko said:
I need help overloafing the <,> operators! I wrote a very simple
complex numbers class which basically treats a double as an imaginary
number. I wrote the code to overload the >,< operators but they don't
seem to compile. I'm stuck as to what to do next, please help


class Complex {
public:
Complex( double = 0.0); // constructor
const Complex &operator=( const Complex & ); // assignment

operator= normally returns a non-const reference.
Complex operator+( const Complex & ) const; // addition
Complex operator-( const Complex & ) const; // subtraction
bool operator>( const Complex & ) const; // Comparision
bool operator<( const Complex & ) const; // Comparision
friend ostream &operator<<(ostream&, const Complex &);
friend istream &operator>>(istream&, Complex &);

Don't make operators friends or member if you don't need to. operator+,
operator- and both stream operators don't need to be members in this
case, as long as you provide a member function returning the imaginary
part.
void print() const; // output
private:
double imaginary; // imaginary part
};
// Member function definitions for class Complex

// Constructor
Complex::Complex( double i )
: imaginary( i ) { }

// Overloaded addition operator
Complex Complex::eek:perator+( const Complex &operand2 ) const
{
return Complex(imaginary + operand2.imaginary );
}

// Overloaded subtraction operator
Complex Complex::eek:perator-( const Complex &operand2 ) const
{
return Complex( imaginary - operand2.imaginary );
}

// Overloaded
bool Complex::eek:perator>( const Complex &right ) const // Comparision
{
return right < *this;
}

// Overloaded
bool Complex::eek:perator<( const Complex &right ) const // Comparision
{
return right > *this;
}

Are you sure you want these operators to call themselves recursively?
// Overloaded = operator
const Complex& Complex::eek:perator=( const Complex &right )
{
imaginary = right.imaginary;
return *this; // enables cascading
}

// Display a Complex object in the form: (a, b)
void Complex::print() const
{ cout << imaginary << 'i'; }

ostream &operator<<(ostream &output , const Complex &imagine)
{
output<<imagine.imaginary<<"i";
return output;
}

istream &operator>>(istream &input, Complex &i)
{
input>>i.imaginary;
return input;
}


every portion of this code seems to work fine except the <,> parts!

Compiles fine for me. The problem is that operator> calls operator<,
which in turn calls operator>. This is inifite recursion and may have
been caught by your compiler and flagged as an error.


Jonathan
 
M

maadhuu

totally agreed with the infinite recursion part of it. You could implement
one of them as
bool operator<(const Complex& that) const
{
return imaginary < that.imaginary ;
}
and the other one negates this call -
operator
something like
bool operator >(const Complex& that) const
{
!( *this < that ) ;
}

one other point is that operator= return a const reference and this will
not allow cascading .....You just have to return a reference .
Have a nice time !
Maadhuu.
 
J

Jonathan Mcdougall

maadhuu said:
totally agreed with the infinite recursion part of it.

That may be, but please quote the message you are answering to next
time.
You could implement
one of them as
bool operator<(const Complex& that) const
{
return imaginary < that.imaginary ;
}
and the other one negates this call -
operator
something like
bool operator >(const Complex& that) const
{
!( *this < that ) ;

This is wrong not only because it misses a return, but also because it
would actually return true if *this == that. Make it

return imaginary > that.imaginary;


Jonathan
 
C

Chris Theis

maadhuu said:
totally agreed with the infinite recursion part of it. You could implement
one of them as
bool operator<(const Complex& that) const
{
return imaginary < that.imaginary ;
}
and the other one negates this call -
operator
something like
bool operator >(const Complex& that) const
{
!( *this < that ) ;
}

one other point is that operator= return a const reference and this will
not allow cascading .....You just have to return a reference .
Have a nice time !
Maadhuu.

The negation of less (<) is not greater (>) but greater/equal (>=).
Consequently, your idea will not work as you would expect it. Furthermore,
implementing all those operators considering the imaginary part only does
not really make much sense.

Cheers
Chris
 
M

Matteo

As others have mentioned, your comparison operators < and > call each
other recursively.

A bigger problem is that when it comes to complex numbers, there is no
well defined comparison between them. You can certainly define one
(comparing their magnitudes, but that has problems since 2 complex
numbers can be different and yet have the same magnitude. Or use
lexicographical ordering, but that is usually useless) but it is of
limited utility.

Complex numbers just don't have a natural comparison, and unless you
have some very good rationale, I would recommend that you resist the
temptation to overload operators < and >.

-matt

p.s. If you do need a comparison so that you can use compex numbers in
a sorted standard container, that's another thing. Myself, I'd use
lexicographical ordering, but I'd put it in a separate comparison
object, and pass it in as a template parameter to the container, rather
than confusing the issue by putting it in the class. But I'm a bit of
a purist, with a math background too.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,050
Latest member
AngelS122

Latest Threads

Top