Nothing Special   »   [go: up one dir, main page]

OOP-Lec-9 (Multi Level Inheritance)

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 28

CS-112

Object Oriented Programming


(3+1)
Prerequisites:
Programming Fundamentals
SYED MUHAMMAD RAFI
LECTURER, DEPARTMENT OF SOFTWARE ENGINEERING
FACULTY OF ENGINEERING SCIENCE AND TECHNOLOGY,
ZIAUDDIN UNIVERSITY
Multi Level Inheritance
Lec-9
Multilevel Inheritance
• A type of inheritance in which a class is derived from another derived class is
called multilevel inheritance.
• In multilevel inheritance, the members of parent class are inherited to the child
class and the member of child are inherited to grand child class.
• In this way, the members of parent class and child class are combined in grand
child class.
Example
class A
{
Body of the class
};
class B:public A
{
Body of the class
};
class C : public B
{
Body of the class
};
EXAMPLE class B :public A class C : public B
#include <iostream>
{
using namespace std; {
private:
class A private: int c;
int b; void in()
{ {
private: public: B::in();
void in() cout<<"Enter c: ";
int a;
cin>>c;
public: { }
void in() A::in(); void out()
{
{ cout<<"Enter b:";
B::out();
cout<<"Enter a: "; cin>>b; cout<<"The value of c is "<<c<<endl;
} }
cin>>a; };
} void out() int main()
void out() { {
A::out(); C obj;
{
obj.in();
cout<<"The value of a is"<<a<<endl; cout<<"The Value of b is"<<b<<endl;
obj.out();
} } }
}; };
How Above Program Works
• The above program declares three classes.
• The class A is the parent class and class B is the child class of A.
• The class C is the child class of B.
• The program declares an object obj of class C.
• When obj calls in() function, the control moves to in() declared in class C.
• The following statement declared in in() function of C moves control to class B:
B::in();
Similarly, following statement declared in in() function of B moves control to class
B:
A::in();
Multilevel Inheritance with Parameters
• The member functions in multilevel inheritance can pass the value to the
member functions of parent classes.
• When a function is overridden, the member function in child class calls the
member function in parent class.
• It can also send parameter values to the parent class functions.
• Write a program that declare three classes A,B &C. The class A is Parent class.
Class B is child of class A. Class C is child of class B. In class A declare a data
member a of integer type . Also declare member function In() to enter a from
keyboard. And member function Out() to display the value of a.
• In inherited class B declare a data member b of integer type. Also override
member function In() to enter b from keyboard. And override member
function Out() to display the value of b.
• In inherited class C declare a data member s of integer type. Also override
member function In() to enter c from keyboard. And override member function
Out() to display the value of c. The program declares an object obj of class C.
Call the functions In() and Out() through object obj of Class C.
#include <iostream> class B : public A class C : public B
int main()
{ {
using namespace std; {
private:
class A private: C obj;
int c;
int b; obj.set(1,2,3);
{ public:
obj.out();
public: void set(int g,int h,int k)
private: }
{
int a; void set(int m , int n)
B::set(g,h);
public: { c=k;
A::set(m); }
void set(int x) void out()
{ b=n; {
a=x; } B::out();
void out() cout<<"The value of c is"<<c<<endl;
} }
void out() { };
{ A::out();

cout<<"The value of a cout<<"The values of b is "<<b<<endl;


is"<<a<<endl; }
} };
};
How Above Program Works
• The above program declares three classes with multilevel inheritance.
• The member function set in class A accepts one integer to set the value of data
member a.
• The member function set in class B is overloaded and accept two integers as
parameter.
• The first parameter is passed to set function of class A and second parameter is
used to set the value of data member b.
• The member function set in class C is again overloaded and accepts three
integers as parameters.
• The first two parameters are passed to set function of class B and third
parameter is used to set the value of data member c.
Multiple Inheritance
• A type of inheritance in which a derived class inherit multiple base classes is
known as multiple inheritance.
• In multiple inheritance the derived class combine the members of all base
classes.
Syntax
The syntax of multiple inheritance is as follows:
class child_class : spec parent_class1,spec parent_class2….
{
body of class
}
Where,
class It is the keyword that is used to declare a class.
child_ class It is the name of derived class.
spec It is the access specifier that is used to define the type of
inheritance.
parent_class1 It is the name of first parent class to be inherited.
parent_class2 It is the name of the second parent class to be inherited.
Example
class A
{
body of class
};
Class B
{
body of class
};
Class C : public A, public B
{
body of the class
}
• In the above example, A and B are two independent classes.
• The class C inherits both A and B.
• The members of A and B are combined in C.
class B class C : public A,public B int main()
#include <iostream>
{ { {
C obj;
using namespace std; private: obj.get();
int b; private: obj.show();
class A
public: int c; }
{
void input() public:
private:
{ void get()
int a;
cout<<"Enter b:"; {
public:
cin>>b; A::in();
void in()
} B::input();
{
void output() cout<<"Enter c:";
cout<<"Enter a:";
{ cin>>c;
cin>>a;
cout<<"The value of b is"<<b<<endl; }
}
} void show()
void out()
}; {
{
A::out();
cout<<"The values of a is
"<<a<<endl; B::output();
} cout<<"The value of c is"<<c<<endl;
}; }
};
How above program works
• The above programs declares three classes.
• The class A and B are independent classes and C is child class that inherits both
A and B.
• The program declares an object of class C.
• The program calls gets() and show() functions of class C.
• The gets() function of C calls in() function of A and input() function of B.
• Similarly show() function of C calls out() function of A and output() function of
B.
Constructors in Multiple Inheritance
• In multiple inheritance, the constructors in the base classes and child classes
are executed when an object of derived class is created.
• The constructor may accept parameters or they can be without parameters.
1. Constructor without Parameters
• The constructor without parameters can be called from derived class by writing
a colon after derived class constructor and name of constructor in parent class.
#include <iostream>
class C : public A, public B
using namespace std; {
class A
{
public:
public: C() : B(), A()
A()
{
{
cout<<"Constructor of class A..."<<endl; cout<<"Constructor in class
} C..."<<endl;
};
}
class B };
{
public: int main()
B()
{
{
cout<<"Constructor of class B..."<<endl; C obj;
}
}
};
2. Constructor with Parameters
• The constructor with parameters can be called from derived class by writing
colon after derived class constructor in parent class.
• The required values for the parameters are also provided in call to the parent
class constructors.
# include <iostream> class B class C :public A, public B int main()
using namespace std; { { {
private: C obj(1,2,3);
class A private: int c; obj.showC();
{ int b; public: }
private: public: C():B(),A()
int a; {
B() c=0;
public:
{ }
A()
b=0; C(int x,int y, int z) : A(x),B(y)
{ {
a=0;
}
c=z;
} B(int n) }
{ void showC()
A(int n)
{
{ b=n;
A::showA();
a=n; } B::showB();
} void showB() cout<<"c= "<<c<<endl;
void showA() }
{
};
{ cout<<"b=
cout<<"a= "<<a<<endl; "<<b<<endl;
} }
}; };
Ambiguity in Multiple Inheritance
• The important issue in multiple inheritance is the issue of ambiguity.
• The ambiguity is created in multiple inheritance if the names of the functions
are similar in two or more parents.
• The compiler cannot determine which function to execute, when the object of
derived class attempts to execute such function.
#include <iostream> class C: public A, public B
using namespace std; {
class A
public:
{
public: void show()
void show() {
{
cout<<"Class C"<<endl;
cout<<"Class A"<<endl;
} }
}; };
class B
{ int main()
public: {
void show() C obj;
{
cout<<"Class B"<<endl; obj.show();
} }
};
How above Program Works
• The above program declares three classes.
• The class A and B both has the member function show().
• The class C simply inherits both classes.
• The program declares an object of class C.
• The object has two functions with same name show().
• One function comes from A and second comes from class B.
• The compiler will generate an error when the above program is compiled.
Removing Ambiguity
• The ambiguity can be removed from the above program as follows:
• obj.A::show();
• obj.B::show();

Another way to remove the ambiguity in the multiple inheritance is to overload


both functions in derived class as follows:
#include <iostream> class C: public A, public B
using namespace std; {
class A public:
{
public: void show()
void show() {
{ A::show();
cout<<"Class A"<<endl;
B::show();
}
}; cout<<"Class C"<<endl;
class B }
{ };
public:
void show() int main()
{ {
cout<<"Class B"<<endl; C obj;
}
obj.show();
};
}
Containership
• A technique in which a class contains an object of another class as its member is
called containership.
• This type of relationship between classes is also called has-a relationship.
• It means that one class has an object of another class as its member.
End of lecture

You might also like