Q:

C++ program to demonstrate example of private simple inheritance

belongs to collection: C++ Inheritance programs/examples

0

C++ program to demonstrate example of private simple inheritance

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Private simple inheritance program in C++

/*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.
*/

 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C++ program to read and print students information... >>
<< C++ program to demonstrate example of simple inher...