INHERITANCE IN C++

Introduction:

In this article, we are going to learn about inheritance in C++. Inheritance is one of the most important features of object-oriented programming. we have five types of inheritance. In C++, inheritance is a process in which one object acquires all the properties and behaviors of its parent object automatically.

Inheritance:

The capability of a class to inherit the properties of some other class is known as inheritance. the new class which is created from the parent class is known as the child class(derived class). the existing class is known as the parent class(superclass).

When do we use inheritance?

Consider a group of chocolates. we need to create classes for milky bar, snickers, and KitKat.the methods are quantity(),price(),company() will be same for all the three classes. If we avoid using inheritance for these three classes then we have to write all of these functions in each of the three classes. so let’s see the below example.

For example:

The above example results in duplication. So, there might be a chance of data redundancy

and errors. To avoid this type of error we use inheritance.

Using inheritance, we have to write the functions only one time instead of three times as have inherited the rest of the three classes from the base class (chocolates).

Modes of Inheritance:

Public Mode: If we derive a subclass from a public base class. Then the public member of the base class will become public in the derived class and protected members of the base class will become protected in the derived class.

See also  C++ STRING FUNCTIONS:

Protected Mode: If we derive a subclass from a Protected base class. Then both public members and protected members of the base class will become protected in the derived class.

Private Mode: If we derive a subclass from a Private base class. Then both public members and protected members of the base class will become Private in the derived class.

Types Of Inheritance:-

Single inheritance

Multilevel inheritance

Multiple inheritances

Hierarchical inheritance

Hybrid inheritance

Implementing inheritance in C++:

The syntax for creating the subclass:

class  <derived_class_name> : <access-specifier> <base_class_name>
{
        //body
}

Class      — A keyword to create a new class.

Derived_class_name   — the new class is inherited from the base class.

Access-specifier  — either private, public, or protected. private is the default.

Base-class-name  — the name of the base class.

Example program for inheritance:

In the below example program we inherit the properties from the base class to the derived class. the class is privately inherited. mul() function of class ‘A’ cannot be accessed by the object of class B. It can only be accessed by the member function of class B.

#include <iostream>  
using namespace std;  
class A  
{  
    int a = 23;  
    int b = 5;  
    public:  
    int mul()  
    {  
        int c = a*b;  
        return c;  
    }     
};  
  
class B: private A  
{  
    public:  
    void display()  
    {  
        int result = mul();  
        std::cout <<"Multiplication of a and b is : "<<result<< std::endl;  
    }  
};  
int main()  
{  
   B b;  
   b.display();  
  
    return 0;  
}  

Output:

Multiplication of a and b is: 69

In the next article, we discuss the types of inheritance in C++ in detail with the example programs.

Leave a Comment

Your email address will not be published. Required fields are marked *

Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

we provide projects, courses, and other stuff for free. in order for running we use Google ads to make revenue. please disable adblocker to support us.