Make field public during private inheritance : Inheritance « Class « C++






Make field public during private inheritance

  
#include <iostream>
using namespace std;
class base 
{
   int i;
 public:
   int j, k;
   void seti(int x) {i = x;}
   int geti(void) {return i;}
};

class derived : private base 
{
 public:
                  
   base::j;       
   base::seti;    
   base::geti;    
   int a;
};

int main(void)
{
   derived object;

   object.j = 20; // legal because j is public

   object.a = 40;
   object.seti(10);
   cout << object.geti() << ", " << object.j << ", " << object.a;
}
  
    
  








Related examples in the same category

1.Public inheritancePublic inheritance
2.Three level public inherianceThree level public inheriance
3.Demonstrate inheriting a protected base class.Demonstrate inheriting a protected base class.
4.A simple example of inheritance.A simple example of inheritance.
5.Share member variables between sub classShare member variables between sub class
6.Virtual functions retain virtual nature when inherited.Virtual functions retain virtual nature when inherited.
7.Inherit base as privateInherit base as private
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