0
Posted December 7, 2012 by Spyros in C/C++ Programming
 
 

Understanding Inheritance in C++

Go_inheritance
Go_inheritance

C++ is an object oriented programming system and one its defining features is the ability to reuse code. Reusing a code is basically calling a certain function that has already been defined to pass an instruction. By calling a function that was defined earlier, you can save time and the code is compiled in a more efficient manner. Let’s get into the basics of inheritance in C++.

When a function is defined in C++, it is called as super class or base. When the super class is reused in a snippet, the class is known as sub class or derived class. Inheritances can be classified in to five types – single, multilevel, multiple, hierarchical and hybrid inheritance.

Single Inheritance – When the code has one base or super class and when it is called only once, then it is known as single inheritance.

Multilevel Inheritance – This is where the inheritance of the derived class is shared with another class, creating multiple levels.

Multiple Inheritance – This is similar to multilevel inheritance, but more than one super class is inherited by a derived class.

Hierarchical Inheritance – When a super class is inherited by multiple derived classes or when multiple super classes are inherited by a single derived class, it is called as Hierarchical Inheritance.

Let’s look at inheritance in C++ with an example (from http://www.learncpp.com/cpp-tutorial/112-basic-inheritance-in-c/ )

#include <string>
class Person
{
public:
    std::string m_strName;
    int m_nAge;
    bool m_bIsMale;
 
    std::string GetName() { return m_strName; }
    int GetAge() { return m_nAge; }
    bool IsMale() { return m_bIsMale; }
 
    Person(std::string strName = "", int nAge = 0, bool bIsMale = false)
        : m_strName(strName), m_nAge(nAge), m_bIsMale(bIsMale)
    {
    }
};

Here, the super class is defined to store the name, age and sex. These three parameters are common to all people. So, when you are in a derived class, you don’t have to define the name, the age or sex again and use the parameter from the super class, provided that it is defined as Public.
Suppose you want to write a program to get details about employees in an organization and if the program includes manipulations over the name, age and sex of the person, you can call the base class and let the instructions be passed to the derived class, like the example shown below:

class Employee
{
public:
    double m_dSalary;
    int m_nAttendance;
};

To inherit the Person super class, all you have to do is define the derived class and add the name of the super class using the syntax – class [derived class name]: public [super class name]

Example – class Employee : public Person

So if you want to inherit the Person class in the Employee class, the program will look something like this:

// Employee publicly inheriting Person
class Employee : public Person
{
public:
    double m_dSalary;
    int m_nAttendance;
 
    Employee(double dSalary = 0.0, int nAttendance = 0)
       : m_dSalary(dSalary), m_nAttendance(nAttendance)
    {
    }
};

Here, we have made the derived class inherit the base class without defining the parameters about the name, age or sex. In short, inheritance in C++ is the process of accessing the base class information from the derived class.

This guest post has been brought to you by Robin Mckenzie of Centurylink internet, a site that offers savings and current information on consumers’ broadband internet and cable.


Spyros