Inheritance and protected Members : public inheritance « Class « C++ Tutorial






#include <iostream>
using namespace std;
   
class base {
protected:
  int i, j; // private to base, but accessible by derived
public:
  void set(int a, int b) { i=a; j=b; }
  void show() { cout << i << " " << j << "\n"; }
};
   
class derived : public base {
  int k;
public:
  // derived may access base's i and j
  void setk() { k=i*j; }
   
  void showk() { cout << k << "\n"; }
};
   
int main()
{
  derived ob;
   
  ob.set(2, 3); // OK, known to derived
  ob.show(); // OK, known to derived
   
  ob.setk();
  ob.showk();
   
  return 0;
}








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