Trusted answers to developer questions

What are the different types of inheritance in C++?

Get Started With Machine Learning

Learn the fundamentals of Machine Learning with this free course. Future-proof your career by adding ML skills to your toolkit — or prepare to land a job in AI or Data Science.

Inheritance is a concept used in the object-oriented paradigm to write code that is more maintainable and reusable. C++ has different types of inheritance (e.g., access specifiers) that influence the accessibility of the members of a class.

svg viewer

Effects of using different types

  1. When inheritance is applied, the members of the base class are copied into the derived class. The access specifiers of these copied members, in the derived class, depends on the type of inheritance used.

  2. The type of inheritance used only influences the accessibility of these copied members when they are accessed using an object of the derived class.

One rule to remember is that accessibility can only be decreased (e.g., from public to protected) and not increased (e.g., from private to public).

The keywords used to label different types of inheritance are the same as the keywords used for access specifiers. The syntax for using these keywords is explained in the examples below, where the copied members are commented out in the derived classes to give a better understanding of the concept:

1. public

In public inheritance, all members of the base class with the same accessibility are copied into the derived class. This means that private and protected members remain inaccessible through an object of the derived class.

Public inheritance is the most common type of inheritance used. When no type is explicitly mentioned, public inheritance is used by default.

Example

class Base{
private:
int private_base;
protected:
int protected_base;
public:
int public_base;
};
class Derived: public Base{
private:
int private_derived;
// int private_base;
protected:
int protected_derived;
// int protected_base;
public:
int public_derived;
// int public_base;
};
int main(){
// Access 'copied' members of Base class using object of Derived class:
Derived d;
d.private_base; // Not accessible
d.protected_base; // Not accessible
d.public_base; // Accessible
}

2. protected

public members are copied as protected, while all other members keep their original accessibility in protected inheritance.

Example

class Base{
private:
int private_base;
protected:
int protected_base;
public:
int public_base;
};
class Derived: protected Base{
private:
int private_derived;
// int private_base;
protected:
int protected_derived;
// int protected_base;
// int public_base;
public:
int public_derived;
};
int main(){
// Access 'copied' members of Base class using object of Derived1 class:
Derived d;
d.private_base; // Not accessible
d.protected_base; // Not accessible
d.public_base; // Not accessible
}

3. private

Private inheritance copies all members of the base class into the derived class as private members.

Example

class Base{
private:
int private_base;
protected:
int protected_base;
public:
int public_base;
};
class Derived: private Base{
private:
int private_derived;
// int private_base;
// int protected_base;
// int public_base;
protected:
int protected_derived;
public:
int public_derived;
};
int main(){
// Access 'copied' members of Base class using object of Derived1 class:
Derived d;
d.private_base; // Not accessible
d.protected_base; // Not accessible
d.public_base; // Not accessible
}

Example

The following example is a bit more complex as it adds another child class of the derived class:

class Base{
private:
int private_base;
protected:
int protected_base;
public:
int public_base;
};
class Derived1: protected Base{
private:
int private_derived1;
// int private_base;
protected:
int protected_derived1;
// int protected_base;
// int public_base;
public:
int public_derived1;
};
class Derived2: public Derived1{
private:
// int private_base;
// int private_derived1;
protected:
// int protected_base;
// int public_base;
// int protected_derived1;
public:
// int public_derived1;
};
int main(){
// Access 'copied' members of Base class using object of Derived1 class:
Derived1 der1;
der1.private_base; // Not accessible
der1.protected_base; // Not accessible
der1.public_base; // Not accessible
// Access 'copied' members of Derived1 class using object of Derived2 class:
Derived2 der2;
der2.private_derived1; // Not accessible
der2.protected_derived1; // Not accessible
der2.public_derived1; // Accessible
}

RELATED TAGS

object oriented
public
protected
private
c++
Copyright ©2024 Educative, Inc. All rights reserved
Did you find this helpful?