• 12-30-2002
Tonyukuk
Point p1(0,3);
Point p2; //this is the default constructor
cout << p2.getX() << “ “ << p2.getY() << endl;
//result is `0 0`
//result is `X:0 Y:3`;

• 12-30-2002
SilentStrike
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.
• 12-30-2002
MrWizard
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.

Code:

```#ifndef POINT_H #define POINT_H #include <iostream> using namespace std; class Point {   private:     int m_x;     int m_y;   public:     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 */```
EDIT:

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.
• 12-30-2002
ammar
Quote:

Code:

```cout << p1 << endl; //operator overloading of << //result is `X:0 Y:3`; cout << p1 - p2<<endl; //operator overloading, - gives the distance```

`cout << p1;`