Ali
Ali

Reputation: 10463

Operator overloading+ add two objects

I'm trying to add two object that they are in the same class.

In the private section of the class I have two int variables

class One {

private:
int num1, num2;
public:

    One operator+=(const One&); // - a member operator that adds another One object - to the current object and returns a copy of the current object
    friend bool operator==(const One&, const One&); // - a friend operator that compares two One class objects for equality
};

 One operator+(const One&, const One&);// - a non-friend helper operator that adds One objects without changing their values and returns a copy of the resulting One 

I'm not sure I have a problem on the opeartor+ I guess

One operator+(const One &a, const One &b){

One c,d,r;

c = a;
d = b;

r += b;
r += a;

return r;
}

I think the above code is wrong, but I tried to use like b.num1 and I get compile error

error: 'int One::num1' is private error: within this context

and I can't use b->num1 as well because the above function is not in the member function section.

error: base operand of '->' has non-pointer type 'const One'

This is how it calls in main

Result = LeftObject + RightObject;

Upvotes: 0

Views: 10126

Answers (2)

Robᵩ
Robᵩ

Reputation: 168866

If you have already implemented this member function:

One One::operator+=(const One&);

Then you may implement the non-member addition operator thus:

One operator+(const One& lhs, const One& rhs) {
  One result = lhs;
  result += rhs;
  return result;
}

This can be simplified somewhat into the following:

One operator+(One lhs, const One& rhs) {
  return lhs += rhs;
}

This pattern (which you can adapt for all operator/operator-assignment pairs) declares the operator-assignment version as a member -- it can access the private members. It declares the operator version as a non-friend non-member -- this allows type promotion on either side of the operator.

Aside: The += method should return a reference to *this, not a copy. So its declaration should be: One& operator+(const One&).


EDIT: A working sample program follows.

#include <iostream>
class One {
private:
  int num1, num2;

public:
  One(int num1, int num2) : num1(num1), num2(num2) {}
  One& operator += (const One&);
  friend bool operator==(const One&, const One&);
  friend std::ostream& operator<<(std::ostream&, const One&);
};

std::ostream&
operator<<(std::ostream& os, const One& rhs) {
  return os << "(" << rhs.num1 << "@" << rhs.num2 << ")";
}

One& One::operator+=(const One& rhs) {
  num1 += rhs.num1;
  num2 += rhs.num2;
  return *this;
}

One operator+(One lhs, const One &rhs)
{
  return lhs+=rhs;
}

int main () {
  One x(1,2), z(3,4);
  std::cout << x << " + " << z << " => " << (x+z) << "\n";
}

Upvotes: 6

Zang MingJie
Zang MingJie

Reputation: 5275

I can't see why the operator+ is wrong:

#include <stdio.h>

class One {
    public:
        One(int n1, int n2): num1(n1),num2(n2) {}

    private:
        int num1, num2;
    public:
        One operator+=(const One& o) {
            num1 += o.num1;
            num2 += o.num2;
            return *this;
        }
        friend bool operator==(const One&, const One&); // - a friend operator that compares two One class objects for equality
        void print() {
            printf("%d,%d\n", num1, num2);
        }
};

One operator+(const One& a, const One& b) {
    One r(0,0);
    r += b;
    r += a;
    return r;
}

int main() {
    One a(1,2),b(3,4);
    One r = a + b;
    r.print();
}

Upvotes: 1

Related Questions