Inheritance in C++ | Types of Inheritance

Inheritance in C++ is one of the key features of Object-oriented programming. It allows user to create a new class (derived class) from an existing class(base class). This makes it easier to create and maintain an application. This also provides an opportunity to reuse the code functionality and fast implementation time. An existing class that is “parent” of a new class is called a base class. New class that inherits properties of the base class is called a derived class. The idea of inheritance implements the is a relationship. For example, mammal IS-A animal, dog IS-A mammal hence dog IS-A animal as well and so on.
A class can be derived from more than one classes, which means it can inherit data and functions from multiple base classes. To define a derived class, we use a class derivation list to specify the base class(es). A class can be derived using the following syntax:

class derived-class: access-specifier base-class

Where access-specifier is one of public, protected, or private, and base-class is the name of a previously defined class. If the access-specifier is not used, then it is private by default.

Run Online

#include <iostream>
using namespace std;
// Base class
class Shape {
protected:
  int width;
  int height;
public:
  void setWidth(int w) {
    width = w;
  }
  void setHeight(int h) {
    height = h;
  }
};

// Derived class
class Rectangle: public Shape {
  public:
  int getArea() { 
    return (width * height); 
  }
};

int main(void) {
  Rectangle Rect;
  Rect.setWidth(5);
  Rect.setHeight(7);
  // Print the area of the object.
  cout << "Total area: " << Rect.getArea() << endl;

  return 0;
}
Output
Total Objects: 1
Total area: 35
Access Control and Inheritance
class derived-class: access-specifier base-class

A derived class can access all the non-private members of its base class. Thus base-class members that should not be accessible to the member functions of derived classes should be declared private in the base class.
Following table shows different levels of access permissions of who can access what:

Accesspublicprotectedprivate
Same classyesyesyes
Derived classesyesyesno
Outside classesyesnono
A derived class can inherit all base class methods except:

  • Constructors, destructors and copy constructors of the base class.
  • Overloaded operators of the base class.
  • The friend functions of the base class.
Types of Inheritance
  • Single Inheritance – In this type of inheritance one derived class inherits from only one base class. It is the most simplest form of Inheritance.
  • Multiple Inheritance – In this type of inheritance a single derived class may inherit from two or more than two base classes.
  • Hierarchical Inheritance – In this type of inheritance, multiple derived classes inherits from a single base class.
  • Multilevel Inheritance – In this type of inheritance the derived class inherits from a class, which in turn inherits from some other class. The Super class for one, is sub class for the other.
  • Hybrid Inheritance (also known as Virtual Inheritance) – Hybrid Inheritance is combination of Hierarchical and Mutilevel Inheritance.

 

types of inheritance in c++

Watch it on YouTube

One thought on “Inheritance in C++ | Types of Inheritance

  • May 28, 2018 at 6:24 am
    Permalink

    Thanks for sharing excellent informations. Your website is very cool. I’m impressed by the details that you’ve on this web site. It reveals how nicely you perceive this subject. Bookmarked this website page, will come back for more articles. You, my friend, ROCK! I found just the info I already searched all over the place and simply couldn’t come across. What a perfect web site.

    Reply

Leave a Reply

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