Inheritance in C++

Submitted by: 
Language: 

Inheritance in C++

In this tutorial you will learn:
1. What is inheritance?
2. What are different types of inheritance?
3. Why to use inheritance?
4. C++ syntax

What is inheritance?
A form of software reuse in which you create a class that absorbs an existing class’s data and behavior and enhances them with new capabilities. Just like we are the children of our parents and we inherit certain characteristics of our parents, similarly in a program if we have more than one classes in a program , one class may be the base(parent) class and the other class may be the derived(child) class. Likewise two unrelated classes can be present in a single program.
Derived class is inherited from the parent class and it contains some specific features(data members or member functions) of the base class. It is usually not necessary that the derived class contain all the data members and the member function of the base class. It depends of the type of inheritance between the base and derived class which may be public, private or protected. Several classes can be inherited from the base class by changing the specifier. In some programs the inherited class can also act as a base class and that inherited class can also act as the base for the other classes. Inheritance saves time which we could waste by writing the code that has already been written and it is helpful in reusing the already made code which is the key feature of object oriented programming.

Note that private aspects of the base class are part of the child, but are not (directly) accessible within the derived class. C++ also provide a feature of deriving a class from two classes at the same time. Hence C++ supports multiple inheritance whereas java doesn’t.
Basic syntax for inheritance:
class nameOfDerivedClass : kind nameOfBaseClass
where
kind is one of public, private or protected.

Types of Inheritance:
There are three types of inheritance
• Public Inheritance
• Protected Inheritance
• Private inheritance

Public Inheritance:
It is the most commonly used type of inheritance. In public inheritance public and protected members of the base class remain, respectively, public and protected members of the derived class.
Syntax:
class nameOfDerivedClass : public nameOfBaseClass

style="display:block; text-align:center;"
data-ad-format="fluid"
data-ad-layout="in-article"
data-ad-client="ca-pub-3664195971165750"
data-ad-slot="8323940646">

Protected Inheritance:
Public and protected member of the base class are inherited as protected members of the derived class. A base class’s protected members can be accessed within that base class and by members of any classes derived from that base class. Offers an intermediate level of protection between public and private access

Syntax:
class nameOfDerivedClass : protected nameOfBaseClass

Private Inheritance:
Public and protected members of the base class are inherited as private members of the derived class.
Syntax:
class nameOfDerivedClass : private nameOfBaseClass

Why we should use Inheritance?
Inheritance allow us to use the already present code in the program, hence it saves our time and resources. Due to inheritance the reliability of the programs increases remarkably because when we need not to write the new code and use the preexisting code which has been tested already by the programmer there is lesser chance of error. Due to inheritance the data there is consistency of interface and rapid prototyping is achieved.


Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe>
  • You may insert videos with [video:URL]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.