Class Inheritance

From my book:

"There was a good reason to choose the version of the class CBox with public data members in the previous example, rather than the later, more secure version with private data members. Although private data members of a base class are also members of a derived class, they remain private to the base class in the derived class, so member functions defined in the derived class cannot access them. They are only accessible in the derived class through function members of the base class that are not private. You can demonstrate this very easily by changing all the CBox class data members to private and putting a Volume() function in the derived class CCandyBox, so that the class definition is as follows......"

Im really confused in this sentence:

"Although private data members of a base class are also members of a derived class, they remain private to the base class in the derived class, so member functions defined in the derived class cannot access them."

What does it mean they remain private to the base class IN the derived class? How is the base class IN the derived class? Also why are private members of the base class even private to the derived class?

Is there any reason?

I tried to do some research but I could only find articles that tell the rule and not the reason.

Hope someone can explain!
Last edited on
all that mean is when you declare a data member as private in the base class, you cannot access it by name in a derived class, because you cannot access a private data member outside its class by name. You have to use one of the base class's public member methods to access it.
Last edited on
It means that the derived class always contains all the private data members of the base class but they are not accessible by the derived class either directly or through member functions defined in the derived class.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class A
{
public:
   int getX() { return x; }
private:
   int x;
   int y;
};
class B : public A
{
public:
   int getY() { return y; } // error - not permitted because y is private to A
private:
   int z;
};

B object1;

object1 contains all 3 integers in it: x, y and z but only z is directly accessible to any code you write in B and x is only accessible through the getX method so that you may read its value but not change it.
The derived class always contains all of the base class. This is how inheritance works.
The goal of keeping private base class members private to the derived class is so that any object of class B will also behave as an object of type A (because there is an A inside every B). Allowing derived classes to interfere with base class private members would mean that you couldn't guarantee that A would behave as its class definition specified.
Oh ok thanks!
But I still dont understand what it means they are private to the base class IN the derived class.

How are they private to the base class IN the derived class. The base class "in" the derived class can access them via its functions from the base class.

I understand what it means, its just that this sentence is confusing me a bit.

By base class to interfere you mean any functions that are not defined inside of the base class, but in the derived class try to access right?

But anyways thanks Plover!
Last edited on
Topic archived. No new replies allowed.