What is private inheritance? - C++

What is private inheritance?

- When a class is being derived from another class, we can make use of access specifiers. This is essentially useful to control the access the derived class members have to the base class.
- When inheritance is private :

i. Private members of base class are not accessible to derived class.
ii. Protected members of base class become private members of derived class.
iii. Public members of base class become private members of derived class.
#include <iostream>
using namespace std;
class base
{
   int i, j;
   public:
       void setij(int a, int b)
       {
          i = a;
          j = b;
       }

       void showij()
       {
          cout <<”\nI:”<<i<<”\n J:”<<j;
       }
};

class derived : private base
{
   int k;
   public:
       void setk()
       {
          //setij();
          k = i + j;
       }
       void showall()
       {
          cout <<”\nK:”<<k<<show();
       }
};

int main()
{
   derived ob;
    //ob.setij(); // not allowed. Setij() is private member of derived
   ob.setk(); //ok setk() is public member of derived
   //ob.showij(); // not allowed. Showij() is private member of derived
   ob.showall(); // ok showall() is public member of derived
   return 0;
}
What is protected inheritance? - C++
What is protected inheritance? - When a class is being derived from another class, we can make use of access specifiers...
How do we implement inheritance in C++?
How do we implement inheritance in C++? - The inheritance feature in C++ is implemented in the following manner....
What is inline function? - C++
What is inline function? - An inline function is a combination of macro & function. At the time of declaration or definition, function name is preceded by word inline....
Post your comment
Discussion Board
virtual constructor
it is easy to understand.........
ty
rohit yadav 02-10-2016