/*C++ program to demonstrate example of private simple inheritance.*/
#include <iostream>
using namespace std;
class A
{
private:
int a;
protected:
int x; //can access by the derived class
public:
void setVal(int v)
{
x=v;
}
};
class B:private A
{
public:
void printVal(void)
{
setVal(10); //accessing public member function here
//protected data member direct access here
cout << "value of x: " << x << endl;
}
};
int main()
{
B objB; //derived class creation
objB.printVal();
return 0;
}
Output
value of x: 10
/*Here x is the protected data member of class A, class A is inherited privately to class B.
In, private inheritance only protected data member and member functions can be accessed by the derived class.
*/
Private simple inheritance program in C++
Output