Home | | Object Oriented Programming and Data Structures | Various forms of inheritance in C++ with necessary coding

Chapter: Object Oriented Programming and Data Structure : Inheritance and Polymorphism

Various forms of inheritance in C++ with necessary coding

Explain the various forms of inheritance in C++ with necessary coding.

The various forms of inheritance in C++ with necessary coding.

INHERITANCE:

 

  Inheritance is a mechanism of deriving a new class from a old class.

  It provides the concept of reusability

  By inheritance some or all the properties of a class can be derived in to another class.

  The class which provides the properties is called as base class and the class which

 

derives the properties is called as derived class.

 

Types:

There are five types of inheritance viz

1. Single level inheritance

2. Multiple inheritance

3. Multilevel inheritance

4. Hybrid inheritance and

5. Hierarchical inheritance

 

Multiple inheritance:

It is a type of inheritance in which a class can inherit properties from more than one class.

 

Syntax:

Class derivedclass name : visibility mode baseclass1,visibilitmode baseclass2

{

 

body of the derived class;

};

 

visibility mode can be either private or public.

 

Example :

#include

 

#include

class bc1

{

 

protected:

 

int a; public :

 

void get()

{

 

cout <<”\n enter the value for a\n”; cin >>a;

}

};

 

class bc2

{

protected:

 

int b;

public;

void get1()

{

cout <<” \n enter the value for b \n”; cin>>b;

}

};

 

class dc : public bc1, public bc2

{

public :

 

void show()

{

 

cout <<”The values of a and b are” ; cout <<<”\n”<

}

};

 

void main()

{

 

clrscr();

dc d;

d.get();

d.get1();

d.show();

}

 

 

 

 

output:

 

Enter the value for a 20

 

Enter the value for b 30

 

The values of a and b are 20 30

 

Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail
Object Oriented Programming and Data Structure : Inheritance and Polymorphism : Various forms of inheritance in C++ with necessary coding |


Privacy Policy, Terms and Conditions, DMCA Policy and Compliant

Copyright © 2018-2024 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.