0 ratings 0% found this document useful (0 votes) 2 views 18 pages 9 Combination of Inheritance
The document explains the 9 combinations of inheritance and member access in C++, detailing how access specifiers (public, protected, private) and inheritance types (public, protected, private) affect member accessibility in derived classes. It includes rules and examples for each combination, emphasizing that private members are not inherited directly and can only be accessed through base class functions. Key points for exam preparation highlight the importance of understanding these combinations for class design and polymorphism.
AI-enhanced title and description
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content,
claim it here .
Available Formats
Download as PDF or read online on Scribd
Go to previous items Go to next items
Save 9 combination of inheritance For Later Excellent! Let's go step by step, thoroughly covering the 9 combinations of
inheritance and member access in C++ as applicable to B.Tech exam preparation.
Introduction
In C++, the way members (data or functions) of a base class are inherited depends
on:
1. Access Specifiers:
© public, protected, private in the base class.
2. Inheritance Type:
© public, protected, private inheritance in the derived class.
These combinations affect whether base class members remain accessible in the
derived class or outside it.
Base Class Member Types
Member Type Accessible by
public everyone
protected base and derived classes
private base class onlyInheritance Specifiers
© public inheritance + maintains access levels.
* protected inheritance — public and protected become protected.
© private inheritance public and protected become private.
The 9 Combinations
We will now go through each combination with clear examples and outputs.
1. Public Member + Public Inheritance
Rule:
public members of base remain public in derived class.
Example:
#include
using namespace std;
class Base {
public:
int pub;Base() { pub = 10; }
class Derived : public Base {
public:
void show() {
cout << "Public member: " << pub << endl;
u
int maind {
Derived d;
dshow); — // @ Accessible
cout << dpub; // &% Accessible
return 0;
Output:
Public member: 10
102. Public Member + Protected Inheritance
Rule:
* public members of base become protected in derived class.
Example:
#include
using namespace std;
class Base {
public:
int pub;
Base() { pub = 20; }
class Derived : protected Base {
public:
void show() {
cout << "Public member (now protected): " << pub << endl;int main() {
Derived d;
dshow); —// @% Accessible inside derived
// cout << d.pub; // Error! pub is now protected
return 0;
Output:
Public member (now protected): 20
3. Public Member + Private Inheritance
Rule:
public members of base become private in derived class.
Example:
#includeusing namespace std;
class Base {
public:
int pub;
Base() { pub = 30; }
class Derived : private Base {
public:
void show() ¢
cout << "Public member (now private): " << pub << endl;
ub
int maing {
Derived d;
dshow); — // @ Accessible inside derived
// cout << d.pub; // € Error! pub is now private
return 0;Output:
Public member (now private): 30
4. Protected Member + Public Inheritance
Rule:
* protected members of base remain protected in derived class.
Example:
#include
using namespace std;
class Base {
protected
int prot;
public:
Base() { prot = 40; }class Derived : public Base {
public:
void show() {
cout << "Protected member: " << prot << endl;
int main0 {
Derived d;
dshow); — // @ Accessible inside derived
// cout << d.prot; // € Error! protected is not accessible outside
return 0;
Output:
Protected member: 405. Protected Member + Protected Inheritance
Rule:
* protected members of base remain protected in derived class.
Example:
#include
using namespace std;
class Base {
protected
int prot;
public:
Base() { prot = 50; }
class Derived : protected Base {
public:
void show() {
cout << "Protected member: " << prot << endl;int main {
Derived d;
dshow); — // & Accessible inside derived
11 cout << d.prot; // X Error! not accessible outside
return 0;
Output:
Protected member: 50
6. Protected Member + Private Inheritance
Rule:
* protected members of base become private in derived class
Example:
#include
using namespace std;class Base {
protected:
int prot;
public:
Base() { prot = 60; }
class Derived : private Base {
public:
void show() ¢
cout << "Protected member (now private): " << prot << endl;
ub
int maing {
Derived d;
dshow); — // @ Accessible inside derived
// cout << d.prot; // Error! now private
return 0;
ulOutput:
Protected member (now private): 60
7. Private Member + Public Inheritance
Rule:
* private members of base are not inherited; they are only accessible
through base class's public/protected functions.
Example:
#include
using namespace std;
class Base {
private:
int priv;
public:
Base() { priv = 70; }int getPriv() { return priv; }// Accessor function
yu
class Derived : public Base {
public:
void show() {
cout << "Private member accessed via base function: " << getPriv() << endl;
int maind {
Derived d;
show); // @ Access through base function
11 cout << d.priv; // X Error! private member not accessible directly
return 0;
Output:
Private member accessed via base function: 70
58. Private Member + Protected Inheritance
Same as above; private members are not inherited.
Example:
#include
using namespace std;
class Base {
private:
int priv;
public:
Base(){ priv = 80; }
int getPriv(){ return priv; }
b
class Derived : protected Base {
public:
void show() {cout << "Private member accessed via base function: " << getPriv() << endl;
b
int main {
Derived d;
d.show); —// @ Accessible through base function
/I cout << d.priv; // X Error! private not directly accessible
return 0;
Output:
Private member accessed via base function: 80
9. Private Member + Private Inheritance
Rule:
* Same as above; private members are not inherited.
Example:
5#include
using namespace std;
class Base {
private:
int priv;
public:
Base() { priv = 90; }
int getPriv() { return priv; }
k
class Derived : private Base {
public:
void show() {
cout << "Private member accessed via base function: " << getPriv() << endl;
h
int main( {
Derived d;
16d.show();
1 @ Accessible through base function
// cout << d.priv; // & Error! not directly accessible
return 0;
Output:
Private member accessed via base function: 90
Class
Base Member Public
Inheritance
public public in derived
protected protected in
derived
v
Summary Table -
Access in Derived
Protected Private
Inheritance Inheritance
protected in private in derived
derived
protected in private in derived
derivedprivate not inherited not inherited not inherited
(access via (access via (access via
function only) function only) function only)
Important Notes for Exams
¥ private members are never inherited directly. They can only be accessed
through base class functions.
v¥ protected members are accessible within derived classes but not outside.
¥ public inheritance preserves access levels, while protected and private
inheritance restrict them.
¥ Scope resolution operator (::) and accessor functions help when access is
restricted.
¥ Understanding these combinations helps in solving problems related to class
design, polymorphism, and inheritance hierarchy.
Let me know if you want: v Practice problems on inheritance access
¥ More examples with constructors and destructors
v An explanation of virtual functions and overriding mechanisms.
18