0

Example:

class Person
{
private:
     int age;
public:
     int getAge()
     {
         return age;
     }
};
class Employee: public Person
{
private:
    int empNum;
};

Now say I want to overload the + operator to add the ages of two employee objects. One of those employee objects calls the overloaded operator function, but in this instance, I also need that object to call the getAge() function from the base class. I know I could just make age protected instead of private, but is there a way to do this leaving that property private?

Jerold Davis
  • 21
  • 1
  • 2
  • `getAge()` is already public so there's no problem. – laurent Jun 28 '12 at 06:10
  • 2
    What's the problem with `int operator+(Person& a, Person& b) { return a.getAge() + b.getAge(); }`? – Philip Kendall Jun 28 '12 at 06:10
  • 2
    @PhilipKendall I wouldn't recommend such an overloading. Is it really 'obvious' that person + person equals a person with the sum of their ages? Why so? Why not the weight or anything else :-) Operator overloading should be used very judiciously. `a.getAge() + b.getAge()`, or `sumAge(a,b)` .. I'd stick with one of those. – emesx Jun 28 '12 at 06:14
  • @elmes To some extent, I agree with you, but this is largely a matter of style and the original poster did explicitly ask for an overloading of `operator+` to add the ages. – Philip Kendall Jun 28 '12 at 06:29
  • I used the people's ages, because I thought it was easier to look at than what I'm actually doing. It was for simplification. – Jerold Davis Jun 28 '12 at 06:49

4 Answers4

4

You should be able to write your operator like this:

int operator+(const Employee& arg1, const Employee& arg2) {

  int result = arg1.getAge() + arg2.getAge();
  return result;

}
juanchopanza
  • 223,364
  • 34
  • 402
  • 480
Tommy Hui
  • 1,306
  • 6
  • 9
  • +1 - although I'd argue the operator should take `Person` objects rather than `Employee`s, even if that's not what the original poster asked for. – Philip Kendall Jun 28 '12 at 06:13
  • Do you not have to use a class to overload an operator? I was under the impression that it had to be done in the class. – Jerold Davis Jun 28 '12 at 06:47
  • @Jerold Davis: No, You can do it outside of the class. A typical example is to provide a text representation of objects for output streams: http://www.parashift.com/c++-faq-lite/input-output.html#faq-15.8 – Black Jun 28 '12 at 07:41
0

Simply: No.

You always need the method (or variable) to be protected or public to let you call it from a subclass. (Or you can define another "getter" method to access it, but now it must be protected or public.)

Al Kepp
  • 5,831
  • 2
  • 28
  • 48
0

You could overload the plus operator for Person. Alternatively, getAge() is callable from the overloaded operator, since it's public.

loki11
  • 374
  • 1
  • 4
0

If you want to leave that property private, you can use a friend operator overloaded function.

  • So, because it's a friend function, it has no this pointer, meaning it was not called by the object, I simply pass the two objects I want to operate on into the function, correct? – Jerold Davis Jun 28 '12 at 07:01