Upload
miranda-porter
View
216
Download
3
Embed Size (px)
Citation preview
Inheritance
2
InheritanceThe process of deriving new class from the
existing one is called inheritanceThen the old class is called base class and the new
class is called derived class or sub classThe derived class inherits some or all properties
of base classAdvantages
Reusability of the code to increase the reliability of the code
to add some enhancements to the base class
3
InheritanceDefining derived classSyn:class derived class : visibility mode base class name{};: indicates the derived class is derived from the base
class nameVisibility mode optional by default private
visibility mode specifies the features of the base class are privately derived or publicly derived or protected derived
4
InheritanceThe derivations are three types
1 public2 private3 protected
5
InheritancePublic inheritanceIf the inheritance is public then The private members are not inherited The public members of base class becomes public in derive classThe protected members of base class becomes protected in derived class
class A{
private: int a;protected: int b;public: int c;
};Class B: public A{
private: int x;};
The member accessible in derive class B are
b protectedc publicx private
6
InheritancePrivate inheritanceIf the inheritance is private then The private members are not inherited The public members of base class becomes private in derive classThe protected members of base class becomes private in derived class
class A{
private: int a;protected: int b;public: int c;
};class B: private A{
private: int x;};
The member accessible in derive class B areb privatec privatex private
7
InheritanceProtected inheritanceIf the inheritance is protected then The private members are not inherited The public members of base class becomes protected in derive classThe protected members of base class becomes protected in derived class
class A{
private: int a;protected: int b;public: int c;
};class B: protected A{
private: int x;};
The member accessible in derive class B are
b protectedc protectedx private
8
InheritanceVisibility of inherited members
Base class
Derived class
Public Private Protected
Derivation Derivation Derivation
Private Not Inherited Not Inherited Not Inherited
Protected Protected Private Protected
Public Public Private Protected
9
InheritanceClass A
PrivateProtectedPublic
10
InheritanceClass A
PrivateProtectedPublic
Class B : public APrivateProtectedPublic
Cannot inheritCannot inherit
11
InheritanceClass A
PrivateProtectedPublic
Class B : public APrivateProtectedPublic
Cannot inheritCannot inherit
12
InheritanceClass A
PrivateProtectedPublic
Class B : public APrivateProtectedPublic
Class C : private APrivateProtectedPublic
12
Cannot inheritCannot inherit
13
InheritanceClass A
PrivateProtectedPublic
Class B : public APrivateProtectedPublic
Class C : private APrivateProtectedPublic
13
Cannot inheritCannot inherit
14
InheritanceClass A
PrivateProtectedPublic
Class B : public APrivateProtectedPublic
Class C : private APrivateProtectedPublic
Class D : protected CPrivateProtectedPublic
Cannot inheritCannot inherit
15
InheritanceClass A
PrivateProtectedPublic
Class B : public APrivateProtectedPublic
Class C : private APrivateProtectedPublic
Class D : protected CPrivateProtectedPublic
Cannot inheritCannot inherit
16
InheritanceClass A
PrivateProtectedPublic
Class B : public APrivateProtectedPublic
Class C : private APrivateProtectedPublic
Class D : protected CPrivateProtectedPublic
Class E: private B,public CPrivateProtectedPublic
Cannot inheritCannot inherit
17
InheritanceClass A
PrivateProtectedPublic
Class B : public APrivateProtectedPublic
Class C : private APrivateProtectedPublic
Class D : protected CPrivateProtectedPublic
Class E: private B,public CPrivateProtectedPublic
Cannot inheritCannot inherit
18
InheritanceTypes of InheritanceThere are five different inheritances supported in C++:(1) Simple / Single(2) Multiple(3) Hierarchical(4) Multilevel(5) Hybrid
19
InheritanceSingle Inheritance
A
B
20
InheritanceMultiple Inheritance
C
A B
21
InheritanceHierarchical Inheritance
A
B C
22
InheritanceMulti level Inheritance
B
C
A
23
InheritanceHybrid Inheritance
Combination of Hierarchical & Multiple A
B C
D
24
Inheritance
25
Inheritance
26
Inheritance
27
Inheritance