Inherit base as private : private inheritance « Class « C++ Tutorial






#include <iostream>
using namespace std;

class base {
private:
  int i;
public:
  int j, k;
  void seti(int x) { i = x; }
  int geti() { return i; }
};

class derived: private base {
public:
  base::j;
  base::seti;
  base::geti;

  int a;
};

int main()
{
  derived ob;

  ob.j = 20; // legal because j is made public in derived
//ob.k = 30; // illegal because k is private in derived

  ob.a = 40; // legal because a is public in derived
  ob.seti(10);

  cout << ob.geti() << " " << ob.j << " " << ob.a;

  return 0;
}
10 20 40"








9.12.private inheritance
9.12.1.Inherit base as private
9.12.2.how to use an access declaration
9.12.3.Granting Access: Inherit base as private