1
Multiple Inheritance
Harsh wadhwani
19CS40
COMPUTER SCIENCE(C-2)
Bhavya Jain
19CS19
COMPUTER SCIENCE(C-
1)
.
• The capability of a class to derive
properties(the data members) and
functionality(the member functions)
from another class is
called Inheritance. Inheritance is one
of the most important key feature
of Object Oriented Programming.
What is parent class?
The class that is being inherited
inherited by other class is
class is known as parent class, super
class, super class or base class.
class.
What is child class?
A class that inherits another class is
class is known as child class, derived 3
Animal
Dog
In this type of inheritance a single
derived class may inherit from two or
more than two base classes. This means
that a single child class can have
multiple parent classes. Using comma
separation list
4
5
ILLUSTRATION
BIRD
A1 A2 A-n
Macaw
-
Species
SYNTAX :
Class subclass_name:
access_mode base_class1,
access_mode base_class2,
….;
{
//body of subclass
};
6
EXAMPLE:
#include <iostream>
using namespace std;
class bird {
public:
void type()
{
cout << " Aquatic flightless bird" << endl;
}
};
class adaptation {
public:
void life()
{
cout << "Adapted for life in the water" << endl;
}
};
class penguin: public bird, public adaptation{
};
int main()
{
penguin p1;
p1.type();
p1.life();
return 0;
}
7
Output :
CONSTRUCTORS CALLING :
• The constructors of Inherited classes
are called in the same order in which
they are inherited.
For example ,in the following programme
m mammal’s class constructor called before
c class winged animal’s constructor
8
EXAMPLE :
9
#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 :
PARAMETERISED AND DEFAULT CONSTRUCTORS :
#include <iostream>
using namespace std;
class A {
public:
A(){
cout<<" Default Constructor of A class"<<endl;}
A(int x){
cout<<" Parameterized Constructor of A class"<<endl;
}
};
class B {
public:
B(){
cout<<" Default Constructor of B class"<<endl;}
B(int g){
cout<<" Parameterized Constructor of B class"<<endl;
}
};
10
class C: public A, public B {
public:
C(){
cout<<" Default Constructor of C class"<<endl;
}
C(int z):A(z),B(z){
cout<<" Parameterzied Constructor of C class"<<endl;
}
};
int main() {
C obj(20);
return 0;
}
Output:
AMBIGUITY IN MULTIPLE INHERITANCE :
• What if same function is present in both the parent classes??
11
If we 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
{ public:
void someFunction( ) { .... ... .... } };
class derived : public base1, public base2 {
};
int main()
{ derived obj;
obj.someFunction() ; // Error!
return 0;
}
ONE POSSIBLE SOLUTION IS :
12
• The above issue can
be resolved by using
the Scope resolution
operator with the
function or we can say
explicitly calling
int main()
{
obj.base1::someFunction( );
// Function of base1 class is called
obj.base2::someFunction();
// Function of base2 class is called.
return0;
}
DIFFERENCE BETWEEN
MULTIPLE INHERITANCE AND MULTILEVEL INHERITANCE
13
Multiple Inheritance Multilevel Inheritance
“Multiple Inheritance” refers to the
concept of one class extending(or
inherits)
More than one base class
In this type of inheritance, a derived
class is created from another
derived class.
14
15
16
First Second Last
17
Link :
18
THANKS!
Any questions?
You can find me at:
⊚ @username
⊚ user@mail.me
SlidesCarnival icons are editable shapes.
This means that you can:
⊚ Resize them without losing quality.
⊚ Change fill color and opacity.
⊚ Change line color, width and style.
Isn’t that nice? :)
Examples:
19
Find more icons at slidescarnival.com/extra-
free-resources-icons-and-maps
20
DIAGRAMS AND INFOGRAPHICS
✋👆👉👍👤👦👧👨👩👪💃🏃💑❤😂😉
😋😒😭👶😸🐟🍒🍔💣📌📖🔨🎃🎈🎨🏈
🏰🌏🔌🔑 and many more...
😉
You can also use any emoji as an icon!
And of course it resizes without losing quality.
How? Follow Google instructions
https://twitter.com/googledocs/status/730087240156643328
21

Multiple Inheritance

  • 1.
    1 Multiple Inheritance Harsh wadhwani 19CS40 COMPUTERSCIENCE(C-2) Bhavya Jain 19CS19 COMPUTER SCIENCE(C- 1) .
  • 2.
    • The capabilityof a class to derive properties(the data members) and functionality(the member functions) from another class is called Inheritance. Inheritance is one of the most important key feature of Object Oriented Programming.
  • 3.
    What is parentclass? The class that is being inherited inherited by other class is class is known as parent class, super class, super class or base class. class. What is child class? A class that inherits another class is class is known as child class, derived 3 Animal Dog
  • 4.
    In this typeof inheritance a single derived class may inherit from two or more than two base classes. This means that a single child class can have multiple parent classes. Using comma separation list 4
  • 5.
  • 6.
    SYNTAX : Class subclass_name: access_modebase_class1, access_mode base_class2, ….; { //body of subclass }; 6
  • 7.
    EXAMPLE: #include <iostream> using namespacestd; class bird { public: void type() { cout << " Aquatic flightless bird" << endl; } }; class adaptation { public: void life() { cout << "Adapted for life in the water" << endl; } }; class penguin: public bird, public adaptation{ }; int main() { penguin p1; p1.type(); p1.life(); return 0; } 7 Output :
  • 8.
    CONSTRUCTORS CALLING : •The constructors of Inherited classes are called in the same order in which they are inherited. For example ,in the following programme m mammal’s class constructor called before c class winged animal’s constructor 8
  • 9.
    EXAMPLE : 9 #include <iostream> usingnamespace 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 :
  • 10.
    PARAMETERISED AND DEFAULTCONSTRUCTORS : #include <iostream> using namespace std; class A { public: A(){ cout<<" Default Constructor of A class"<<endl;} A(int x){ cout<<" Parameterized Constructor of A class"<<endl; } }; class B { public: B(){ cout<<" Default Constructor of B class"<<endl;} B(int g){ cout<<" Parameterized Constructor of B class"<<endl; } }; 10 class C: public A, public B { public: C(){ cout<<" Default Constructor of C class"<<endl; } C(int z):A(z),B(z){ cout<<" Parameterzied Constructor of C class"<<endl; } }; int main() { C obj(20); return 0; } Output:
  • 11.
    AMBIGUITY IN MULTIPLEINHERITANCE : • What if same function is present in both the parent classes?? 11 If we 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 { public: void someFunction( ) { .... ... .... } }; class derived : public base1, public base2 { }; int main() { derived obj; obj.someFunction() ; // Error! return 0; }
  • 12.
    ONE POSSIBLE SOLUTIONIS : 12 • The above issue can be resolved by using the Scope resolution operator with the function or we can say explicitly calling int main() { obj.base1::someFunction( ); // Function of base1 class is called obj.base2::someFunction(); // Function of base2 class is called. return0; }
  • 13.
    DIFFERENCE BETWEEN MULTIPLE INHERITANCEAND MULTILEVEL INHERITANCE 13 Multiple Inheritance Multilevel Inheritance “Multiple Inheritance” refers to the concept of one class extending(or inherits) More than one base class In this type of inheritance, a derived class is created from another derived class.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
    18 THANKS! Any questions? You canfind me at: ⊚ @username ⊚ user@mail.me
  • 19.
    SlidesCarnival icons areeditable shapes. This means that you can: ⊚ Resize them without losing quality. ⊚ Change fill color and opacity. ⊚ Change line color, width and style. Isn’t that nice? :) Examples: 19 Find more icons at slidescarnival.com/extra- free-resources-icons-and-maps
  • 20.
  • 21.
    ✋👆👉👍👤👦👧👨👩👪💃🏃💑❤😂😉 😋😒😭👶😸🐟🍒🍔💣📌📖🔨🎃🎈🎨🏈 🏰🌏🔌🔑 and manymore... 😉 You can also use any emoji as an icon! And of course it resizes without losing quality. How? Follow Google instructions https://twitter.com/googledocs/status/730087240156643328 21