Inheritance in c++

Inheritance in c++

inheritance in c++

Inheritance in c++

Inheritance in c++ is described that Deriving a new from an existing class is known as inheritance. In inheritance, the existing class is known as the base class or parent class, and the new class is known as the derived class or child class.

 

Inheritance concept is basically used to reuse the data member and member function of one class in another class.

To implement inheritance the syntax is :

class child_class_name : visibility_mode parent_class_name

{

}

According to visibility mode inheritance is 3 types.

  1. private inheritance
  2. protected inheritance
  3. public inheritance

In c++ inheritance is basically 6 types:

  1. single inheritance
  2. multilabel inheritance
  3. multiple inheritance
  4. hierarchical inheritance
  5. hybrid inheritance
  6. multipath inheritance.

 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.

Program:-
#include <iostream>
using namespace std;
// base class
class Test
{
 public:

    Test()
    {
      cout << "Example of single constructor" << endl;
    }
};
class Demo: public Test
{

};
int main()
{  
Demo obj;
return 0;
}

Multiple Inheritance

In this type of inheritance, a single derived class may inherit from two or more than two base classes.

#include <iostream>
using namespace std;
class Test
{
public:
      Test()
         {
             cout << "I am in Test class" << endl;
         }
  };
class Demo
  {
        public:
        Demo()
        {
         cout<<"I am in demo class"<<endl;
        }
 };
class Myclass: public Demo,public Test
{
        public:
       Myclass()
       {
       cout<<"I am in Myclass"<<endl;
       }
};
int main()
{
Myclass obj;
return 0;
}

 

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 Superclass for one is a subclass for the other.

#include <iostream>
using namespace std;
 class Test
 {
   public:
      Test()
         {
             cout << "I am in Test class" << endl;
         }
  };
  class Demo: public Test
   {
        public:
        Demo()
        {
         cout<<"I am in demo class"<<endl;
        }
 };
class Myclass: public Demo
{
        public:
        Myclass()
        {
        cout<<"I am in Myclass"<<endl;
        }
};
 int main()
 {
Myclass obj;
return 0;
 }

Hybrid (Virtual) Inheritance

Hybrid Inheritance is the combination of Hierarchical and Multilevel Inheritance.

 

What is the visibility mode in inheritance?

Deriving a new class form an existing class is known as inheritance.

In inheritance, all the property of the parent is available for the child, but all the property is not accessible by the child class.

In inheritance visibility mode is used to decide the accessibility for the child class, not availability.

What is a virtual base class?

In multipath inheritance, data ambiguity will present. to avoid the ambiguity during multipath inheritance virtual base class is required.

class A{}

 

basicittopic

basicittopic

A Computer Science Study for IT students and people of IT community