Click here to Skip to main content
15,881,380 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I am new on this site, i have some issues about OOP in C++ operator overloading.
I try to do add a two numbers using overloading operator "+" in more ways :

1). First way is : const Point operator+(const Point& point) const.
So i add two numbers in this way using an reference to Point class like parameter :
C++
const Point Point::operator+(const Point& point) const
     {
          return Point(this->x + point.x, this->y + point.y);
     }

This variant works.

2). Second way is : const Point operator+(const int Value) const.
I add two numbers with a constant integer value like parameter :
C++
const Point Point::operator+(const int Value) const
     {
          return Point(this->x + Value, this->y + Value);
     }

This variant works, also.

3). The third way : const Point::operator+(const Point& point1, const Point& point2).

but this code is not ok. I receive an error :

error: 'const Point Point::operator+(const Point&, const Point&)' must take either zero or one argument.

C++
#include <iostream>

using namespace std;

class Point
{
private:
    int x;
    int y;
public:
    Point(int x = 0, int y = 0);
    int getX() const;
    int getY() const;
    void setX(int X);
    void setY(int y);
    void print() const;
    const Point operator+(const Point& point1, const Point& point2);
};

Point::Point(int a, int b)
{
    x = a;
    y = b;
}

int Point::getX() const
{
    return x;
}

int Point::getY() const
{
    return y;
}

void Point::setX(int X)
{
   this->x = X;
}

void Point::setY(int Y)
{
    this->y = Y;
}

void Point::print() const
{
    cout << "\n x = " << x << "\n\n y = " << y <<endl<<endl;
}

const Point::operator+(const Point& point1, const Point& point2)
{
    return Point(point1.x + point2.x, point2.y + point2.y);
}

int main()
{
    Point p1(2, 8), p2(4, 5);
    Point p3 = p1 + p2;
    p1.print();
    p2.print();
    p3.print();

    Point p5 = p1 + p2 + p3;
    p5.print();
    return 0;
}
Posted

http://www.learncpp.com/cpp-tutorial/92-overloading-the-arithmetic-operators/[^]

"When the operator does not modify its operands, the best way to overload the operator is via friend function."

Thus
C++
friend const Point operator+(const Point& point1, const Point& point2);

and


C++
const Point operator+(const Point& point1, const Point& point2)
{
    return Point(point1.x + point2.x, point1.y + point2.y);
}
 
Share this answer
 
v3
Thank you for your interest.

I have one more question.
In main() function :
C++
Point p1(6, 3), p2(2, 6);
Point p3 = p1 + p2;
p1.print();
p2.print();
p3.print();

Point p5 = p1 + p2 + p3;
p5.print();

when i compile i obtain the next result :
C++
x = 6
y = 3

x = 2
y = 6

x = 8
y = 12

x = 16
y = 24


In the third group of x,y the result should be : x = 8 and y = 9 and of course the next group of x,y should be x = 16, y = 18.

I don't understand why this ?
 
Share this answer
 
Comments
[no name] 22-Mar-15 20:29pm    
1. You are new but this should be added as a comment.
2. I already fixed your bug in the code that I posted - check carefully the implementation.
3. Learn to use your debugger.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900