Inherit base as protected
#include <iostream>
using namespace std;
class BaseClass {
protected:
int i, j; // private to BaseClass, but accessible by DerivedClass
public:
void setij(int a, int b) {
i = a;
j = b;
}
void showij() {
cout << i << " " << j << endl;
}
};
class DerivedClass : protected BaseClass{ // Inherit BaseClass as protected.
int k;
public:
void setk() {
setij(10, 12);
k = i * j;
}
void showall() {
cout << k << " ";
showij();
}
};
int main()
{
DerivedClass ob;
ob.setk(); // public member of DerivedClass
ob.showall(); // public member of DerivedClass
return 0;
}
Related examples in the same category