Demonstrate protected members. : Protected « Class « C++






Demonstrate protected members.

Demonstrate protected members.
 

 
#include <iostream> 
using namespace std; 
 
class BaseClass { 
protected: 
  int i, j;             // private to BaseClass, but accessible to D 
public: 
  void set(int a, int b) { 
     i = a; 
     j = b; 
  } 
  void show() { 
     cout << i << " " << j << endl; 
  } 
}; 
 
class DerivedClass : public BaseClass { 
  int k; 
public: 

  void setk() {         // DerivedClass may access BaseClass's i and j 
     k = i * j; 
  } 
 
  void showk() { 
     cout << k << endl; 
  } 
}; 
 
int main() 
{ 
  DerivedClass object; 
 
  object.set(2, 3);     // OK, set() is public in BaseClass 
  object.show();        // OK, show is public BaseClass 
 
  object.setk(); 
  object.showk(); 
 
  return 0; 
}
           
         
  








Related examples in the same category

1.Inheriance for protected member Inheriance for protected member
2.Inherit base as protectedInherit base as protected
3.protected inheritance