Interview Questions Answers.ORG
Interviewer And Interviewee Guide
Interviews
Quizzes
Home
Quizzes
Interviews C Plus Plus Language Interviews:C++ Access ControlC++ COM ActiveXC++ ConstructorsC++ ContainersC++ Exception HandlingC++ FriendC++ InheritanceC++ Inline FunctionC++ New And DeleteC++ Operator OverloadingC++ Pointers & FunctionsC++ ProgrammerC++ ReferencesC++ Static DataC++ SyntaxC++ TemplateC++ Type CheckingC++ Virtual Functions
Copyright © 2018. All Rights Reserved
C++ Inheritance Interview Question:
Explain protected 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 protected:
Private members of base class are not accessible to derived class.
Protected members of base class remain protected in derived class.
Public members of base class become protected in derived class.
#include <iostream>
using namespace std;
class base
{
protected:
int i, j;
public:
void setij(int a, int b)
{
i = a;
j = b;
}
void showij()
{
cout <<”nI:”<<i<<”n J:<<j;
}
};
class derived : protected 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 protected member of derived
ob.setk(); //ok setk() is public member of derived
//ob.showij(); // not allowed. Showij() is protected member of derived
ob.showall(); // ok showall() is public member of derived
return 0;
}
Submitted by: Administrator
Private members of base class are not accessible to derived class.
Protected members of base class remain protected in derived class.
Public members of base class become protected in derived class.
#include <iostream>
using namespace std;
class base
{
protected:
int i, j;
public:
void setij(int a, int b)
{
i = a;
j = b;
}
void showij()
{
cout <<”nI:”<<i<<”n J:<<j;
}
};
class derived : protected 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 protected member of derived
ob.setk(); //ok setk() is public member of derived
//ob.showij(); // not allowed. Showij() is protected member of derived
ob.showall(); // ok showall() is public member of derived
return 0;
}
Submitted by: Administrator
Copyright 2007-2025 by Interview Questions Answers .ORG All Rights Reserved.
https://InterviewQuestionsAnswers.ORG.
https://InterviewQuestionsAnswers.ORG.