Operator overloading

Discussion in 'C++' started by idikoko, Dec 3, 2005.

  1. idikoko

    idikoko Guest

    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!
     
    idikoko, Dec 3, 2005
    #1
    1. Advertising

  2. idikoko wrote:
    > 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
     
    n2xssvv g02gfr12930, Dec 3, 2005
    #2
    1. Advertising

  3. idikoko wrote:
    > 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
     
    Jonathan Mcdougall, Dec 3, 2005
    #3
  4. idikoko

    maadhuu Guest

    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.
     
    maadhuu, Dec 3, 2005
    #4
  5. maadhuu wrote:
    > 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
     
    Jonathan Mcdougall, Dec 3, 2005
    #5
  6. idikoko

    Chris Theis Guest

    "maadhuu" <> wrote in message
    news:...
    > 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
     
    Chris Theis, Dec 3, 2005
    #6
  7. idikoko

    Matteo Guest

    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.
     
    Matteo, Dec 5, 2005
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John Smith
    Replies:
    2
    Views:
    425
    Ivan Vecerina
    Oct 6, 2004
  2. Replies:
    11
    Views:
    737
    James Kanze
    May 16, 2007
  3. hurcan solter
    Replies:
    3
    Views:
    733
    Cholo Lennon
    Aug 29, 2007
  4. Replies:
    11
    Views:
    562
  5. Replies:
    2
    Views:
    312
Loading...

Share This Page