+ All Categories
Home > Documents > Lec 30.31 - inheritance

Lec 30.31 - inheritance

Date post: 22-May-2015
Category:
Upload: princess-sam
View: 111 times
Download: 0 times
Share this document with a friend
Popular Tags:
17
Inheritance Inheritance Chapter: Chapter: 09 09 Lecture: 30 & 31 Lecture: 30 & 31 Date: 08.10.2012 Date: 08.10.2012
Transcript
Page 1: Lec 30.31 -  inheritance

InheritanceInheritance

Chapter: Chapter: 0909

Lecture: 30 & 31Lecture: 30 & 31

Date: 08.10.2012Date: 08.10.2012

Page 2: Lec 30.31 -  inheritance

Inheritance: Base and Derived Classes Inheritance is the Inheritance is the

process of process of creating a new creating a new classes (classes (derivedderived classes) from classes) from existing classes existing classes (based classes).(based classes).

Page 3: Lec 30.31 -  inheritance

Inh

eri

tan

ce:

Base

an

d D

eri

ved

C

lass

es

Page 4: Lec 30.31 -  inheritance

Inheritance: AdvantagesInheritance: Advantages

Permits code reusabilityPermits code reusability

Reusing existing code saves time Reusing existing code saves time and money and increases a and money and increases a program’s reliabilityprogram’s reliability

Page 5: Lec 30.31 -  inheritance

Inheritance: ShapesInheritance: Shapes

RectangleTriangle

Polygon

Page 6: Lec 30.31 -  inheritance

No-Inheritance: ShapesNo-Inheritance: Shapesclass Rectangle{

private:

int numVertices; int xCoord, yCoord; public: void set(int x, int y, int nV); int area();

};

RectangleTriangle

Polygon

class Polygon{ private:

int numVertices; int xCoord, yCoord;

public: void set(int x, int y, int

nV); int area();};

class Triangle{

private: int numVertices;

int xCoord, yCoord; public:

void set(int x, int y, int nV); int area();};

Page 7: Lec 30.31 -  inheritance

Inheritance: Inheritance: ShapesShapes

RectangleTriangle

Polygonclass Polygon{ protected:

int numVertices; float xCoord, yCoord;

public: void set(int x, int y, int

nV);};

class Rectangle : public Polygon{public: int area();

};

Page 8: Lec 30.31 -  inheritance

Inheritance: Inheritance: ShapesShapes

RectangleTriangle

Polygonclass Polygon{ protected:

int numVertices; float xCoord, yCoord;

public: void set(float x, float y, int

nV);};

class Rectangle : public Polygon{public: float area();

};

class Triangle : public Polygon{public: int area();

};

Page 9: Lec 30.31 -  inheritance

Inheritance: SyntaxInheritance: Syntax Syntax:Syntax:

class class DerivedClassNameDerivedClassName : access-level : access-level BaseClassNameBaseClassName

where where access-levelaccess-level specifies the type of derivationspecifies the type of derivation

private by default, orprivate by default, or PublicPublic

Any class can serve as a base classAny class can serve as a base class Thus a derived class can also be a base classThus a derived class can also be a base class

Page 10: Lec 30.31 -  inheritance

Inheritance: SyntaxInheritance: Syntax Syntax:Syntax:

class class DerivedClassNameDerivedClassName : access-level : access-level BaseClassNameBaseClassName

Example:Example:

class class BaseClassBaseClass

{{

}}

class DerivedClass: public class DerivedClass: public BaseClassBaseClass

{{

}}

Page 11: Lec 30.31 -  inheritance

Access Specifier Access Specifier withoutwithout InheritanceInheritance

Page 12: Lec 30.31 -  inheritance

Access Specifier Access Specifier withwith InheritanceInheritance

Page 13: Lec 30.31 -  inheritance

Inheritance and Inheritance and AccessibilityAccessibility

Page 14: Lec 30.31 -  inheritance

Overriding member Overriding member functionsfunctions

Member functions in a derived class can Member functions in a derived class can have the have the same namessame names as in the base class as in the base class

When the same function exists in both the When the same function exists in both the base and derived classes, the function in base and derived classes, the function in the derived class will be executed.the derived class will be executed. In order to call a function in the base class use the use the In order to call a function in the base class use the use the

scope resolution operator, e.g.,scope resolution operator, e.g.,

Stack :: push()Stack :: push()

Name (base class)

Scope

resolution

operator

Call to a member function in base class

Page 15: Lec 30.31 -  inheritance

Stack ApplicationsStack Applications Memory managementMemory management Tower of HanoiTower of Hanoi

Page 16: Lec 30.31 -  inheritance

Stack ApplicationsStack Applications Converting a decimal number to binaryConverting a decimal number to binary

Page 17: Lec 30.31 -  inheritance

Inheritance: ShapesInheritance: Shapes

Point

Circle 3D-Point

class Point{protected: int x, y;public: void set (int a, int b);

};

class Circle : public Point{private:

double r;};

class 3D-Point: public Point{private:

int z;};

xy

xyr

xyz


Recommended