Home » C++ Inheritance in C++ with examples C++ Inheritance in C++ with examples 1

Save Saved Removed 0

Inheritance in C++ is a programming technique for producing a new class which has a properties and functionality of a class from which it is inherited. The class which is to be produced is called child class or derived class, the class from which it is inherited is called parent class or a base class.

Inheritance is one of the most powerful technique of programming, which is used for code reusability, program reliability and for saving programmer’s precious time and effort.

Types of Inheritance:-

There are two types of inheritance.

Single inheritance. Multiple inheritance.

In this tutorial we will thoroughly discuss the single inheritance with proper examples, so you could understand it easily.

Let’s take an example of a vehicle class, this class has few properties, which are number of gears, number of seats, top speed, and Color and oil capacity. Now we will discuss child class, vehicle class could have several children class, which can be a car, motorcycle, bus and a truck. The beauty of inheritance is that, whenever child classes are created they will inherit the properties and functionality of there parent class. For example all the children of vehicle have inherited properties which are number of gears, number of seats, top speed, and Color and oil capacity. Also child class could have his own functionality. Inheritance also used for implementing other programming concepts like polymorphism and function overriding.

For implementing inheritance, parent class should use protected access specifier, so that child class could utilize properties and functions of parent class.

Syntax of Inheritance:-

The syntax of defining the derived class is as follows:

Class sub_class : specifier parent_class

{

Body of the class

};

Sub_class It is the name of a derived class.

: It creates a relationship between derived class and super class.

Specifier: It indicates the type of inheritance. It can be private, public or protected.

Parent_class: It indicates the name of parent class that is being inherited.

Example of Inheritance in C++:-

Let’s take a look at the example program:-

#include<iostream> #include<conio.h> class Parent { protected: int n; public: Parent() { n = 0; } Parent(int p) { n = p; } void show() { std::cout << "n = " << n << std::endl; } }; class Child : public Parent { private: char ch; public: Child() : Parent() { ch = 'x'; } Child(char c, int m) : Parent(m) { ch = c; } void display() { std::cout << "ch = " << ch << std::endl; } }; main() { Child obj1, obj2('@', 100); std::cout << "obj1 is as follows :

"; obj1.show(); obj1.display(); std::cout << "

obj2 is as follows :

"; obj2.show(); obj2.display(); getch(); }

Above program shows the perfect examples of Parent child relationship. In this program we have two classes, first class named as parent and another class named as child which is inherited by parent class. In Parent class we have define a function show() for the display of variable “n”. In the child class we have defined a function display, which will display the value of variable “ch”. We can access the constructor of parent class by the use of scope resolution operator (“:”). At the end we make two objects of a child class in a main function. By the used of first child object we invoke the function of parent class and by the use of second object we invoke the function of child class. Therefore we can invoke and manipulate the inherited properties and functionality by using the objects of child class.