Why can't I access private members in a derived class?

My book says it right here, like so:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
//Base class declaration
class Person {
public:
  const string& NameIs() const;
  void change_name(const string& n);
  void write_info() const;
private:
  string name;
};

//Derivate class declaration
class Employee : public Person {
public:
  Employee() : salary(0) {}
  long int earn() const;
  change_salary( long int new_salary );
  void write_info() const;
private:
  long int salary;
};


"A derived class inherits all data members from the base class ... This means that an object of the class Employee has the data members name and salary."

Then it shows a nice little picture of ugly rectangles where an object of the Person class has "name" whereas an object of the Employee class has "name" and "salary".

So I try to access a private member from a subclass and it says "error: within this context" and that it's a private member.

WTF.
You can't access private members outside the class ( or friends ), to access it from a derived class, it needs to be protected
That solved it. Thanks!
Topic archived. No new replies allowed.