Inheritance-Types Of Inheritance (Single MultiLevel Multiple)

Inheritance
Inheritance

Inheritance:-

The process of creating a new class from an old class with some additional feature is known as Inheritance.For example when Parent give birth to their children then children takes some feature of parent by default. In this situation Parent also don’t know what they have given to their children.

But in the concept of Inheritance , We can do as we want or according to our requirements. We can reuse the features of old class , as many as we want.

There are following Types of Inheritance.

  1. Single
  2. Multi Level
  3. Multiple
  4. Hierarchical
  5. Hybrid

SINGLE :-

  1. An inheritance in which a single class inherits the other one old class is known as Single Inheritance.This form of inheritance is implemented by defining two classes . One class which inherits other class  is known as Base Class/Parent Class/Super Class. The class that is old or reused  is known as Derived Class/Child Class/Sub Class.

Syntax,

class der_class_name: access_specifier  baseclass

{

……………………………………………

……………………………………………

};

Example,

#include<iostream.h>

#include<conio.h>

       class Acc

       {

        public:

        float sal = 5000;

};

      class Designer: public Acc {

         public:

         float gift = 500;

};

      void  main()

{

Designer p1;

cout<<“Sal: “<<p1.sal<<endl;

cout<<“Gift: “<<p1.gift<<endl;

}

Output:

Sal: 5000

Gift: 500

MULTI LEVEL  :-

An inheritance in which a class inherits the other old class, also the class that is inheriting a class will inherited by other class and so on , this type of Inheritance  is known as  Multi Level  Inheritance.This form  is implemented by defining more than two classes .

In other words  we can say , It is also possible to derive a class from an existing derived class . This form  is known as multilevel inheritance . It is implemented by defining atleast three classes .

Syntax,

class der_class1_name: access_specifier  baseclass1

{

……………………………………………

……………………………………………

};

class der_class2_name: access_specifier  der_class1_name

{

……………………………………………

……………………………………………

};

class der_class3_name: access_specifier  der_class2_name

{

……………………………………………

……………………………………………

};

Example,

#include<iostream.h>

#include<conio.h>

class person

{

char name [25];

long int pno;

public:

void read ()

{

cout<<” Enter name and pno = “;

cin>>name>>pno;

}

void display ()

{

cout<<” \nName = “<<name;

cout<<”\nPhone number = “<<pno;

}

};

class student: public person

{

int rno;

char course [25];

public:

void read()

{

person:: read ();

cout<<”Enter rno and course =”;

cin>>rno >>course;

}

void display ()

{

person:: display();

cout<<” \n rno = “<<rno;

cout<<” \n course = “<<course;

}

};

class exam: public student

{

int m[5];

double per;

public :

void read ();

void cal ();

void display ();

};

void exam: read()

{

student :: read () ;

cout<<” Enter marks:”;

for(int i=0; i<4; i++)

cin>>m[i];

}

void exam:: cal ()

{

int totmarks=0;

for(int i=0; i<4; i++)

totmarks += sum[i];

per=double (totmarks)/4;

}

void exam:: display()

{

student:: display ();

cout<<” nMarks : “;

for(int i=0; i<4; i++)

cout<<” \n Percentage = “<<per;

}

void main()

{

clrscr ();

exam e1;

e1.read ();

e1.cal ();

e1.dispaly();

getch();

}

MULTIPLE :-

An inheritance in which a class inherits the other two or more  old classes, is known as  Multiple Inheritance.This form of inheritance is implemented by defining more than two classes .

In this  more than one base classes  and one derived class is available.

In other words  we can say , It is also possible to derive a class from more than one class . This form of inheritance is known as multiple inheritance . It is implemented by defining atleast three classes .

Syntax ,

 class der_class_name: access_specifier  baseclass1, access _specifier  baseclass2, ……………

{

……………………………………………

……………………………………………

};

Example,

#include<iostream.h>

#include<conio.h>

class mybase1

{

protected:

int x;

public:

void readx()

{

cout<<” enter value of x :”;

cin>>x;

}

void showx()

{

cout<<” x = “<<x;

}

};

class mybase2

{

protected :

int y;

public;

void ready()

{

cout<<” enter value of y: “;

cin>>y;

}

void showy()

{

cout<<” \ny = “<<y;

}

};

class der: public mybase1,public mybase2

{

int z;

public:

void add()

{

z=x+y;

}

void showz()

{

cout<<” \nz = “<<z;

}

};

void main()

{

clrscr();

der d1;

d1.readx ();

d1.ready();

d1.add ();

d1.showx();

d1.showy();

d1.showz();

getch();

}

HIERACHICAL :-

An inheritance in which more than one  class inherits the other one  old class, is known as  Hierachical Inheritance.This form of inheritance is implemented by defining more than two classes .In this  one base and more than one derived classes are available.

In other words  we can say , It is also possible to derive  more than one class from an one old class . This form  is known as Hierachical . It is implemented by defining atleast three classes .

 Syntax,

class der_class1_name: access_specifier  baseclass1

{

……………………………………………

……………………………………………

};

class der_class2_name: access_specifier  baseclass1

{

……………………………………………

……………………………………………

};

class der_class3_name: access_specifier  baseclass1

{

……………………………………………

……………………………………………

};

Example,

#include<iostream.h>

#include<conio.h>

using namespace std;

class Design

{

public:

int a;

int b;

void finddata(int n, int m)

{

a= n;

b = m;

}

};

class Rect : public Design

{

public:

int rectarea()

{

int result = a*b;

return result;

}

};

class Triangle : public Design

{

public:

int triarea()

{

float result = 0.5*a*b;

return result;

}

};

void main()

{

Rect r;

Triangle t;

int length,breadth,base,height;

std::cout << “Enter the length and breadth of a Rect: ” << std::endl;

cin>>length>>breadth;

r.finddata(length,breadth);

int m = r.rect_area();

std::cout << “Area of the Rect is : ” <<m<< std::endl;

std::cout << “Enter the base and height of the triangle: ” << std::endl;

cin>>base>>height;

t.finddata(base,height);

float n = t.triangle_area();

std::cout <<“Area of the triangle is : ”  << n<<std::endl;

getch();

}

Output:

Enter the length and breadth of a Rect:

23

20

Area of the Rect is : 460

Enter the base and height of the triangle:

2

5

Area of the triangle is : 5

You Also Can Visit For More Knowledge In C++:-

Visit Your YouTube  Channel:-  Think , About It

Visit Your Website :- www.atozlives.com

Thanks,

“Play With All Computer Science , Mathematics  And Technology.”

 

3 COMMENTS

LEAVE A REPLY

Please enter your comment!
Please enter your name here