1 what is the purpose of inheritance? zspecialisation extending the functionality of an existing...

Post on 05-Jan-2016

215 Views

Category:

Documents

1 Downloads

Preview:

Click to see full reader

TRANSCRIPT

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

2

Inheritance types in C++

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

3

Single Inheritance

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

Base Class

Derived Class

4

Single Inheritance

class A { // Base Class A};

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

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.

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.

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

Multiple Inheritance

class A { // Base Class A};

class B //Base Class B{};

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

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.

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

{};

Hybrid Inheritance

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

CM505.40 12

Single inheritance

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

CM505.40 13

Single inheritance

• The declaration of derived class is same as ordinary class

• A derived class contains of the following components

CM505.40 14

Single inheritance

• The keyword class

• The name of the Derived class

• A single colon

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

CM505.40 16

Single inheritance

BASE CLASS

DERIVED CLASS

CM505.40 17

SYNTAX:

class derived_classname:visibilitymode base_classname

{ ………………….

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

Single inheritance

CM505.40 18

Single inheritance : Syntax

class A { …………………

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

members of derived class B …………… }

CM505.40 19

Program for single inheritance class person

{

char name[10];

int age;

char gender;

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

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;

}

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; }

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; }

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();}

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

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

top related