simple notes Unit 4-Inheritance (2).pptx

riyanahameed04 13 views 32 slides Jul 11, 2024
Slide 1
Slide 1 of 32
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
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28
Slide 29
29
Slide 30
30
Slide 31
31
Slide 32
32

About This Presentation

c++


Slide Content

UNIT 4 INHERITANCE

Syllabus Inheritance- Defining derived classes-Visibility modes- Single , Multilevel, Multiple, Hierarchical and Hybrid inheritance Virtual base classes Abstract classes Constructors in derived classes- Nesting of classes.

What? Inheritance is the capability of one class to acquire properties and characteristics from another class. The class whose properties are inherited by other class is called the   Parent  or  Base  or  Super  class. And , the class which inherits properties of other class is called  Child  or  Derived  or  Sub  class.

Example

Adv : Reusability of code

Syntax c lass derivedclassname : visibility label baseclassname { }; Animal dog Eg : class dog : public Animal { }; Keyword p rivate/public/protected

Types C++ offers five types of Inheritance. They are: Single Inheritance Multiple Inheritance Hierarchical Inheritance Multilevel Inheritance Hybrid Inheritance (also known as Virtual Inheritance)

Single Inheritance

Multiple Inheritance

Multilevel Inheritance

Heirarchical Inheritance

Hybrid Inheritance

Visibility labels/modes – private/public Base class - demo private – a,b public- read() Derived class – demochild private – c,d public –display() Private Inheritance All public members of base class b ecome private in derived Public Inheritance All public members of base class b ecome public in derived Note:- Private members of base class not inherited – Data Hiding After Inheritance - Derived Class private – c,d,read () public –display() After Inheritance - Derived Class private – c,d public –read(), display()

Try to do- Example 1 c lass B p rivate a Public b g et_ab () g et_a () s how_a () class D p rivate c public m ul () d isplay()

Example 2 c lass B p rivate a Public b g et_ab () g et_a () s how_a () class D p rivate C public m ul () d isplay()

Making Private Member Inheritable If private data of base class need to be inherited Modifying private as public Problem:???? Data hiding violated So 3 rd visibility label – protected Limited purpose Can be accessed by base class and immediate derived class

Visibility labels/modes – private/public/protected Base class - demo private – a,b protected- c public- read() Derived class – demochild private – d,e protected- f public –display() Private Inheritance All public & protected members of base class b ecome private in derived Protected Inheritance All protected &public members of base class b ecome protected in derived Note:- Private members of base class not inherited – Data Hiding After Inheritance - Derived Class private – d,e,c,read () Protected - f public –display() After Inheritance - Derived Class private – d,e p rotected – f, c,read () public –display()

Base class - demo private – a,b protected- c public- read() Derived class – demochild private – d,e protected- f public –display() Public Inheritance All protected members of base class b ecome protected and public members become p ublic in derived After Inheritance - Derived Class private – d,e p rotected – f, c public –read(), display() Note:- Private members of base class not inherited – Data Hiding

Visibility Labels

Inheritance Visibility Mode

Single class A { }; c lass B:public A { }; Multiple Class A { }; Class B { }; Class c:public A,public B { }; Multilevel c lass A { }; c lass B:public A { }; c lass C:public B { }; Hierarchical Class A { }; Class B :public A { }; Class C:public A { }; Class D: public A { }; Hybrid Class A { }; Class B:public A { }; Class c:public A { }; Class D: public B ,public C { {;

Ambiguity Resolution in Inheritance Output Class

Virtual Base Class Hybrid Inheritance, Here all protected and public members of Grandparent class is inherited twice via Parent 1 and Parent 2 c lass to Child Class Ambiguity arises Can be avoided by making common base class as virtual base class

Example

Abstract Class Class that is not used to create objects Created to act as base class

Program Explanation Inheritance Programs

Constructors in Derived Class If any base class constructor take argmnt then there should be derived constructor Execution order Multiple Inheritance – order in declaration of derived class Multilevel Inheritance- order of Inheritance

General Form Derivedclassconstructor (arg1,arg2,… argn ): Base 1(arg1), Base 2(arg2) ……………. ……………. Base n( argn ) { } Eg : D( int a,int b, int c, int d): A ( inta , int b), B( int c) { d1=d; }

Example Program Output

Nesting

THANK YOU
Tags