previous | start

Declaring operator+ as a Friend

class Pair
{
  int x, y;
public:
  Pair();
  Pair(int xval, int yval);

  int getX();
  int getY();
  string toString();
  Pair add(const Pair& other);
  Pair add(int d);

  Pair operator+(const Pair& other);
  Pair operator+(int d);
friend Pair operator+(int d, const Pair& p);
  
};

In the Pair.cpp file, the friend qualifier is not repeated.

Pair Pair::operator+(const Pair& other)
{
  int xval = x + other.x;
  int yval = y + othery.y;

  return Pair(xval, yval);
}

// Don't repeat friend in Pair.cpp
Pair operator+(int d, const Pair& other)
{
  int xval = d + other.x;
  int yval = d + other.y;

  return Pair(xval, yval);
}



previous | start