Inherit base as private : Inheritance « Class « C++






Inherit base as private

Inherit base as private
  

#include <iostream>
using namespace std;

class BaseClass {
  int x;
public:
  void setx(int n) { 
     x = n; 
  }
  void showx() { 
     cout << x << '\n'; 
  }
};

// Inherit BaseClass as private.
class DerivedClass : private BaseClass {
  int y;
public:
  
  void setxy(int n, int m) { 
     setx(n);             // setx is accessible from within DerivedClass
     y = m; 
  }
  
  void showxy() { 
     showx();             // showx is accessible from within DerivedClass
     cout << y << '\n'; 
  }
};
 
int main()
{
  DerivedClass ob;

  ob.setxy(10, 20); 

  ob.showxy();

  return 0;
}


           
         
    
  








Related examples in the same category

1.Public inheritancePublic inheritance
2.Three level public inherianceThree level public inheriance
3.Make field public during private inheritance
4.Demonstrate inheriting a protected base class.Demonstrate inheriting a protected base class.
5.A simple example of inheritance.A simple example of inheritance.
6.Share member variables between sub classShare member variables between sub class
7.Virtual functions retain virtual nature when inherited.Virtual functions retain virtual nature when inherited.
8.call contructor from parent class
9.Cascade constructor and destructor call
10.Call parent constructor and pass in parameter
11.Access control under inheritance