Lec 30.31 - inheritance

Post on 22-May-2015

111 views 0 download

Tags:

transcript

InheritanceInheritance

Chapter: Chapter: 0909

Lecture: 30 & 31Lecture: 30 & 31

Date: 08.10.2012Date: 08.10.2012

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).

Inh

eri

tan

ce:

Base

an

d D

eri

ved

C

lass

es

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

Inheritance: ShapesInheritance: Shapes

RectangleTriangle

Polygon

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

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

};

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

};

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

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

{{

}}

Access Specifier Access Specifier withoutwithout InheritanceInheritance

Access Specifier Access Specifier withwith InheritanceInheritance

Inheritance and Inheritance and AccessibilityAccessibility

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

Stack ApplicationsStack Applications Memory managementMemory management Tower of HanoiTower of Hanoi

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

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