Thread: what is operator obverloading?

  1. #1
    Registered User
    Join Date
    Oct 2002

    what is operator obverloading?

    Point p1(0,3);
    Point p2; //this is the default constructor
    cout << p2.getX() << << p2.getY() << endl;
    //result is `0 0`
    cout << p1 << endl; //operator overloading of <<
    //result is `X:0 Y:3`;
    cout << p1 - p2<<endl; //operator overloading, - gives the distance

    how can I use operator overloading in this situation?

  2. #2
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    What do you mean by 'how can I use operator overloading?' The question is unclear.

    What will the function declarations look like? How would you implement them?

    A point minus a point should be a point (er.. well, vector, but ah, close enough right? ). A distance member function could be useful for finding the distance a point is from the origin, however.
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  3. #3
    Registered User
    Join Date
    Apr 2002
    Well basically operator overloading is defining your own operators for use with your class. Lets say you have 2 integers, and you use the minus sign to subtract them. That's intuitive. What does it mean to subtract some class from another one? It might not be as clear. So basically you define your own minus operator to perform whatever calculation you desire. The same theory applies to the ostream operator as well ( << ). I'll give you an example of this.

    Here is the header file for a simple point class with overloaded operators as you mentioned.

    #ifndef POINT_H
    #define POINT_H
    #include <iostream>
    using namespace std;
    class Point
        int m_x;
        int m_y;
        Point( ) : m_x(0), m_y(0) { }
        Point( int x, int y ) : m_x(x), m_y(y) { }
        friend ostream& operator <<( ostream &os, const Point &pt )
          os << "( " << pt.m_x << ", " << pt.m_y << " )";
          return os;
        Point operator -( const Point &pt )
          return( Point( m_x - pt.m_x, m_y - pt.m_y ) );
    #endif /* POINT_H */

    Mathematically the difference between two points is a vector though. I simply return a Point for simplicity and to show a basic usage of operator overloading.

  4. #4
    Just a Member ammar's Avatar
    Join Date
    Jun 2002
    cout << p1 << endl; //operator overloading of <<
    //result is `X:0 Y:3`;
    cout << p1 - p2<<endl; //operator overloading, - gives the distance
    Without opreator overloading, you cann't output your class like this:
    cout << p1;
    this will give an error, so you can use operator overloading to overload the << operator, to take an object of class point as argument, exactly like MrWizard's code.

    In the next case you cann't write p1 - p2, because the - operator is not defined for objects of type point...
    So in normal cases you would creat a function distance(), that is a member function or a friend, that takes point objects as arguments...
    But using operator overloading you are creating a function, but this function enables you to write p1 - p2, and that's operator overlaoding...

    Hope it's clear now...

Popular pages Recent additions subscribe to a feed
Website Security Test