protected fields: private to base, but accessible by derived : protected « Class « C++ Tutorial






#include <iostream>
using namespace std;

class base {
protected:
  int i, j; //protected fields: 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:
  void setk() { 
     k=i*j; 
  }

  void showk() { 
    cout << k << "\n"; 
  }
};

int main()
{
  derived ob;

  ob.set(2, 3);
  ob.show();

  ob.setk();
  ob.showk();

  return 0;
}
2 3
6








9.10.protected
9.10.1.Demonstrate protected members.
9.10.2.protected fields: private to base, but accessible by derived
9.10.3.Protected Base-Class Inheritance