11

I know that the derived class can't access the private members of the base class, so why does the derived class inherit the private members of the base class? Is there any case that it is useful?

Thanks!

skydoor
  • 25,218
  • 52
  • 147
  • 201
  • 10
    What does the book you are learning C++ from have to say on this subject? –  Jan 06 '10 at 21:35

9 Answers9

28

The derived class needs the private members even though it can't access them directly. Otherwise it's behavior would not build on the class it is deriving from.

For example, pretend the private stuff is:

int i;

and the class has a geti() and seti(). The value of i has to be put somewhere, even if it is private,

Richard Pennington
  • 19,673
  • 4
  • 43
  • 72
  • Sir I admit it, rarely I came across to such an elegant and awesome answer. Thanks you sir for this answer specially the last line :)) – Ibrahim Amer Mar 04 '16 at 22:42
13

The public and protected methods of the base class can still access private variables of the base class, and these methods are available in the derived class.

UncleO
  • 8,299
  • 21
  • 29
4

The base class can still use the private member variables & methods.

If you want the derived classes to have access to members but keep those members hidden from the outside world, make them protected:.

Here's an example to illustrate:

class Base
{
public:
  Base() : val(42.0f) {};
  float GetValue() const 
  {
    return val_;
  }
private:
  float val_;
};

class Derived : public Base
{
public:
  float ComputedValue() const
  {
    return GetValue() * 2.0f;
  } 
};
John Dibling
  • 99,718
  • 31
  • 186
  • 324
4

Don't forget that the base class may have methods that are not private, and thus accessible by the derived class. Those protected or public base class methods can still invoke the private base class methods. This is particularly useful if you want to lock down core functionality in the base class, such as with a Template Method design pattern implementation:

class base
{
public:

  virtual ~base() { /* ... */ }

  virtual void base_func() { foo_private (); }
  virtual void do_func() = 0;

private:

  void foo_private()
  {
    // pre-do_func() operations

    do_func();

    // post-do_function operations
  }

};

class derived : public base
{
public:

  void derived_func() { base_func(); }

  virtual void do_func()
  {
    // Derived class specific operations
  }
};
Void - Othman
  • 3,441
  • 18
  • 18
2

The reason is because derived classes have an is-a relationship to the superclass.

A derived class instantiation IS A superclass instantiation...just with more (or less due to setting some superclass functions private) stuff.

Paul Nathan
  • 39,638
  • 28
  • 112
  • 212
  • 2
    Very confusing for someone new to OOP – Jaywalker Jan 07 '10 at 05:46
  • @Jaywalker I don't see how it's confusing. The derived class _is-a_ base class, and its base component still needs to be able to access _its_ `private` variables in its methods, regardless of whether it allowed the derived class to use them directly. – underscore_d Nov 19 '17 at 20:43
2

As has been outlined by other answers here, the derived class syntactically cannot access the private members of the base class; but it needs to have a copy of the same in its memory layout. Think of casting. using 'C' casting you can cast a derived to a private base. The compiler would then need the correct memory offset in order to produce a valid memory-layout for the base object.

Ex.

class Base {
public: 
  void printA()  {
    a = 10;
    std::cout << a << std::endl;
  }
private:
  int a;
};
class Derived : private Base{
  int b;
};

Derived* d = new Derived;
Base* b = (Base*)d;
b->printA();
Abhay
  • 7,092
  • 3
  • 36
  • 50
0

The derived class doesn't "inherit" the private members of the base class in any way - it can't access them, so it doesn't "inherit" them.

An instance of the derived class contains instances of the private members of the base class, for obvious reasons.

So I don't even know what you mean by that question.

Stefan Monov
  • 11,332
  • 10
  • 63
  • 120
  • 3
    "The derived class doesn't "inherit" the private members of the base class" Yes it does -- unless you are attempting to split syntactic hairs. – John Dibling Jan 06 '10 at 22:14
  • 1
    I assumed that by "inherit" he meant "can access" - that was the least nonsensical interpretation of the question. – Stefan Monov Jan 07 '10 at 01:11
0

when derived class object is created, base class constructor is also called for base object creation. if private members of base class are not allocated memory , the base object will be incomplete.

hence derived class object inherits private members of base, as they are created during creation of base class object, but are not accessible as they are private.

ishan
  • 301
  • 2
  • 9
0

Although the private members are not accessible from the base class, they are inherited by them because these properties are used by the derived class with the help of non-private functions.

Private members of the base class are not directly accessed, but derived by base class by derived class.

Community
  • 1
  • 1
PPARIHAR
  • 1
  • 2