Inheritance In C++

Inheritance In C++

With the aid of examples, we will learn about inheritance in C++ in this tutorial.In object-oriented programming, inheritance is among the most crucial ideas.

It is simpler to design and manage applications when we can declare a class in terms of another class thanks to inheritance.

Additionally, it allows for quick implementation times and code functionality reuse.

Programmers have the option to specify that a new class should inherit the members of an existing class rather than writing entirely new data members and member functions.

The new class is referred to as the derived class, and the current class is known as the base class.

Inheritance is the capacity of a class to derive traits and properties from another class. One of the key components of Object-Oriented Programming is inheritance.

New classes are produced through the process of inheritance from the current classes. The current class is referred to as the “base class” or “parent class,” and the newly generated class is referred to as the “derived class” or “child class.” The term “inherited from the base class” now refers to the derived class.

When we say a derived class inherits the base class, we imply that it receives all of the base class’s properties without altering them and has the option to add new features of its own. The base class won’t be impacted by these additional features in the derived class. The base class’s specialized class is known as the derived class.

Subclass of Derived Class: A subclass or derived class is a class that receives properties from another class.


Super Class: The class from which a subclass inherits its properties is referred to as the Base Class or Superclass.


The following subtopics make up this article:

  • When should one use inheritance?
  • Transmission Methods
  • various inheritances

One of the essential components of C++’s Object-oriented programming is inheritance. It enables us to derive a new class from an existing class (base class).

It is possible for classes in C++ to inherit methods and attributes from one another. The “inheritance idea” is divided into two categories:

Base class (parent) is the class being inherited from, whereas derived class (child) is the class that inherits from another class.
Use the: sign to indicate that you want to inherit from a class.

The derived class can add new features on top of those that are inherited from the base class. For instance,

class Animal {
    // eat() function
    // sleep() function
};

class Dog : public Animal {
    // bark() function
};

Here, the Animal class is where the Dog class is descended from. The dog has access to Animal’s species because it is descended from animals.

Keep in mind that Dog inherits from Animal while using the term public.

class Dog : public Animal {…};

Instead of public, we might alternatively use the words private and protected. Later in this tutorial, we will learn about the differences between utilizing private, public, and protected.

Inheritance is a relationship

Inheritance is a relationship that is. Only when there is an is-a relationship between the two classes do we use inheritance.

Here are a few instances:

  • A vehicle is a car.
  • The fruit is an orange.
  • A surgeon is a doctor.
  • An animal is a dog.

Example 1: Simple Example of C++ Inheritance

// C++ program to demonstrate inheritance

#include <iostream>
using namespace std;

// base class
class Animal {

   public:
    void eat() {
        cout << "I can eat!" << endl;
    }

    void sleep() {
        cout << "I can sleep!" << endl;
    }
};

// derived class
class Dog : public Animal {
 
   public:
    void bark() {
        cout << "I can bark! Woof woof!!" << endl;
    }
};

int main() {
    // Create object of the Dog class
    Dog dog1;

    // Calling members of the base class
    dog1.eat();
    dog1.sleep();

    // Calling member of the derived class
    dog1.bark();

    return 0;
}

Output

I can eat!
I can sleep!
I can bark! Woof woof!!

In this case, dog1 (the Dog-derived class object) has access to Animal-derived class members. Due to Dog inheriting traits from animals.

// Calling members of the Animal class
dog1.eat();
dog1.sleep();

C++ protected Members

When discussing C++ inheritance, the access modifier protected is particularly important.

Protected members are inaccessible from outside the class, just like private members are. However, derived classes and friend classes/functions can access them.

If we wish to hide a class’s data while yet wanting its derived classes to inherit it, we require protected members.

Please see our C++ Access Modifiers guide for further information about protection.

Example 2: C++ protected Members

// C++ program to demonstrate protected members

#include <iostream>
#include <string>
using namespace std;

// base class
class Animal {

   private:
    string color;

   protected:
    string type;

   public:
    void eat() {
        cout << "I can eat!" << endl;
    }

    void sleep() {
        cout << "I can sleep!" << endl;
    }

    void setColor(string clr) {
        color = clr;
    }

    string getColor() {
        return color;
    }
};

// derived class
class Dog : public Animal {

   public:
    void setType(string tp) {
        type = tp;
    }

    void displayInfo(string c) {
        cout << "I am a " << type << endl;
        cout << "My color is " << c << endl;
    }

    void bark() {
        cout << "I can bark! Woof woof!!" << endl;
    }
};

int main() {
    // Create object of the Dog class
    Dog dog1;

    // Calling members of the base class
    dog1.eat();
    dog1.sleep();
    dog1.setColor("black");

    // Calling member of the derived class
    dog1.bark();
    dog1.setType("mammal");

    // Using getColor() of dog1 as argument
    // getColor() returns string data
    dog1.displayInfo(dog1.getColor());

    return 0;
}

Output

I can eat!
I can sleep!
I can bark! Woof woof!!
I am a mammal
My color is black

Since the variable type is protected in this case, the Dog derived class can access it. This is evident since we used the procedure setType to initialize the type in the Dog class ().

On the other hand, Dog does not allow the initialization of the private variable color.

class Dog : public Animal {
public:
void setColor(string clr) {
// Error: member “Animal::color” is inaccessible
color = clr;
}
};

Additionally, because the protected keyword conceals data, we are unable to directly obtain type from an object of the Dog or Animal class.

// Error: member “Animal::type” is inaccessible
dog1.type = “mammal”;

C++ Inheritance Access Modes

We studied C++ access specifiers like public, private, and protected in our earlier lectures.

So far, we’ve utilized the public keyword to derive a class from a base class that already existed. The private and protected keywords can be used to inherit classes, though. For instance,

class Animal {
// code
};
class Dog : private Animal {
// code
};

class Cat : protected Animal {
// code
};

Access modes refer to the many ways we can derive classes. The following happens as a result of these access modes:

public: If a derived class is declared in public mode, all base class members are passed down to the derived class unchanged.

private: In this scenario, every base class member is converted to a private member in the derived class.

protected: In the derived class, the public members of the base class are changed to protected members.

The derived class’s private members are always private in the base class.

Visit our C++ public, private, and protected inheritance tutorial to learn more.

Overriding Member Functions through Inheritance

Let’s say that member functions with the same name and arguments exist in both the base class and the derived class.

The member function in the derived class is called in place of the one in the base class if we create an object of the derived class and attempt to access that member function.

The base class member function is superseded by the member function of the derived class.

Find out more about C++’s function overriding.

This Post Has One Comment

Leave a Reply