27
Inheritance

Inheritance. 2 The process of deriving new class from the existing one is called inheritance Then the old class is called base class and the new class

Embed Size (px)

Citation preview

Page 1: Inheritance. 2 The process of deriving new class from the existing one is called inheritance Then the old class is called base class and the new class

Inheritance

Page 2: Inheritance. 2 The process of deriving new class from the existing one is called inheritance Then the old class is called base class and the new class

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

Page 3: Inheritance. 2 The process of deriving new class from the existing one is called inheritance Then the old class is called base class and the new 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

Page 4: Inheritance. 2 The process of deriving new class from the existing one is called inheritance Then the old class is called base class and the new class

4

InheritanceThe derivations are three types

1 public2 private3 protected

Page 5: Inheritance. 2 The process of deriving new class from the existing one is called inheritance Then the old class is called base class and the new class

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

Page 6: Inheritance. 2 The process of deriving new class from the existing one is called inheritance Then the old class is called base class and the new class

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

Page 7: Inheritance. 2 The process of deriving new class from the existing one is called inheritance Then the old class is called base class and the new class

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

Page 8: Inheritance. 2 The process of deriving new class from the existing one is called inheritance Then the old class is called base class and the new class

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

Page 9: Inheritance. 2 The process of deriving new class from the existing one is called inheritance Then the old class is called base class and the new class

9

InheritanceClass A

PrivateProtectedPublic

Page 10: Inheritance. 2 The process of deriving new class from the existing one is called inheritance Then the old class is called base class and the new class

10

InheritanceClass A

PrivateProtectedPublic

Class B : public APrivateProtectedPublic

Cannot inheritCannot inherit

Page 11: Inheritance. 2 The process of deriving new class from the existing one is called inheritance Then the old class is called base class and the new class

11

InheritanceClass A

PrivateProtectedPublic

Class B : public APrivateProtectedPublic

Cannot inheritCannot inherit

Page 12: Inheritance. 2 The process of deriving new class from the existing one is called inheritance Then the old class is called base class and the new class

12

InheritanceClass A

PrivateProtectedPublic

Class B : public APrivateProtectedPublic

Class C : private APrivateProtectedPublic

12

Cannot inheritCannot inherit

Page 13: Inheritance. 2 The process of deriving new class from the existing one is called inheritance Then the old class is called base class and the new class

13

InheritanceClass A

PrivateProtectedPublic

Class B : public APrivateProtectedPublic

Class C : private APrivateProtectedPublic

13

Cannot inheritCannot inherit

Page 14: Inheritance. 2 The process of deriving new class from the existing one is called inheritance Then the old class is called base class and the new class

14

InheritanceClass A

PrivateProtectedPublic

Class B : public APrivateProtectedPublic

Class C : private APrivateProtectedPublic

Class D : protected CPrivateProtectedPublic

Cannot inheritCannot inherit

Page 15: Inheritance. 2 The process of deriving new class from the existing one is called inheritance Then the old class is called base class and the new class

15

InheritanceClass A

PrivateProtectedPublic

Class B : public APrivateProtectedPublic

Class C : private APrivateProtectedPublic

Class D : protected CPrivateProtectedPublic

Cannot inheritCannot inherit

Page 16: Inheritance. 2 The process of deriving new class from the existing one is called inheritance Then the old class is called base class and the new class

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

Page 17: Inheritance. 2 The process of deriving new class from the existing one is called inheritance Then the old class is called base class and the new class

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

Page 18: Inheritance. 2 The process of deriving new class from the existing one is called inheritance Then the old class is called base class and the new class

18

InheritanceTypes of InheritanceThere are five different inheritances supported in C++:(1) Simple / Single(2) Multiple(3) Hierarchical(4) Multilevel(5) Hybrid

Page 19: Inheritance. 2 The process of deriving new class from the existing one is called inheritance Then the old class is called base class and the new class

19

InheritanceSingle Inheritance

A

B

Page 20: Inheritance. 2 The process of deriving new class from the existing one is called inheritance Then the old class is called base class and the new class

20

InheritanceMultiple Inheritance

C

A B

Page 21: Inheritance. 2 The process of deriving new class from the existing one is called inheritance Then the old class is called base class and the new class

21

InheritanceHierarchical Inheritance

A

B C

Page 22: Inheritance. 2 The process of deriving new class from the existing one is called inheritance Then the old class is called base class and the new class

22

InheritanceMulti level Inheritance

B

C

A

Page 23: Inheritance. 2 The process of deriving new class from the existing one is called inheritance Then the old class is called base class and the new class

23

InheritanceHybrid Inheritance

Combination of Hierarchical & Multiple A

B C

D

Page 24: Inheritance. 2 The process of deriving new class from the existing one is called inheritance Then the old class is called base class and the new class

24

Inheritance

Page 25: Inheritance. 2 The process of deriving new class from the existing one is called inheritance Then the old class is called base class and the new class

25

Inheritance

Page 26: Inheritance. 2 The process of deriving new class from the existing one is called inheritance Then the old class is called base class and the new class

26

Inheritance

Page 27: Inheritance. 2 The process of deriving new class from the existing one is called inheritance Then the old class is called base class and the new class

27

Inheritance