Inheritance and its types explained.ppt

130 views 16 slides May 02, 2024
Slide 1
Slide 1 of 16
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

About This Presentation

inheritece and its types brief explaination ..


Slide Content

C++ Inheritance
PPT Presentation on topic
Prepared by:
Asst. prof. Nida Ansari

Oneofthemostimportantconceptsinobject-oriented
programmingisthatofinheritance.Inheritanceallowsus
todefineaclassintermsofanotherclass,whichmakesit
easiertocreateandmaintainanapplication.Thisalso
providesanopportunitytoreusethecodefunctionality
andfastimplementationtime.
Whencreatingaclass,insteadofwritingcompletelynew
datamembersandmemberfunctions,theprogrammer
candesignatethatthenewclassshouldinheritthe
membersofanexistingclass.Thisexistingclassiscalled
thebaseclass,andthenewclassisreferredtoas
thederivedclass.
C++ Inheritance

Inheritanceistheprocessbywhichnewclassescalled
derivedclassesarecreatedfromexistingclassescalled
baseclasses.
Thederivedclasseshaveallthefeaturesofthebase
classandtheprogrammercanchoosetoaddnew
featuresspecifictothenewlycreatedderivedclass.
C++ Inheritance

General Format for implementing the concept of Inheritance:
class derived_classname: access specifier
baseclassname
For example, if the baseclass is MyClassand the derived
class is sample it is specified as:
class sample: public MyClass
The above makes sample have access to both publicand
protectedvariables of base class MyClass
C++ Inheritance

public, private and protected access specifiers:
1Ifamemberorvariablesdefinedinaclassisprivate,then
theyareaccessiblebymembersofthesameclassonlyand
cannotbeaccessedfromoutsidetheclass.
2Publicmembersandvariablesareaccessiblefromoutside
theclass.
3Protectedaccessspecifierisastagebetweenprivateand
public.Ifamemberfunctionsorvariablesdefinedinaclass
areprotected,thentheycannotbeaccessedfromoutside
theclassbutcanbeaccessedfromthederivedclass.
C++ Inheritance

Inheritance Example:
class MyClass
{ public:
MyClass(void) { x=0; }
void f(int n1)
{ x= n1*5;}
void output(void) { cout<<x; }
private:
int x;
};
C++ Inheritance

Inheritance Example:
class sample: public MyClass
{ public:
sample(void) { s1=0; }
void f1(int n1)
{ s1=n1*10;}
void output(void)
{ MyClass::output(); cout << s1; }
private:
int s1;
};
C++ Inheritance

Inheritance Example:
int main(void)
{sample s;
s.f(10);
s.output();
s.f1(20);
s.output();
}
The output of the above program is
50
200
C++ Inheritance

1. Single class Inheritance:
Singleinheritanceistheonewhereyouhavea
singlebaseclassandasinglederivedclass.
Types of Inheritance
Class Employee
Class Manager
It is a Base class (super)
it is a sub class (derived)

2. Multilevel Inheritance:
InMultilevelinheritance,aclassinheritsits
propertiesfromanotherderivedclass.
Types of Inheritance
Class A
Class B
it is a Base class (super) of B
it is a sub class (derived) of A
and base class of class C
Class C derived class(sub) of class B

3.MultipleInheritances:
InMultipleinheritances,aderivedclassinherits
frommultiplebaseclasses.Ithaspropertiesof
boththebaseclasses.
Types of Inheritance
Class A Class B
Base class
Class C Derived class

4. Hierarchical Inheritance:
InhierarchicalInheritance,it'slikeaninvertedtree.
Somultipleclassesinheritfromasinglebase
class.It'squiteanalogoustotheFilesystemina
unixbasedsystem.
Types of Inheritance
Class A
Class B Class CClass D

5.HybridInheritance:
Inthistypeofinheritance,wecanhavemixtureof
numberofinheritancesbutthiscangeneratean
errorofusingsamenamefunctionfromnoof
classes,whichwillbotherthecompilertohowto
usethefunctions.
Therefore,itwillgenerateerrorsintheprogram.
Thishasknownasambiguityorduplicity.
Ambiguityproblemcanbesolvedbyusingvirtual
baseclasses
Types of Inheritance

Types of Inheritance
Class A
Class B
Class D
Class C
5.HybridInheritance:
Tags