Do you know private inheritance?
Submitted by: AdministratorWhen 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;
}
Submitted by: Administrator
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;
}
Submitted by: Administrator
Read Online C++ Inheritance Job Interview Questions And Answers
Top C++ Inheritance Questions
☺ | What is a base class? |
☺ | Explain protected inheritance? |
☺ | Do you know private inheritance? |
☺ | Explain Private Inheritance? |
☺ | Explain the advantages of inheritance? |
Top C Plus Plus Language Categories
☺ | C++ Pointers & Functions Interview Questions. |
☺ | C++ Operator Overloading Interview Questions. |
☺ | C++ Exception Handling Interview Questions. |
☺ | C++ Virtual Functions Interview Questions. |
☺ | C++ Template Interview Questions. |