29
Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 1 Haresh Jaiswal Rising Technologies, Jalna.

05. inheritance

Embed Size (px)

Citation preview

Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 1

Haresh Jaiswal

Rising Technologies, Jalna.

Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 2

Introduction Inheritance allows us to extend the definition of a class without

making any physical changes to the existing class.

Inheritance lets you create new classes reusing some featuresfrom existing class.

Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 3

Introduction Any new class that you create from an existing class is

called derived class & existing class is called as base class.

A

B

Base Class

Derived Class

Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 4

Introduction The inheritance enables a derived class to inherit features from

its base class. Furthermore, the derived class can add newfeatures of its own.

A

B

void show()

{

}

void show()

{

}

void print()

{

}

Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 5

Introduction Therefore, rather than to create a completely new class from

scratch, you can take advantage of inheritance and reducesoftware complexity.

A

B

void show()

{

}

void show()

{

}

void print()

{

}

Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 6

Inheritance

Class Bird

Non Flying BirdFlying Bird

Robin Swallow Penguin Kiwi

Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 7

Inheritance The concept of inheritance provides the idea of reusability. This

means that we can add additional features to an existing classwithout modifying it.

A

B

void show()

{

}

void show()

{

}

void print()

{

}

Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 8

Inheritance Inheritance is the process by which objects of one class acquire

properties of another class. It supports the concepts ofhierarchical classification.

Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 9

Forms/Types of Inheritance Single Inheritance

Multiple Inheritance

Multi-Level Inheritance

Hierarchical Inheritance

Hybrid Inheritance

Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 10

Single Inheritance It is the inheritance hierarchy wherein a derived class inherits

from only one base class.

Class A

Class B

Base Class

Derived Class

Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 11

Multiple Inheritance It is the inheritance hierarchy wherein a derived class inherits

from multiple base classes.

Class BClass A

Class C

Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 12

Multi Level Inheritance It is the inheritance hierarchy wherein subclass acts as a base

class for other classes.

Class B

Class A

Class C

Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 13

Hierarchical Inheritance It is the inheritance hierarchy wherein multiple subclasses inherit

from one base class.

Class B

Class A

Class C

Class D Class E

Class D

Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 14

Hybrid Inheritance The inheritance hierarchy that reflects any legal combination of

other four types of inheritance.

Class B

Class A

Class C

Class D

Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 15

Defining Derived Class

Derived class can be defined by specifying its relationship withthe base class in addition to its own details.

In order to derive a class from another, we use a colon (:) in thedeclaration of the derived class using the following format.

class Derived_Class : VisiblityModifier Base_Class

{

...

...

};

Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 16

Defining Derived Class

Where Derived_Class is the name of the derived class andBase_Class is the name of the class on which it is based.

class Derived_Class : VisiblityModifier Base_Class

{

...

...

};

Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 17

Defining Derived Class The VisiblityModifier is optional, and if present it may be

public

protected

private.

It describes the access level for the members that are inherited fromthe base class.

class Derived_Class : VisiblityModifier Base_Class

{

...

...

};

Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 18

Defining Derived Class

If VisiblityModifier is not specified then default visibility isprivate.

class Derived_Class : Base_Class

{

...

...

};

Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 19

Defining Derived Class

A class can have members under following 3 visibilities

Public

Protected

Private

In inheritance, public & protected members of base class getsinherited into derived class.

But private members of base class are inaccessible to derivedclass.

Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 20

Public Derivation

class Abc

{

// members of ABC

};

class Xyz : public Abc

{

// members of XYZ

};

Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 21

Private Derivation

class Abc

{

// members of ABC

};

class Xyz : private Abc

{

// members of XYZ

};

Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 22

Protected Derivation

class Abc

{

// members of ABC

};

class Xyz : protected Abc

{

// members of XYZ

};

Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 23

Default Private Derivation

class Abc

{

// members of ABC

};

class Xyz : Abc

{

// members of XYZ

};

Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 24

Private Visibility Mode When a base class is privately inherited by a derived class,

All public & protected members of base class becomes privatemembers of derived class.

No private member of base class will be accessible by derived class.

In this case the public as well as protected members of base classcan be accessed by the member functions of derived class.

But no member of base class can be accessed by object ofderived class.

Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 25

Public Visibility Mode When a base class is publicly inherited by a derived class,

all public members of base class becomes public members ofderived class.

all protected members of base class will become protectedmembers of derived class.

No private member of base class will be accessible by derived class.

In this case the public members of base class can be accessed viaobject of derived class.

Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 26

Protected Visibility Mode C++ Provides a third visibility modifier ‘protected’, which serve a

limited purpose in inheritance.

A member declared as protected is accessible by the memberfunctions within its class and any class immediately derived fromit.

It cannot be accessed by the functions outside these two classes.

Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 27

Protected Visibility Mode When a base class is protectedly inherited by a derived class,

all public & protected members of base class becomes protectedmembers of derived class.

No private member of base class will be accessible by derived class.

In this case no members of base class can be accessed via objectof derived class.

Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 28

Visibility Modesclass ABC{

private://optional//accessible to member functions within this class only.

protected://accessible to member functions within this class and

derived class only.

public://accessible to all functions within the program.

};

Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 29

Private

Protected

Public

Private

Protected

Public

Private

Protected

Public

Private

Protected

Public

Not InheritableNot Inheritable

Class B : Public A

Class A

Class C : Private A

Class X : Protected B