1 What is the purpose of Inheritance? zSpecialisation Extending the functionality of an existing...

25
1 What is the purpose of Inheritance? Specialisation Extending the functionality of an existing class Generalisation sharing commonality between two or more classes Improved efficiency and greater robustness Plays a major part in polymorphism

Transcript of 1 What is the purpose of Inheritance? zSpecialisation Extending the functionality of an existing...

Page 1: 1 What is the purpose of Inheritance? zSpecialisation Extending the functionality of an existing class zGeneralisation sharing commonality between two.

1

What is the purpose of Inheritance?

SpecialisationExtending the functionality of an existing

classGeneralisation

sharing commonality between two or more classes

Improved efficiency and greater robustness

Plays a major part in polymorphism

Page 2: 1 What is the purpose of Inheritance? zSpecialisation Extending the functionality of an existing class zGeneralisation sharing commonality between two.

2

Inheritance types in C++

1. Single level Inheritance2. Multilevel Inheritance3. Multiple Inheritance4. Hierarchical Inheritance5. Hybrid Inheritance

Page 3: 1 What is the purpose of Inheritance? zSpecialisation Extending the functionality of an existing class zGeneralisation sharing commonality between two.

3

Single Inheritance

Inheriting the properties of one Derived class from only one Base class is known as Single Inheritance.

Base Class

Derived Class

Page 4: 1 What is the purpose of Inheritance? zSpecialisation Extending the functionality of an existing class zGeneralisation sharing commonality between two.

4

Single Inheritance

class A { // Base Class A};

class B: public A //Derived Class B{};

Page 5: 1 What is the purpose of Inheritance? zSpecialisation Extending the functionality of an existing class zGeneralisation sharing commonality between two.

5

Multilevel Inheritance

When Single level inheritance is extended to more levels then it is called multilevel inheritance.

In this inheritance one class is derived from another derived class and the level of derivation can be extended to any number of levels.

Page 6: 1 What is the purpose of Inheritance? zSpecialisation Extending the functionality of an existing class zGeneralisation sharing commonality between two.

6

Multilevel Inheritance

Base Class

Intermediate Base Class

Derived Class

class A { // Base Class A};

class B: public A //Intermediate Base Class B

{};

class C: public B // C Derived Class

from B {};The class B is known as intermediate base class since it provides a link for the inheritance between A and C. The chain ABC is known as inheritance path.

Page 7: 1 What is the purpose of Inheritance? zSpecialisation Extending the functionality of an existing class zGeneralisation sharing commonality between two.

Multiple InheritanceInheriting the properties of the Derived class from

more than one Base Class is

known as Multiple

Inheritance.

Derived Class

Base Clas 1

Base Class

2 Base Class

n

Page 8: 1 What is the purpose of Inheritance? zSpecialisation Extending the functionality of an existing class zGeneralisation sharing commonality between two.

Multiple Inheritance

class A { // Base Class A};

class B //Base Class B{};

class C: public A, B // C Derived Class from A and B {};

Page 9: 1 What is the purpose of Inheritance? zSpecialisation Extending the functionality of an existing class zGeneralisation sharing commonality between two.

Hierarchical Inheritance

• In Hierarchical Inheritance, many sub classes inherit the properties from a single base class. The base class will include all the features that are common to the subclasses.

• A subclass can be constructed by inheriting the all or some properties of the base class.

• A subclass can serve as a base class for the lower level classes and so on.

Page 10: 1 What is the purpose of Inheritance? zSpecialisation Extending the functionality of an existing class zGeneralisation sharing commonality between two.

Hierarchical Inheritance

Base Class

Sub Class

Sub Class

Sub class

class A { // Base Class A};

class B: public A //Derived Class B

{};

class C: public A // C Derived Class

{};

Page 11: 1 What is the purpose of Inheritance? zSpecialisation Extending the functionality of an existing class zGeneralisation sharing commonality between two.

Hybrid Inheritance

Applying two or more types of inheritance for designing the program is known Hybrid Inheritance.

Page 12: 1 What is the purpose of Inheritance? zSpecialisation Extending the functionality of an existing class zGeneralisation sharing commonality between two.

CM505.40 12

Single inheritance

A single inheritance contains only one derived class and one base class

Page 13: 1 What is the purpose of Inheritance? zSpecialisation Extending the functionality of an existing class zGeneralisation sharing commonality between two.

CM505.40 13

Single inheritance

• The declaration of derived class is same as ordinary class

• A derived class contains of the following components

Page 14: 1 What is the purpose of Inheritance? zSpecialisation Extending the functionality of an existing class zGeneralisation sharing commonality between two.

CM505.40 14

Single inheritance

• The keyword class

• The name of the Derived class

• A single colon

Page 15: 1 What is the purpose of Inheritance? zSpecialisation Extending the functionality of an existing class zGeneralisation sharing commonality between two.

CM505.40 15

Single inheritance

• The type of derivation (private, public or protected)

• The name of the base or parent class

• The reminder of the class definition

Page 16: 1 What is the purpose of Inheritance? zSpecialisation Extending the functionality of an existing class zGeneralisation sharing commonality between two.

CM505.40 16

Single inheritance

BASE CLASS

DERIVED CLASS

Page 17: 1 What is the purpose of Inheritance? zSpecialisation Extending the functionality of an existing class zGeneralisation sharing commonality between two.

CM505.40 17

SYNTAX:

class derived_classname:visibilitymode base_classname

{ ………………….

members of derived class …………… ……………… };

Single inheritance

Page 18: 1 What is the purpose of Inheritance? zSpecialisation Extending the functionality of an existing class zGeneralisation sharing commonality between two.

CM505.40 18

Single inheritance : Syntax

class A { …………………

members of base class A …………………. } class B: public A { …………….

members of derived class B …………… }

Page 19: 1 What is the purpose of Inheritance? zSpecialisation Extending the functionality of an existing class zGeneralisation sharing commonality between two.

CM505.40 19

Program for single inheritance class person

{

char name[10];

int age;

char gender;

public: void get data(); void display(); };

Page 20: 1 What is the purpose of Inheritance? zSpecialisation Extending the functionality of an existing class zGeneralisation sharing commonality between two.

CM505.40 20

Program for single inheritance void person ::get data() { cout<<“enter the name”; cin>>name; cout<<“enter the age”; cin>>age; cout<<“enter the gender”; cin>>gender;

}

Page 21: 1 What is the purpose of Inheritance? zSpecialisation Extending the functionality of an existing class zGeneralisation sharing commonality between two.

CM505.40 21

Program for single inheritance

void person :: display() { cout<<“the name of the student is “<<name; cout<<“the age of the student is “<<age; cout<<“the gender of the student is “<<gender; }

Page 22: 1 What is the purpose of Inheritance? zSpecialisation Extending the functionality of an existing class zGeneralisation sharing commonality between two.

CM505.40 22

Program for single inheritance class student : public person { int roll, no; char branch[10];public: void get data() { person :: getdata(); cout<<enter the roll no”; cin>>roll,no; cout<<“enter branch”; cin>>branch; }

Page 23: 1 What is the purpose of Inheritance? zSpecialisation Extending the functionality of an existing class zGeneralisation sharing commonality between two.

CM505.40 23

Program for single inheritance

void display() { person :: display(); cout<<“the roll no is”<<roll, no; cout<<“the branch is “<<branch; } };void main(){ student s; s.getdata(); s.display();}

Page 24: 1 What is the purpose of Inheritance? zSpecialisation Extending the functionality of an existing class zGeneralisation sharing commonality between two.

24

Summary: Inheritance

Inheriting from a class doesn’t affect the integrity of that class - objects of the original base class can still be created

Generalisation allows removal of redundancy and duplication among classes

Some base classes are abstract - they are not specific enough to be instantiated but act as holders for common attributes and methods of derived classes

Page 25: 1 What is the purpose of Inheritance? zSpecialisation Extending the functionality of an existing class zGeneralisation sharing commonality between two.

25

Summary: Inheritance

In C++ the protected keyword allows methods of a derived class access to its inherited attributes

Base class constructor methods are automatically called by the constructors of derived classes but argument lists must be compatible

Destructors are called in the reverse order of constructors