C++ Inheritance

What is Inheritance?

Introduction

In object oriented programming inheritance plays a role by aiding in the organization and recycling of code. It permits the development of classes referred to as derived classes or subclasses which inherit characteristics and functionalities from a base class or superclass. The base class acts as a template offering shared methods and properties that can be utilized by derived classes. Through inheriting from the base class the derived class can. Alter these attributes and methods. This process streamlines code upkeep encourages code reusability and facilitates polymorphism. Allowing objects from classes to be treated as instances of a common superclass. Inheritance boosts modularity and adaptability, in software development.

Why is Inheritance Important in Object-Oriented Programming?

Inheritance is crucial in object-oriented programming as it allows developers to reuse code from existing classes. By creating new classes based on existing ones, developers can inherit properties and behaviors, saving time and reducing the need to rewrite code. This leads to more concise and maintainable programs.

In C++, inheritance allows the creation of class hierarchies, making it easier to organize and categorize objects. It also enables the establishment of relationships between different classes, promoting modular and efficient programming. Inheritance in C++ can be public, private, or protected, each defining different levels of accessibility for the members of the base class.

  • Public Inheritance: Members of the base class are accessible to the derived class and outside the class hierarchy.
  • Private Inheritance: Only the derived class can access the base class's members.
  • Protected Inheritance: The derived class and its friend classes can access the base class's members, protecting them from external access.

Basics of Inheritance in C++

Class Definition

In C++, inheritance allows a child class to inherit the members of a parent class. There are three types of inheritance: public, protected, and private.

Public Inheritance: The child class inherits all public members of the parent class.

class Parent {
public:
    // Public members
};

class Child : public Parent {
    // Child class members
};

Protected Inheritance: The child class inherits the protected members of the parent class.

class Parent {
protected:
    // Protected members
};

class Child : protected Parent {
    // Child class members
};

Private Inheritance: The child class inherits public and protected members of the parent class as private members.

class Parent {
    private:
        // Private members
};

class Child : private Parent {
    // Child class members
};

Base Classes

A base class provides a foundation for derived classes, containing common attributes and behaviors that can be inherited. Base classes help organize and reuse code efficiently, as derived classes can specialize and add more features. Multiple inheritance allows a derived class to inherit from more than one base class, combining characteristics from different sources.

For example, consider a base class Animal and derived classes Dog and Cat. Both Dog and Cat can inherit common attributes and methods from Animal while adding their own specialized properties.


class Animal {
    // Common members for all animals
};

class Dog : public Animal {
    // Specific members for dogs
};

Derived Classes

Derived classes are created from existing classes (base classes) and inherit their properties and behaviors. The derived class syntax involves using the keyword class, followed by the derived class name, a colon, and the visibility mode of the base class.


class BaseClass {
    // Members of the base class
};

class DerivedClass : public BaseClass {
    // Members of the derived class
};

If multiple inheritance is used, the base class names are separated by commas.

Types of Inheritance in C++

Single Inheritance

Single inheritance derives a new class from a single base class. The derived class inherits the properties and behaviors of the base class.


class BaseClass {
    // Members of the base class
};

class DerivedClass : public BaseClass {
    // Members of the derived class
};

Multiple Inheritance

Multiple inheritance allows a derived class to inherit from multiple base classes, combining their attributes and methods.


class BaseClass1 {
    // Members of the first base class
};

class BaseClass2 {
    // Members of the second base class
};

class DerivedClass : public BaseClass1, public BaseClass2 {
    // Members of the derived class
};

Multilevel Inheritance

Multilevel inheritance creates a chain of inheritance, where a class inherits from another class, which in turn inherits from another class.


class Grandparent {
    // Members of the grandparent class
};

class Parent : public Grandparent {
    // Members of the parent class
};

class Child : public Parent {
    // Members of the child class
};

Hierarchical Inheritance

Hierarchical inheritance involves multiple derived classes inheriting from a single base class.


class Animal {
public:
    void eat() {
        std::cout << "Animal can eat." << std::endl;
    }
};

class Dog : public Animal {
public:
    void bark() {
        std::cout << "Dog can bark." << std::endl;
    }
};

class Cat : public Animal {
public:
    void meow() {
        std::cout << "Cat can meow." << std::endl;
    }
};

Hybrid Inheritance

Hybrid inheritance combines two or more types of inheritance, creating a more complex class hierarchy.

Access Specifiers and Inheritance

Access specifiers, such as public, private, and protected, define the visibility of class members. These modifiers ensure encapsulation and control access to variables and methods. Inheritance allows the reuse of code and supports code organization. The application of access specifiers in inheritance determines the visibility of inherited members within the derived class.

Public Inheritance

Public inheritance allows a derived class to access the public and protected members of its base class. Public members remain public, and protected members remain protected in the derived class. Private members of the base class are not directly accessible in the derived class.

Protected Inheritance

Protected inheritance enables the derived class to access the protected members of the base class. These members are accessible within the derived class and any further derived classes.

Private Inheritance

Private inheritance restricts the access of the base class members to the derived class. Public and protected members of the base class become private members in the derived class. This restricts external access and helps maintain encapsulation.

Create a free account to access the full topic

“It has all the necessary theory, lots of practice, and projects of different levels. I haven't skipped any of the 3000+ coding exercises.”
Andrei Maftei
Hyperskill Graduate