Inheritance in C++ with Example

Posted on

C++ Inheritance Reusability is one more necessary characteristic of Object Oriented Programming (OOP). It’s a good apply if programmers reuse codes somewhat than making an attempt to write down issues again and again which is time-consuming. So reusability reduces frustration and improve the working time. C++ helps the idea of reusability. Courses of C++ will be reuse is a number of methods. Considered one of them is the idea of Inheritance. On this chapter you’ll study inheritance and the way it works together with its varied varieties.


When creating a category, as an alternative of writing utterly new knowledge members and member capabilities, programmer can designate that the brand new class ought to inherit the members of an current class. This current class known as the bottom class, and the brand new class is known as the derived class.

What’s Inheritance?

The strategy of deriving a brand new class from an previous one known as inheritance. The previous class is known as base class and the brand new class is known as derived class or sub class. Inheritance idea permits programmers to outline a category by way of one other class, which makes creating and sustaining software simpler. When writing a brand new class, as an alternative of writing new knowledge member and member capabilities once more, programmers could make a bonding of the brand new class with the previous one which the brand new class ought to inherit the members of the prevailing class. A category can get derived from a number of courses, which implies it could inherit knowledge and capabilities from a number of base courses. Right here is the syntax how inheritance is carried out.

class derived-class: visibility-mode base-class

Visibility mode is utilized in inheritance of C++ to point out or relate how base courses are considered with respect to derived class. When one class will get inherited from one other, visibility mode is used to inherit all the general public and guarded members of the bottom class. Personal members by no means get inherited and therefore don’t participate in visibility. By default, visibility mode stays “non-public”.

What are Base class and Derived class?

The present class from which the derived class will get inherited is called the bottom class. It acts as a mum or dad for its baby class and all its properties i.e. public and guarded members get inherited to its derived class.

A derived class will be outlined by specifying its relationship with the bottom class along with its personal detains, i.e. members. The overall type of defining a derived class is:

class derived-class_name : visivility-mode base-class_name{ . . . .  // members of the derived class . . . .};

Types of Inheritance

C++ affords 5 forms of Inheritance. They’re:

  • Single Inheritance
  • A number of Inheritance
  • Hierarchical Inheritance
  • Multilevel Inheritance
  • Hybrid Inheritance (often known as Digital Inheritance)

inheritanc cpp - Inheritance in C++  with Example

 

Single Inheritance

In single inheritance there is just one base class and one derived class. The Derived class will get inherited from its base class. That is the best type of inheritance. Within the above determine, fig(a) is the diagram for single inheritance.

 

A number of Inheritance

In one of these inheritance, a single derived class might inherit from two or extra base courses. Within the above checklist of figures, fig(b) is the construction of A number of Inheritance.

Program for A number of Inheritance:

Instance:

#embody utilizing namespace std;class stud {protected:int roll, m1, m2;public:void get(){cout << "Enter the Roll No.: "; cin >> roll;cout << "Enter the two highest marks: "; cin >> m1 >> m2;}};class extracurriculam {protected:int xm;public:void getsm(){cout << "nEnter the mark for Extra Curriculam Activities: "; cin >> xm;}};class output : public stud, public extracurriculam {int tot, avg;public:void show(){tot = (m1 + m2 + xm);avg = tot / 3;cout << "nntRoll No: " << roll << "ntTotal  : " << tot;cout << "ntAverage: " << avg;}};int major(){output O;O.get();O.getsm();O.show();}

Instance:

Multiple Inheritance - Inheritance in C++  with Example

 

Hierarchical Inheritance

In one of these inheritance, a number of derived courses get inherited from a single base class. Within the above checklist of figures, fig(c) is the construction of Hierarchical Inheritance.

Instance:

class base_classname {properties;strategies;};class derived_class1 : visibility_mode base_classname {properties;strategies;};  class derived_class2 : visibility_mode base_classname {properties;strategies;}; ... ... ... ... ... ...  class derived_classN : visibility_mode base_classname {properties;strategies;};

Program for Hierarchical Inheritance:

Instance:

#embody #embody utilizing namespace std;class member {char gender[10];int age;public:void get(){cout << "Age: "; cin >> age;cout << "Gender: "; cin >> gender;}void disp(){cout << "Age: " << age << endl;cout << "Gender: " << gender << endl;}};class stud : public member {char degree[20];public:void getdata(){member::get();cout << "Class: "; cin >> degree;}void disp2(){member::disp();cout << "Degree: " << degree << endl;}};class employees : public member {float wage;public:void getdata(){member::get();cout << "Salary: Rs."; cin >> wage;}void disp3(){member::disp();cout << "Wage: Rs." << wage << endl;}};int major(){member M;employees S;stud s;cout << "Scholar" << endl;cout << "Enter knowledge" << endl;s.getdata();cout << endl << "Displaying knowledge" << endl;s.disp();cout << endl << "Workers Knowledge" << endl;cout << "Enter knowledge" << endl;S.getdata();cout << endl << "Displaying knowledge" << endl;S.disp();}

Instance:

Hierarchical Inheritance - Inheritance in C++  with Example

 

Multilevel Inheritance

The courses will also be derived from the courses which might be already derived. One of these inheritance known as multilevel inheritance.
Program for Multilevel Inheritance:

Instance:

#embody utilizing namespace std;class base {public:void display1(){cout << "nBase c
lass content material.";}};class derived : public base {public:void display2(){cout << "1st derived class content material.";}};class derived2 : public derived {void display3(){cout << "n2nd Derived class content material.";}};int major(){derived2 D;//D.display3();D.display2();D.display1();}

Instance:

Multilevel Inheritance - Inheritance in C++  with Example

 

Hybrid Inheritance

This can be a Combination of two or Extra Inheritance and on this Inheritance a Code Might Accommodates two or Three forms of inheritance in Single Code. Within the above determine, the fig(5) is the diagram for Hybrid inheritance.