0% found this document useful (0 votes)
3 views

Multiple and Hierarchical Inheritance

Uploaded by

jamiwasit0922
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Multiple and Hierarchical Inheritance

Uploaded by

jamiwasit0922
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 6

1: Multiple inheritance

2: Hierarchical inheritance
MUHAMMAD ABDUL JABBAR

1
Multiple Inheritance
 In this type of inheritance a single derived class may inherit from two or more than two base
classes.

 Syntax

class C: access_specifier A, access_specifier B

2
Example
#include <iostream> class Bat: public Mammal, public WingedAnimal
using namespace std; {
class Mammal {
public:
public:
Bat()
Mammal() {
{
cout << "Mammals can give direct birth." <<
endl; }}; cout<<"I am Bat"<<endl;}};
class WingedAnimal {
int main() {
public:
Bat b1;
WingedAnimal() {
return 0;}
cout << "Winged animal can flap." << endl; }};

3
Constructors in Multiple
Inheritance
 In C++, when dealing with multiple inheritance, the constructors of the base classes are called
in a specific order to ensure proper initialization.
 The constructors are invoked in the order in which the base classes appear in the inheritance
list of the derived class declaration.
 Here also default constructors will call automatically.
 Parameterized Constructors will call explicitly using Constructor Initialization List.

4
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.
Problem
class base1 {
 This problem can be solved using the scope resolution public:
void someFunction( ) {....} };
function to specify which function to class either base1 or class base2 {
base2. void someFunction( ) {....} };
Solution class derived : public base1, public base2 {};
int main() {
obj.base1::someFunction( ); // Function of base1 class is called int main() {
obj.base2::someFunction(); // Function of base2 class is called. derived obj;
} obj.someFunction() // Error! }

5
Hierarchical Inheritance
 Hierarchical inheritance is a type of inheritance in object-oriented programming where a single
derived class inherits from a single base class, but multiple derived classes can be created from
the same base class.
Each derived class forms a separate branch in the inheritance hierarchy.
 Syntax:
class BaseClass {
// Base class members and methods};
class DerivedClass1 : public BaseClass {
// Derived class 1 members and methods};
class DerivedClass2 : public BaseClass {
// Derived class 2 members and methods};

// More derived classes can be created from the same base class

You might also like