Hierarchical Inheritance in C++

 Inheritance In C++




Inheritance is a process of creating new classes from existing classes. Existing class is known as base class and new class is known as derived class. The derived class is also called as sub class or child class and base class is called as super class or parent class. The derived class inherits all the features of base class .

The main advantages of of inheritance are

reusability

to increase the reliability of the code

to add more enhancements to the base class

Types of Inheritance:

Hierarchical  Inheritance:

In hierchical inheritance there is single base class and multiple derived classes and these derived classes are also inherited by some other classes. This is just like tree structure of family.

How to define Derived class:

The singly derived class is same as that of ordinary class.  Derived class consist following components,

the class keyword

the name of the derived class

a single colon

the type of derivation(private,public,protected)

the name of the base class or parent class

the remainder of the class definition


The general syntax for Multilevel Inheritance

Students -BBA 

Students-   BCA  

Students- MCA  

Students- MBA           

Students- Eng.

Students- MBA- HR 

Students- MBA- MKT

Students- MBA-  Fin

Students- Eng-IT  

Stufents- Eng- Mech  

Students- Eng-E&TC

In college there are different courses Student is the main base class where it is further derived into BBA, BCA, MCA, MBA and eng. MBA is further derived into HR, MkT, Fin and Eng.is further derived into IT, Mech, E&TC. 

The hierarchical inheritance is used the  cases where hierarchy has to be maintained. For example the any business or organization data base is maintained in hierarchy. Organization has different departments like HR, Account, Admin, IT, etc. 

The general syntax to demonstrate Hierarchical Inheritance

class students

{

private:

//data member

public:

//member functions

};


class BBA: public students

{

private:

//data member

public:

//member functions

};

class BCA : public students

{

private:

//data member

public:

//member functions

};

class MCA : public students

{

private:

//data member

public:

//member functions

};

class MBA : public students

{

private:

//data member

public:

//member functions

};

class Eng : public students

{

private:

//data member

public:

//member functions

};

class HR : public MBA

{

private:

//data member

public:

//member functions

};

class MKT : public MBA

{

private:

//data member

public:

//member functions

};

class Fin : public MBA

{

private:

//data member

public:

//member functions

};

class IT : public Eng

{

private:

//data member

public:

//member functions

};

class E&TC : public Eng

{

private:

//data member

public:

//member functions

};

class Mech : public Eng

{

private:

//data member

public:

//member functions

};

टिप्पणी पोस्ट करा

0 टिप्पण्या