inheritance in c++

By | August 10, 2019

Inheritance in C++ -: Inheritance is the process by which object one class acquire the properties of object of another class. Inheritance support the concept of hierarchical classification. For example the bird ‘robin’ is the part of the class ” flying bird” which is again a part of the class “bird”. The principle behind this sort of division is divided class shares common characteristic with the class from which is derived as illustrated.

Reuse ability is the another important function of object oriented programming system. C++ also support the concept of reuse ability . The c++  classes can be reuse in several way.  Once a class has been written and tested . it can be used by other programmers to suite there  requirement. This is basically done by creating a new classes and reusing the properties of the existing one . This mechanism of deriving a new class from an old one is called inheritance.

In this process the old class referred the base class  and new one is called the derived class or subclass.

inheritance in c++

c++ inheritance

 

Types of Inheritance in c++ -: In the c++ programming language support many type inheritance these are the

  1. Single inheritance
  2. multilevel Inheritance
  3. Multiple Inheritance
  4. Hierarchical Inheritance
  5. Hybrid Inheritance

 

Single Inheritance in c++ -:  For understanding a single inheritance we take a example in this a base class B and a derived class D. The class B contain a private data member one public data member and three public member function. The class D contain one private data and two public member function.

Single Inheritance in C++  public class example:

#include <iostream>

Using namespace std;

class B

{

    int a;

public:


    int b;

    void get_ab();


    int get_a(void);


    void show_a(void);

};


class D : class B         //public derivation

{


   int c;

public:

   void mul(void);

   void display(void);

};

//--------------------------------------------

void B:: get_ab(void)
{
  a=5;
  b=10;
}

int B:: get_a()
{
  return a;
}
void B :: show_a()
{

  cout << "a = <<a <<"\n";

}
void D :: mul()
{

   c=b * get_a();

}

void D :: display()

{

   cout << "a =" << get_a() << "\n"; 
    cout << "b =" << b << "\n";
    cout << "c =" << c << "\n";

}

//-----------------------------------

int main()
{
 D d;
 d.get_ab();
 d.mul();
 d.show_();
 d.display();

 d.b=20;
 d.mul();
 d.display();
 
return 0;
}

output of this program

a=5
a=5
b=10
c=50

a=5
b=20
c=100

 

Multilevel inheritance in c++ : – It is not uncommon that a class is derived from another derived class. Multilevel inheritance represents a type of inheritance when a Derived class is a base class for another class

For example  we take a example of  family base class is a grandfather (A) and intermediate base class is a father(B) and derived class is a child (C). this type inheritance called the multilevel inheritance.

 

 

Example of multilevel inheritance 

#Include <iostream>

using namespace std;

class student

{ 
   protected:
  
     int rollnumebr;

  public:

     void getnumber(int);
    
     void putnumber(void);

};

void student :: getnumber(int a)

{

      rollnumber = a;

}

void student :: putnumber()

{

   cout <<"roll number: " << roll number <<"\n";
  
}

class test : public student    // first level derivation

{

 protected:

  float sub1;
  float sub2;

public:

     void getmarks (float, float);
     void putmarks (void);
};


void test :: getmarks(float x,float y)
{
  sub1= x;
  sub2=y;
}

void test :: putmarks()

{
  cout << "marks in SUB1=" << sub1 <<"\n";
  cout << "marks in SUB2=" << sub2 <<"\n";
}

class result : public test
{
 float total ;
public:
 void display(void);
};

void result :: display(void)
{
 total = sub1 + sub2 ;
 putnumber();
 putmarks();
cout << " total = " << total <<"\n" ;
}

int main ()
{
   result student1;
   student1 getnumber(78);
   student1 getmarks(75.0,85.5);
   student1 dispay();

 return();
}


The output of this program 

Roll number 78

marks in sub1=75

marks in sub2= 85.4

total = 160.4

 

 

Leave a Reply

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