Inheritance in C++ | Types of inheritance | Examples of all inheritance | My CS Tutorial - My CS Tutorial

Breaking

Programming languages full tutorial and programs, exam papers, visual basic( Vb.net ), information of new technologies and more....

Sunday, July 26, 2020

Inheritance in C++ | Types of inheritance | Examples of all inheritance | My CS Tutorial

Inheritance in c++ | Types of inheritance | Single inheritance in c++ | Multiple inheritance in c++ | Multilevel inheritance | Hybrid inheritance in C++ | Hierarchy inheritance in C++ | My CS Tutorial


Inheritance in c++ | My CS Tutorial

A key feature of C++ classes is inheritance. Inheritance allows to create classes which are derived from other classes, so that they automatically include some of its "parent's" members, plus its own.

C++ Inheritance 


Inheritance is one of the feature of Object Oriented Programming System(OOPs),The capability of a class to derive properties and characterstics from another class is called inheritance.
Inheritance is one of the most important feature of OOPs.


CONTENT IN THIS POST:-


● C++ Inheritance
● Sub class
● Super class
● Modes of inheritance
● Advantages of inheritance
● Example of inheritance
● Types of inheritance
1) Single inheritance and example
2) Multilevel inheritance and example
3) Multiple inheritance and example
4) Hierarchical inheritance and example
5) Hybrid inheritance and example


What is child(sub) class:-


A class that inherits another class is known as child class, it is also known as derived class or subclass.

What is parent(super) class:-


The class that is being inherited by other class is known as parent class, super class or base class.

Syntax of Inheritance


class parent_class
{
    //Body of parent class
};
class child_class : access_modifier parent_class
{
   //Body of child class
};

Modes of Inheritance:-


【1】 Public mode :-    If we derived a subclass from a public base class, then the public member of the base class will become public in the derived class and protected member of the base class will became protected in derived class.

【2】 Protected mode :-   If we derive a subclass from a protected base class than both public member and protected member of the base class will become protected in derived class.

【3】 Private mode :-     If we derive a subclass from a private base class then both public member and protected member of the base class will become private in derived class.


The advantages of using inheritance in C++ Programming:-


The main advantages of inheritance are code reusability and readability. When child class inherits the properties and functionality of parent class, we need not to write the same code again in child class. This makes it easier to reuse the code, makes us write the less code and the code becomes much more readable.

Lets take a example to understand this: Lets assume that Human is a class that has properties such as height, weight, colour etc and functionality such as eating(), sleeping(), dreaming(), working() etc.
Now we want to create Male and Female class, these classes are different but since both Male and Female are humans they share some common properties and behaviours (functionality) so they can inherit those properties and functionality from Human class and rest can be written in their class separately.
This approach makes us write less code as both the classes inherited several properties and functions from base class thus we didn’t need to re-write them. Also, this makes it easier to read the code.


Inheritance Example


Before we discuss the types of inheritance, lets take an example:

Here we have two classes Teacher and MathTeacher, the MathTeacher class inherits the Teacher class which means Teacher is a parent class and MathTeacher is a child class. The child class can use the property collegeName of parent class.

Simple C++ program of inheritance


#include <iostream>
using namespace std;
class Teacher {
public:
  Teacher(){
    cout<<"Hey Guys, I am a teacher"<<endl;
  }
  string collegeName = "My CS Tutorial";
};
//This class inherits Teacher class
class MathTeacher: public Teacher {
public:
  MathTeacher(){
    cout<<"I am a Programming Teacher"<<endl;
  }
  string mainSub = "Programming";
  string name = "Chauhan";
};
int main() {
  MathTeacher obj;
  cout<<"Name: "<<obj.name<<endl;
  cout<<"College Name: "<<obj.collegeName<<endl;
  cout<<"Main Subject: "<<obj.mainSub<<endl;
  return 0;
}

Output:


Hey Guys, I am a teacher
I am a Programming Teacher
Name: Chauhan
College Name: My CS Tutorial
Main Subject: Programming


Types of Inheritance in C++


1) Single inheritance
2) Multilevel inheritance
3) Multiple inheritance
4) Hierarchical inheritance
5) Hybrid inheritance

【1】 Single inheritance

In Single inheritance one class inherits one class exactly.
OR
A class is allowed to inherit from only one class.

For example:

Lets say we have class A and B

B inherits A

Example of Single inheritance:

#include <iostream>
using namespace std;
class A {
public:
  A(){
     cout<<"Constructor of A class"<<endl;
  }
};
class B: public A {
public:
  B(){
     cout<<"Constructor of B class";
  }
};
int main() {
   //Creating object of class B
   B obj;
   return 0;
}

Output:


Constructor of A class
Constructor of B class

【2】 Multiple Inheritance

Multiple inheritance is a feature of C++.In multiple inheritance, a class can inherit more than one class. This means that in this type of inheritance a single child class can have multiple parent classes.

For example:

C inherits A and B both

Example of Multiple Inheritance:

#include <iostream>
using namespace std;
class A {
public:
  A(){
     cout<<"Constructor of A class"<<endl;
  }
};
class B {
public:
  B(){
     cout<<"Constructor of B class"<<endl;
  }
};
class C: public A, public B {
public:
  C(){
     cout<<"Constructor of C class"<<endl;
  }
};
int main() {
   //Creating object of class C
   C obj;
   return 0;
}

output:


Constructor of A class
Constructor of B class
Constructor of C class

【3】 Multilevel Inheritance

In this type of inheritance one class inherits another child class.
OR
A derived class is created from another derived class.

for example:

C inherits B and B inherits A

Example of Multilevel inheritance:

#include <iostream>
using namespace std;
class A {
public:
  A(){
     cout<<"Constructor of A class"<<endl;
  }
};
class B: public A {
public:
  B(){
     cout<<"Constructor of B class"<<endl;
  }
};
class C: public B {
public:
  C(){
     cout<<"Constructor of C class"<<endl;
  }
};
int main() {
  //Creating object of class C
  C obj;
  return 0;
}

Output:


Constructor of A class
Constructor of B class
Constructor of C class


【4】 Hierarchical Inheritance


In this type of inheritance, one parent class has more than one child class.
OR
More than one sub class is inherited from a single base class.(More than one derived class is created from a single base class.)

For example:

Class B and C inherits class A

Example of Hierarchical inheritance:

#include <iostream>
using namespace std;
class A {
public:
  A(){
     cout<<"Constructor of A class"<<endl;
  }
};
class B: public A {
public:
  B(){
     cout<<"Constructor of B class"<<endl;
  }
};
class C: public A{
public:
  C(){
     cout<<"Constructor of C class"<<endl;
  }
};
int main() {
   //Creating object of class C
   C obj;
   return 0;
}

Output:


Constructor of A class
Constructor of C class

【5】 Hybrid Inheritance


Hybrid inheritance,also called multipath inheritance.Hybrid inheritance is a combination of more than one type of inheritance. For example, A child and parent class relationship that follows multiple and hierarchical inheritance both can be called hybrid inheritance.


Example of Hybrid inheritance:

#include<iostream.h>
#include<conio.h>
class stu{ //First base Class//
int id;
char name[20];
public: //If not declared, data members are by default defined as private//

void getstu(){
cout << "Enter stuid, name";
cin >> id >> name;
}
};

class marks: public stu{//derived class//
protected: //without this command, data members will not be available next//
int m, p, c;// without ‘protected:’ command, m1, m2, & m3 are private members//
public:
void getmarks(){
cout << "Enter 3 subject marks:";
cin >> m >> p >> c;
}
};

class sports{//Second base class//
protected:
int spmarks;
public:
void getsports(){
cout << "Enter sports marks:";
cin >> spmarks;
}
};

class result : public marks, public sports{//Derived class by hybrid inheritance//
int tot;
float avg;
public :
void show(){}
tot=m+p+c;
avg=tot/3.0;
cout << "Total=" << tot << endl;
cout << "Average=" << avg << endl;
cout << "Average + Sports marks =" << avg+spmarks;
}
};

void main(){
result r;//object//
r.getstu();
r.getmarks();
r.getsports();
r.show();
getch();
};


Modes of inheritance in c++ | base class and derived class in c++ | Types of inheritance in c++ | example of single inheritance | example of multiple inheritance | example of multilevel inheritance | example of hybrid inheritance | example of hierarchy inheritance in c++ | My CS Tutorial

_______________________________________


Please share this post and blog link with your friends.For more programs use this blog.

If you have any problem, please comment in comment box, subscribe this blog for notifications of new post on your email and follow this blog.If you have any method of this tutorial or program or want to give any suggestion send email on hc78326@gmail.com

Created by-- HARSH CHAUHAN

No comments:

Post a Comment