Demonstrate public inheritance. : public inheritance « Class « C++ Tutorial






#include <iostream> 
using namespace std; 
 
class BaseClass { 
  int i, j; 
public: 
  void set(int a, int b) { 
     i = a; j = b; 
  } 
  void show() { 
     cout << i << " " << j << "\n"; 
  } 
}; 
 
class DerivedClass : public BaseClass { 
  int k; 
public: 
  DerivedClass(int x) { 
     k = x; 
  } 
  void showDerivedClass() { 
     cout << k << "\n"; 
  } 
 
  // i = 10; // Error! i is private to BaseClass and access is not allowed. 
}; 
 
int main() 
{ 
  DerivedClass ob(3); 
 
  ob.set(1, 2); // access member of base class 
  ob.show();    // access member of base class 
 
  ob.showDerivedClass();   // uses member of derived class 
 
  return 0; 
}
1 2
3








9.14.public inheritance
9.14.1.Demonstrate public inheritance.
9.14.2.Inherited as public
9.14.3.Inheritance and protected Members
9.14.4.Base-Class Access Control