English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

C++ Inheritance

In this article, you will learn about C ++All the knowledge of inheritance. More specifically, what inheritance is and different methods of implementing inheritance through examples.

Inheritance is C ++One of the main features of object-oriented programming. It allows users to create a newClass(Derived class).

Derived classes inherit all the functions of the base class and can have their own other functions.

Why use inheritance?

Suppose you need three characters in the game-amathematics teacher(Maths teacher),afootball player (Footballer)and amerchants(Businessman).

Since all roles are people, they can walk and talk. However, they each have some special skills. Mathematics teachers canteach mathematics, football players canplay football, merchants canRunning a business.

You can create three classes separately that can walk, talk, and perform their special skills, as shown in the figure below.

In each class, you will copy the same Walk (walking) and Talk (talking) code for each character.

If you want to add new features-eat (eating), we need to implement the same code for each character. This can easily lead to errors (when copying) and duplicate code.

If we have aPersonClass, which has basic functions such as speaking, walking, eating, sleeping, and adding special skills to these functions according to our roles, is easymore. This is done using inheritance.

With inheritance, you do not have to implement the same code for each class. You just need toInheritanceThat's it.

Therefore, for mathematics teachers (derived class), you can inherit all the functions of Person (base class) and add new functions.TeachMaths(teaching mathematics). Similarly, for football players, you inherit all the functions of Person and add new functionsPlayFootball(playing football) and so on.

This makes your code more concise, easy to understand, and scalable.

It is important to remember that:When using inheritance, each derived class should meet the conditions, regardless of whether it is a base class. In the above example, the mathematics teacher isa person, a football player isa person. You can't have:Businessman is a business.

C ++Implementation of inheritance in programming

class Person 
{
  ... .. ...
};
class MathsTeacher : public Person 
{
  ... .. ...
};
class Footballer : public Person
{
  .... .. ...
};

In the above example, Person class is the base class, and MathsTeacher and Footballer classes are derived from Person.

Derived classes appear together with the class declaration, followed by a colon, the keyword public, and the name of the base class from which the class is derived.

Since MathsTeacher and Footballer are derived from Person, they can access all the member data and member functions of Person.

Example: C ++Inheritance in programming

Create game characters using the concept of inheritance.

#include <iostream>
using namespace std;
class Person
{
     public:
        string profession;
        int age;
        Person(): profession("unemployed"), age(16) { }
        void display()
        {
             cout << "My profession is: " << profession << endl;
             cout << "My age is: " << age << endl;
             walk();
             talk();
        }
        void walk() { cout << "I can walk." << endl; }
        void talk() { cout << "I can talk." << endl; }
};
// The MathsTeacher class inherits from the base class Person.
class MathsTeacher : public Person
{
    public:
       void teachMaths() { cout << "I can teach mathematics." << endl; }
};
// The Footballer class inherits from the base class Person.
class Footballer : public Person
{
    public:
       void playFootball() { cout << "I can play football." << endl; }
};
int main()
{
     MathsTeacher teacher;
     teacher.profession = "teacher";
     teacher.age = 23;
     teacher.display();
     teacher.teachMaths();
     Footballer footballer;
     footballer.profession = "athlete";
     footballer.age = 19;
     footballer.display();
     footballer.playFootball();
     return 0;
}

Output result

My profession is: teacher
My age is: 23
I can walk
I can talk
I will teach mathematics.
My profession is: athlete
My age is: 19
I can walk
I can talk
I will play football.

In this program, Person is the base class, and MathsTeacher and Footballer are derived from the base class Person.

The Person class has two data members- profession and age. It also has two member functions- walk() and talk()

Both MathsTeacher and Footballer can access all data members and member functions of Person.

However, MathsTeacher and Footballer also have their own member functions: teachMaths() and playFootball(). These functions can only be accessed by their own class.

In the main() function, a new MathsTeacher teacher object is created.

Because it can access the data members of Person, it can set the profession (occupation) and age of teacher. Display this data using the display() function defined in the Person class. Similarly, the teachingMaths() function is defined in the MathsTeacher class.

Similarly, a new Footballer footballer object will also be created. It can also access the data members of Person and can display them by calling the display() function. Then only the footballer (footballer) can access the playFootball() function.

Access specifiers in inheritance

When creating a derived class from a base class, different access specifiers can be used to inherit the base class's data members.

These can be public, protected, or private.

In the above example, the base class Person is publicly inherited by MathsTeacher and football.

Learn aboutC ++of public, protected, and private inheritanceMore Information.

Member function overriding in inheritance

Assuming that the member functions of the base class and derived class have the same name and parameters.

If an object of the derived class is created and the member function is attempted to be accessed, only the member function of the derived class can be called.

The member functions of the derived class will override the member functions of the base class.

Learn aboutC ++of function overridingMore Information.