Q.4) Explain different types of inheritance with suitable examples of each type.
Ans.
Inheritance is the technique for creating new class (derived classes) from the existing
classes (base classes). Derived class inherits the some or all feature of base class.
C++ supports the following type of inheritance.
-Single Inheritance
-Multiple Inheritance
-Hierarchical Inheritance
-Multilevel Inheritance
-Hybrid Inheritance
-Multipath Inheritance
Public, Protected and Private
Inheritance in C++ Programming
In this article, you'll learn to use public, protected and private inheritance in C+
+. You'll learn where and how it is used, with examples.
You can declare a derived class from a base class with different access control,
i.e., public inheritance, protected inheritance or private inheritance.
#include <iostream>
using namespace std;
class base
.... ... ....
};
class derived : access_specifier base
.... ... ....
};
// Note: Either keyword public, protected or private
// is used in place of access_specifier.
Example of public, protected and private
inheritance in C++
class base
{
public:
int x;
protected:
int y;
private:
int z;
};
class publicDerived: public base
{
// x is public
// y is protected
// z is not accessible from publicDerived
};
class protectedDerived: protected base
{
// x is protected
// y is protected
// z is not accessible from protectedDerived
};
class privateDerived: private base
{
// x is private
// y is private
// z is not accessible from privateDerived
}
In the above example, we observe the following things:
base has three member variables: x, y and z which
are public, protected and private member respectively.
publicDerived inherits variables x and y as public and protected. z is not
inherited as it is a private member variable of base.
protectedDerived inherits variables x and y. Both variables become
protected. z is not inherited
If we derive a class derivedFromProtectedDerived from
protectedDerived, variables x and y are also inherited to the derived
class.
privateDerived inherits variables x and y. Both variables become
private. z is not inherited
If we derive a class derivedFromPrivateDerived from privateDerived,
variables x and y are not inherited because they are private variables of
privateDerived.
Accessibility in Public Inheritance
private protected public
Accessibility
variables variables variables
Accessible from own class? yes yes yes
Accessible from derived class? no yes yes
Accessible from 2nd derived
no yes yes
class?
Accessibility in Protected Inheritance
private protected
Accessibility public variables
variables variables
Accessible from own class? yes yes yes
yes
Accessible from derived
no yes (inherited as protected
class?
variables)
Accessible from 2nd
no yes yes
derived class?
Accessibility in Private Inheritance
private
Accessibility protected variables public variables
variables
Accessible from own
yes yes yes
class?
Accessible from derived no yes yes
class? (inherited as private (inherited as private
private
Accessibility protected variables public variables
variables
variables) variables)
Accessible from 2nd
no no no
derived class?
C++ Multiple Inheritance
In C++ programming, a class can be derived from more than one parents. For
example: A classBat is derived from base classes Mammal and WingedAnimal.
It makes sense because bat is a mammal as well as a winged animal.
Example 2: 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 Mammal {
public:
Mammal()
{
cout << "Mammals can give direct birth." << endl;
}
};
class WingedAnimal {
public:
WingedAnimal()
{
cout << "Winged animal can flap." << endl;
}
};
class Bat: public Mammal, public WingedAnimal {
};
int main()
{
Bat b1;
return 0;
}
Output
Mammals can give direct birth.
Winged animal can flap.
Ambiguity in Multiple Inheritance
The most obvious problem with multiple inheritance occurs during function
overriding.
Suppose, two base classes have a same function which is not overridden in
derived class.
If you try to call the function using the object of the derived class, compiler
shows error. It's because compiler doesn't know which function to call. For
example,
class base1
{
public:
void someFunction( )
{ .... ... .... }
};
class base2
{
void someFunction( )
{ .... ... .... }
};
class derived : public base1, public base2
{
};
int main()
{
derived obj;
obj.someFunction() // Error!
}
This problem can be solved using scope resolution function to specify which
function to class either base1 or base2
int main()
obj.base1::someFunction( ); // Function of base1 class is called
obj.base2::someFunction(); // Function of base2 class is called.
C++ Multilevel Inheritance
In C++ programming, not only you can derive a class from the base class but
you can also derive a class from the derived class. This form of inheritance is
known as multilevel inheritance.
class A
{
... .. ...
};
class B: public A
... .. ...
};
class C: public B
... ... ...
};
Here, class B is derived from the base class A and the class C is derived from
the derived classB.
Example 1: C++ 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 obj;
obj.display();
return 0;
}
Output
Base class content.
In this program, class C is derived from class B (which is derived from base
class A).
The obj object of class C is defined in the main() function.
When the display() function is called, display() in class A is executed. It's
because there is nodisplay() function in class C and class B.
The compiler first looks for the display() function in class C. Since the function
doesn't exist there, it looks for the function in class B (as C is derived from B).
The function also doesn't exist in class B, so the compiler looks for it in
class A (as B is derived from A).
If display() function exists in C, the compiler overrides display() of
class A (because of member function overriding).