Το παρακάτω παράδειγμα ορίζει τον τελεστή + στην κλάση point,
επιτρέποντας την πρόσθεση σημείων:
#include <iostream.h>
class point {
private:
int x, y;
public:
point(int sx, int sy); // Constructor
point operator+(point b); // Overload the + operator
void display(); // Display member function
};
point::point(int sx, int sy)
{
x = sx;
y = sy;
}
void
point::display()
{
cout << "(" << x << "," << y << ")\n";
}
point
point::operator+(point b)
{
point p(x, y); // p is a new point at x, y
p.x += b.x;
p.y += b.y;
return (p);
}
main()
{
point p1 = point(1, 2);
point p2 = point(10, 20);
point p3 = point(0, 0);
p3 = p1 + p2;
p1.display();
p2.display();
p3.display();
}