Curriculum
Course: Learn C++
Login

Curriculum

Learn C++

Text lesson

Introduction to Inheritance in C++

In this lesson, you will learn

  • Introduction to Inheritance
  • Types of Inheritance
  • Syntax of Inheritance

 

Introduction to Inheritance

  • Inheritance is one of the fundamental concepts in object-oriented programming (OOP) and is a key feature of the C++ programming language.
  • It allows you to create new classes that are based on existing classes, inheriting their properties and behaviors.
  • Inheritance promotes code reuse and is a crucial part of building complex, hierarchical class structures.

 

Terminology

Before diving into the details of inheritance, let’s clarify some essential terminology:

Base Class (Parent Class or Super Class): The class whose properties and behaviors are inherited by another class.

Derived Class (Child Class or Sub Class): The class that inherits properties and behaviors from a base class.

 

Types of Inheritance

In C++, there are several types of inheritance, each serving different purposes:

1. Single Inheritance

A derived class inherits from a single base class.

 

2. Multilevel Inheritance

A derived class is created from another derived class.

 

3. Multiple Inheritance

A derived class inherits from multiple base classes.

 

4. Hierarchical Inheritance

Multiple derived classes inherit from a single base class.

 

Hybrid Inheritance

A combination of different types of inheritance.

 

Syntax of Inheritance

The syntax for inheriting from a base class is as follows:

class BaseClass {
    // Base class members
};

class DerivedClass : access_specifier BaseClass {
    // Derived class members
};

access_specifier can be either public, protected, or private and determines the visibility of the base class members in the derived class.

 

Examples

1. Private derivation

In C++, when a class is inherited in private mode, it affects the accessibility of the base class members in the derived class. Here’s how it works:

class Derived: private Base //Private derivation
{
          // Members of derived class
}
  • Private Members of the Base Class: These are never accessible directly in the derived class, regardless of the inheritance mode (public, protected, or private).

  • Protected Members of the Base Class: When inherited in private mode, protected members of the base class become private in the derived class. This means they can only be accessed by the derived class itself, not by further derived classes.

  • Public Members of the Base Class: When inherited in private mode, public members of the base class also become private in the derived class. They can be accessed within the derived class but are hidden from any further derived classes or from external code.

 

Example: Private Derivation

#include<iostream>
using namespace std;

class Base {
public:
    int publicVar;
protected:
    int protectedVar;
private:
    int privateVar;
};

class Derived : private Base {
public:
    void display() {
        publicVar = 10;        // Accessible but treated as private in Derived
        protectedVar = 20;     // Accessible but treated as private in Derived
        // privateVar = 30;    // Not accessible, private in Base
        cout << "Public Var: " << publicVar << endl;
        cout << "Protected Var: " << protectedVar << endl;
    }
};

int main() {
    Derived obj;
    obj.display();
    // obj.publicVar = 40;    // Error: publicVar is private in Derived
    return 0;
}

Output:

Public Var: 10   
Protected Var: 20

 

2. Public Derivation

In C++, when a class is inherited in public mode, it maintains the accessibility of the base class members in the derived class. Here’s how the members of the base class behave when inherited publicly:

class Derived: public Base //Public derivation
{
          // Members of derived class
}
  1. Private Members of the Base Class: These are never accessible directly in the derived class, regardless of the inheritance mode (public, protected, or private).

  2. Protected Members of the Base Class: When inherited in public mode, protected members of the base class remain protected in the derived class. This means they can be accessed by the derived class and any further derived classes but not by external code.

  3. Public Members of the Base Class: When inherited in public mode, public members of the base class remain public in the derived class. This means they can be accessed both by external code and by any further derived classes.

 

Example: Public Derivation

#include<iostream>
using namespace std;

class Base {
public:
    int publicVar;
protected:
    int protectedVar;
private:
    int privateVar;
};

class Derived : public Base {
public:
    void display() {
        publicVar = 10;        // Accessible, remains public in Derived
        protectedVar = 20;     // Accessible, remains protected in Derived
        // privateVar = 30;    // Not accessible, private in Base
        cout << "Public Var: " << publicVar << endl;
        cout << "Protected Var: " << protectedVar << endl;
    }
};

int main() {
    Derived obj;
    obj.display();
    obj.publicVar = 40;        // Accessible, remains public in Derived
    // obj.protectedVar = 50;  // Error: protectedVar is protected in Derived
    return 0;
}

Output:

Public Var: 10
Protected Var: 20


 

 


End of the lesson….enjoy learning

 

 

Student Ratings and Reviews

 

5.0
5.0 out of 5 stars (based on 1 review)
Excellent100%
Very good0%
Average0%
Poor0%
Terrible0%

 

 

November 8, 2024

amazing experience

ved

 

 

Submit a Review

 

 


Warning: Undefined array key "student_url_profile" in /var/www/wp-content/plugins/masterstudy-lms-learning-management-system/_core/lms/helpers.php on line 1340

Warning: Undefined array key "student_url_profile" in /var/www/wp-content/plugins/masterstudy-lms-learning-management-system/_core/lms/helpers.php on line 1345