Inheritance in C++ Programming - Part 3

Multilevel Inheritance and Multiple Inheritance in C++ Programming

Levels of Inheritance
In C++ programming, a class be can derived from a derived class which is known as multilevel inheritance. For example:
class A
{ .... ... .... };
class B : public A
{ .... ... .... };
class C : public B
{ .... ... .... };

In this example, class B is derived from class A and class C is derived from derived class B.

Example to Demonstrate the Multilevel Inheritance
#include <iostream>
using namespace std;

class A
{
public:
    void display()
    {
    cout<<"Base class content.";
    }
};

class B : public A
{

};

class C : public B
{

};
int main()
{
C c;
c.display();
return 0;
}

Output
Base class content.

Explanation of Program: In this program, class B is derived from A and C is derived from B. An object of class C is defined in main( ) function. When the display( ) function is called, display( ) in class A is executed because there is no display( ) function in C and B. The program first looks for display( ) in class C first but, can't find it. Then, looks in B because C is derived from B. Again it can't find it. And finally looks it in A and executes the codes inside that function.

If there was display( ) function in C too, then it would have override display( ) in A because of member function overriding.

Multiple Inheritance in C++
In C++ programming, a class can be derived from more than one parents. For example: A class Rectangle is derived from base classes Area and Circle.

Source Code to Implement Multiple Inheritance in C++ Programming
This program calculates the area and perimeter of an rectangle but, to perform this program, multiple inheritance is used. 

#include <iostream>
using namespace std;
class Area
{
    public:
        float area_calc(float l,float b)
        {
        return l*b;
        }
};

class Perimeter
{
    public:
        float peri_calc(float l,float b)
        {
        return 2*(l+b);
        }
};

/* Rectangle class is derived from classes Area and Perimeter. */
class Rectangle : private Area, private Perimeter
{
    private:
        float length, breadth;
    public:
        Rectangle() : length(0.0), breadth(0.0) { }
        void get_data( )
        {
        cout<<"Enter length: ";
        cin>>length;
        cout<<"Enter breadth: ";
        cin>>breadth;
        }
        float area_calc()
        {
        /* Calls area_calc() of class Area and returns it. */
        return Area::area_calc(length,breadth);
        }
        float peri_calc()
        {
        /* Calls peri_calc() function of class Perimeter and returns it. */ 
        return Perimeter::peri_calc(length,breadth);
        }
};

int main()
{
    Rectangle r;
    r.get_data();
    cout<<"Area = "<<r.area_calc();
    cout<<"\nPerimeter = "<<r.peri_calc();
    return 0;
}

Output
Enter length: 5.1
Enter breadth: 2.3
Area = 11.73
Perimeter = 14.8

Note: This program is intended to give you idea on how multiple inheritance works rather than the condition in which multiple inheritance is used.

Ambiguity in Multiple Inheritance
Multiple inheritance may be helpful in certain cases but, sometimes odd sort of problem encounters while using multiple inheritance. For example: Two base classes have functions with same name which is not overridden in derived class and if you write code to access that function using object of derived class, compiler shows error because, it cannot determine which function to call. Here is a code for this type of ambiguity in multiple inheritance 

class base1
{
    public:
        void some_function( )
        { .... ... .... } 
};
class base2
{
    void some_function( )
        { .... ... .... } 
};
class derived : public base1, public base2
{

};
int main()
{
    derived obj;

    /* Error because compiler can't figure out which function to call either same_function( ) of base1 or base2 .*/ 
    obj.same_function( ) 
}

But, this problem can be solved easily using scope resolution function to specify which function to class either base1 or base2.

int main()
{
obj.base1::same_function( ); /* Function of class base1 is called. */
obj.base2::same_function( ); /* Function of class base2 is called. */

No comments:

Post a Comment