×

C++ Programs

C++ Most popular & Searched Programs

C++ Basic I/O Programs

C++ Constructor & Destructor Programs

C++ Manipulators Programs

C++ Inheritance Programs

C++ Operator Overloading Programs

C++ File Handling Programs

C++ Bit Manipulation Programs

C++ Classes & Object Programs

C++ program to demonstrate example of private simple inheritance

Learn about private simple inheritance in C++, and implement private simple inheritance using a C++ program.
[Last updated : March 02, 2023]

Private Simple Inheritance in C++

In C++, the private simple inheritance is defined as the inheritance in which public and protected member of the base class become private members of the derived class.

This program will demonstrate example of private simple inheritance in c++ programming language.

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

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.