You are on page 1of 12

OBJECT-ORIENTED lecture 11

PARADIGM Instructor : Syeda Hira Fatima


MULTILEVEL INHERITANCE
Multilevel Inheritance in C++ is the process of deriving a class from another
derived class. When one class inherits another class it is further inherited by another
class. It is known as multi-level inheritance.
For example, if we take Grandfather as a base class then Father is the derived class
that has features of Grandfather and then Child is the also derived class that is
derived from the sub-class Father which inherits all the features of Father.
UNMARKED LAB TASK
Create two classes first class as base1, 2nd as base2, third class as derived class
Now create Public, private and protected data members in both classes base1 and base2.
Base 2 is the derived class of base 1
First base 2 should publically inherit the base 1
2nd base 2 should privately inherit the base 1
3rd base 2 should protectedly inherit the base 1
Derived class will inherit base 2 using public visibility mode( it has only one member function
i.e Print which attempts tp acess different data members of base 1 and base 2)
Now change visibility modes and see the output and access of data members
MULTIPLE INHERITANCE
Multiple Inheritance is the concept of the Inheritance in C++ that allows a child
class to inherit properties or behaviour from multiple base classes. Therefore, we can
say it is the process that enables a derived class to acquire member functions,
properties, characteristics from more than one base class.
#include <iostream> using namespace std; class child_class : public Base_class, public
Base_class2
// create a base class1 {
class Base_class
// access specifier
{ public: public:
// It is a member function void display3() // It is a member function of
derive class
void display() {
cout << " It is the function of the derived class
{ cout << " It is the first function of the Base class " <<
endl; " << endl;
}};
}};
int main()
class Base_class2
{
{ public: // create an object for derived class
child_class ch;
void display2()
ch.display(); // call member function of
{ cout << " It is the second function of the Base class " Base_class1
<< endl; ch.display2(); // call member function of
Base_class2
}
ch.display3(); // call member function of
}; child_class
}
Ambiguity Problem in Multiple Inheritance
In Multiple Inheritance, when a single class is derived from two or more base or parent classes.
So, it might be possible that both the parent class have the same-named member functions, and it
shows ambiguity when the child class object invokes one of the same-named member functions.
Hence, we can say, the C++ compiler is confused in selecting the member function of a class for
the execution of a program.
#include <iostream> create a child class to inherit the member function of class A an
using namespace std; d class B
class child: public A, public B
class A {
{ public:
void disp()
public:
{
void show() cout << " It is the member function of the child class " <
< endl;
{ cout << " It is the member function of class A " << endl;}};
}
class B };
{ public:
int main ()
void show() {
{cout << " It is the member function of class B " << endl;}}; // create an object of the child class to access the member fun
ction
child ch;
ch.show(); // It causes ambiguity
ch.disp();
return 0;
}
we need to resolve the ambiguous problem in multiple Inheritance. The ambiguity problem can be
resolved by defining the class name and scope resolution (::) operator to specify the class from
which the member function is invoked in the child class.

You might also like