Multiple Inheritance

BhavyaJain137 523 views 21 slides May 29, 2021
Slide 1
Slide 1 of 21
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21

About This Presentation

Multiple inheritances are the type of inheritance in object-oriented programming in C++.


Slide Content

1 INHERITANCE Multiple Inheritance Harsh wadhwani 19CS40 COMPUTER SCIENCE(C-2) Bhavya Jain 19CS19 COMPUTER SCIENCE(C-1) .

General synopsis 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 by other class is known as parent class, super class or base class. What is child class ? A class that inherits another class is known as child class , derived class or subclass . 3 Animal Dog

Multiple inheritance 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

S yntax : 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

E xample : 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 S olution 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 [email protected]

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