Inheritance in C++

Advertisements

Prev Tutorial Next Tutorial

Inheritance in C++

The process of obtaining the data members and methods from one class to another class is known as inheritance. It is one of the fundamental features of object-oriented programming.

Important points

  • In the inheritance the class which is give data members and methods is known as base or super or parent class.
  • The class which is taking the data members and methods is known as sub or derived or child class.

Syntax

class subclass_name : superclass_name  
{  
    // data members
   // methods  
}  

Real Life Example of Inheritance in C++

The real life example of inheritance is child and parents, all the properties of father are inherited by his son.

real life example of inheritance in c++

Diagram

inheritance in C++

In the above diagram data members and methods are represented in broken line are inherited from faculty class and they are visible in student class logically.

Advantage of inheritance

If we develop any application using this concept than that application have following advantages,

  • Application development time is less.
  • Application take less memory.
  • Application execution time is less.
  • Application performance is enhance (improved).
  • Redundancy (repetition) of the code is reduced or minimized so that we get consistence results and less storage cost.

Tpyes of Inheritance

Based on number of ways inheriting the feature of base class into derived class it have five types they are:

  • Single inheritance
  • Multiple inheritance
  • Hierarchical inheritance
  • Multiple inheritance
  • Hybrid inheritance

Single inheritance

In single inheritance there exists single base class and single derived class.

single inheritance

Multiple inheritances

In multiple inheritances there exists single base class, single derived class and multiple intermediate base classes.

Single base class + single derived class + multiple intermediate base classes.

Intermediate base classes

An intermediate base class is one in one context with access derived class and in another context same class access base class.

multilevel inheritance

Hence all the above three inheritance types are supported by both classes and interfaces.

Multiple inheritance

In multiple inheritance there exist multiple classes and singel derived class.

multiple inheritance

Hybrid inheritance

Combination of any inheritance type

hybrid inheritance

Inheriting the feature from base class to derived class

In order to inherit the feature of base class into derived class we use the following syntax

Syntax

class classname-2 : classname-1
{
variable  declaration;
method declaration;
}

Explanation

  • classname-1 and classname-2 represents name of the base and derived classes respectively.
  • : is operator which is used for inheriting the features of base class into derived class it improves the functionality of derived class.

Example of Inheritance in C++

#include<iostream.h>
#include<conio.h>

class employee
{
public:
int salary;
};
class developer : public employee
{
employee e;
public:
void salary()
{
cout<<"Enter employee salary: ";
cin>>e.salary;   // access base class data member
cout<<"Employee salary: "<<e.salary;
}
};

void main()
{
clrscr();
developer obj;
obj.salary();
getch();
}

Output

Enter employee salary: 50000
Employee salary: 50000

Prev Tutorial Next Tutorial

Google Advertisment

Buy This Ad Space @$20 per Month, Ad Size 600X200 Contact on: hitesh.xc@gmail.com or 9999595223

Magenet is best Adsense Alternative here we earn $2 for single link, Here we get links ads. Magenet

For Projects 9999595223

Google Advertisements


Buy Websites 9999595223

Buy College Projects with Documentation Contact on whatsapp 9999595223. Contact on: hitesh.xc@gmail.com or 9999595223 Try this Keyword C++ Programs

Advertisements