ArtsAutosBooksBusinessEducationEntertainmentFamilyFashionFoodGamesGenderHealthHolidaysHomeHubPagesPersonal FinancePetsPoliticsReligionSportsTechnologyTravel
  • »
  • Technology»
  • Computers & Software»
  • Computer Science & Programming

Inheritance in C++ Programming Language

Updated on February 19, 2017

Inheritance

Inheritance is one of the major concept in Object Oriented Programming world.Inheritance allows reuse and sharing of code in true sense.This helps the developer to save lot of time and effort.

Inheritance is probably the most powerful feature of OOPS.It is the process of creating the new class called Derived class from existing class called Base class.The derived class inherits all the features of base class but also it can add more beautiful features and improvements of its own.The base class is unchanged by this process.The main advantage of inheritance is used to get re-usability i.e code re-usability.Revising existing code saves the time and money.

Reusability and Extensibility are the two most important features of Object oriented programming paradigm.It is always nice if we can reuse something that already exists instead of creating it fresh.

Base & Derived Classes

The base class is also called Ancestor class,Parent class or Super class.

A class which can be derived from more than one classes, which means it can inherit data and functions from multiple base classes is called derived class.

The class which is inherited from the base class or parent class is called Derived class,Descendant class or Sub class.

A derived class may itself be a base class from which additional classes can be derived.There is no specific limit on the number of classes that may be derived from one another,which forms a class hierarchy.

What happens when Classes are inherited?

  1. All non private variables and methods of base class/classes become part of the derived class.
  2. The variables and methods thus inherited are not required to be defined unless you want to override their behaviour or data.

Access Specifiers

Access Specifiers are the access control rules or the visibility modes.There are three types of access specifiers-:

  • Public Access Specifiers
  • Protected Access Specifiers
  • Private Access Specifiers

The access specifiers are used when we are inheriting the class.The access specifiers is optional and can be public,private or protected.Default access specifiers is private.The role of the access specifier is described below-:

  • When public access specifier is used,the public members of the base class remain public member of the derived class. Similarly the protected members of base class remains protected member of derived class.
  • When private access specifier is used,the public and protected members of base class become private members of derived class.
  • When protected access specifier is used the, public members of base class become protected members of the derived class and protected members of the base class remain protected members of the derived class.

Access Specifier Behaviour

Access Specifier
Accessible from own class
Accesible from derived class
Accessible from outside class
public
Yes
Yes
Yes
private
Yes
Yes
No
protected
Yes
No
No

Types of Inheritance

There are many ways to inherit from classes.Here are different types

  • Single Inheritance
  • Multiple Inheritance
  • Multilevel Inheritance
  • Hybrid Inheritance
  • Hierarchical Inheritance

Single Inheritance

In this kind of Inheritance,there is one base class and one derived class.When only one class is derived from a single base class,such derivation of a class is known as Single Inheritance.Further the derived class is not used as a base class for other class derivation.

class X
{
public:
void show()
{
cout<<"Base Class";
}
};
class Y:public X
{
public:
void display()
{
cout<<"Derived Class";
}
};
void main()
{
Y t;
t.show();
t.display;
}

Output:
Base Class
Derived Class

Multiple Inheritance

When a class is derived from two or more than two base classes the derived class inherits the properties of all the base classes than this type of derivation is known as Multiple Inheritance. The derived class is not used further in defining another class.

class A
{
public:
void show()
{
cout<<"Base Class A";
}
};
class B
{
public:
void show1()
{
cout<<"Base Class B";
}
};
class C:public A,public B
{
public:
void show2()
{
cout<<"Derived Class of Base classes A and B";
}
};
void main()
{
C g;
g.show();
g.show1();
g.show2();
}

Output:
Base Class A
Base Class B
Derived Class of Base classes A and B

Multilevel Inheritance

When a class is derived from a single base class and further can be used as the base class for deriving another class.This derivation can be continued to any level.

class X
{
public:
void show()
{
cout<<"Base Class X";
}
};
class Y:public X
{
public:
void show1()
{
cout<<"Derived Class Y of Base class X";
}
};
class Z:public Y
{
public:
void show2()
{
cout<<"Derived Class Z of Base class Y";
}
};
void main()
{
Z t;
t.show();
t.show1();
t.show2();
}

Output:
Base Class X
Base Class Y
Derived Class Z of Base class Y

Hierarchical Inheritance

When more than one class is derived from the same base class.Further the derived classes are not used as base class.

class W
{
public:
void show()
{
cout<<"Base Class";
}
};
class X:public W
{
public:
void show1()
{
cout<<"Derived class X of W";
}
};
class Y:public W
{
public:
void show2()
{
cout<<"Derived class Y of W";
}
};
class Z:public W
{
public:
void show3()
{
cout<<"Derived class Z of W";
}
};
void main()
{
X g;
g.show();
g.show1();

Y t;
t.show();
t.show2();

Z r;
r.show();
r.show3();
}

Output:
Base Class 
Derived class X of W
Base Class
Derived class Y of W
Base Class
Derived class Z of W

Hybrid Inheritance

When the class is derived involving two or more forms of inheritance than this type of derivation is known as Hybrid derivation. In this we can use single,multilevel and multiple inheritance.

class X
{
public:
void show()
{
cout<<"Base Class";
}
};
class Y:public X
{
public:
void show1()
{
cout<<"Derived class Y of X";
}
};
class W
{
public:
void show2()
{
cout<<"Base class W";
}
};
class Z:public Y,public W
{
public:
void show3()
{
cout<<"Derived class Z of Y and W";
}
};
void main()
{
Z g;
g.show();
g.show1();
g.show2();
g.show3();
}

Output:
Base Class
Derived Class Y of Base Class W
Derived Class Z of Y and W

Had you ever used the feature of Inheritance

See results

Comments

    0 of 8192 characters used
    Post Comment

    No comments yet.