Demonstrate inheriting a protected base class. : Inheritance « Class « C++






Demonstrate inheriting a protected base class.

Demonstrate inheriting a protected base class.
  


#include <iostream>
using namespace std;

class BaseClass {
  int i;
protected:
  int j;
public:
  int k;
  void setInt(int a) { 
     i = a; 
  }
  int getInt() { 
     return i; 
  }
};


class DerivedClass : protected BaseClass {  // Inherit BaseClass as protected.
public:
  void setj(int a) { 
     j = a; 
  } 
  void setk(int a) { 
     k = a; 
  } 
  int getj() { 
     return j; 
  }
  int getk() { 
     return k; 
  }
};

int main()
{
  DerivedClass ob;

  ob.setk(10);
  cout << ob.getk() << ' ';
  ob.setj(12);
  cout << ob.getj() << ' ';

  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.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