Top Banner
CE00314-2 Further Programming Concepts in C++ Lecture 5 Inheritance & Polymorphism
28

CE00314-2 Further Programming Concepts in C++ Lecture 5 Inheritance & Polymorphism.

Jan 01, 2016

Download

Documents

Bernard Gaines
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: CE00314-2 Further Programming Concepts in C++ Lecture 5 Inheritance & Polymorphism.

CE00314-2Further Programming Concepts in C++

Lecture 5

Inheritance & Polymorphism

Page 2: CE00314-2 Further Programming Concepts in C++ Lecture 5 Inheritance & Polymorphism.

Introduction

Basic inheritance Polymorphism Constructors and destructors Extending inheritance

Page 3: CE00314-2 Further Programming Concepts in C++ Lecture 5 Inheritance & Polymorphism.

Bank accounts

“Normal” account Account Already considered Owner details, balance, methods etc.

Transfer Cheque Technique to transfer money to other account Charge for handling

Savings Savings Deposit, withdrawal & interest

Investments Investment Only interest can be withdrawn

Page 4: CE00314-2 Further Programming Concepts in C++ Lecture 5 Inheritance & Polymorphism.

Accounts relationships

Account

ChequeSavings

Investment

Page 5: CE00314-2 Further Programming Concepts in C++ Lecture 5 Inheritance & Polymorphism.

Declarations - abridged

Within header file

class Account{protected:

double dBalance;Public:

Account(double dBal = 0){

dBalance = dBal;}

double GetBalance(void){

return dBalance;}

};

Page 6: CE00314-2 Further Programming Concepts in C++ Lecture 5 Inheritance & Polymorphism.

Default values - revisited

Use can be esoteric Especially when removing implementation from

class definition

See DefaultValues.cpp & DefaultValues.h

Page 7: CE00314-2 Further Programming Concepts in C++ Lecture 5 Inheritance & Polymorphism.

Protected

New keyword Allows derived classes / objects to access.

“Family” Not available to “outsiders”

Private members Only within Or by friends

Page 8: CE00314-2 Further Programming Concepts in C++ Lecture 5 Inheritance & Polymorphism.

Savings Account

class Savings:public Account{protected:double dRate;

public:Savings(double = 0.0, double = 0.5);double Compound(void);double Withdraw(double);

};

Page 9: CE00314-2 Further Programming Concepts in C++ Lecture 5 Inheritance & Polymorphism.

Savings Account

See Savings.cpp & Savings.h Acc2 is Savings

Inherited from base Account Attributes Methods

Adds new as required for speciality Attributes Interest Rate – dRate Methods Own constructor(s)

Own method(s) – Compound()

Page 10: CE00314-2 Further Programming Concepts in C++ Lecture 5 Inheritance & Polymorphism.

More Inheritance - Investment

class Investment:public Savings{protected:

double dFundsAvailable;

public:Investment(double = 0.0, double = 1.5, int = -99);

double Compound(); // redefinitiondouble Withdraw(double); // redefinitiondouble GetAvailable();

};

Page 11: CE00314-2 Further Programming Concepts in C++ Lecture 5 Inheritance & Polymorphism.

Investment Account

Inherited from Savings NOT directly from Account

See Investment.cpp & Investment.h Note

Investment Withdraw method Complete redefinition

Investment Partial, adds to Savings

Page 12: CE00314-2 Further Programming Concepts in C++ Lecture 5 Inheritance & Polymorphism.

Polymorphism

Meaning The ability to appear in many forms

Programming Objects react differently to same “instruction”

Depending upon data types or class

Practically Ability to redefine methods for derived classes

Page 13: CE00314-2 Further Programming Concepts in C++ Lecture 5 Inheritance & Polymorphism.

Polymorphism

Shape

Circle Square Triangle

Page 14: CE00314-2 Further Programming Concepts in C++ Lecture 5 Inheritance & Polymorphism.

Polymorphism

Shape – base type Anchor position, “name” field, colour etc.

Circle – inherits from Shape Radius, method to draw circle.

Square – inherits from Shape Height, width, methods to draw square

Triangle – inherits from Shape Height, base width, methods to draw triagle

Page 15: CE00314-2 Further Programming Concepts in C++ Lecture 5 Inheritance & Polymorphism.

Polymorphism

Let the object look after itself Carry what it needs to know Remove functionality to the object Do not carry items relating to others

Page 16: CE00314-2 Further Programming Concepts in C++ Lecture 5 Inheritance & Polymorphism.

Shape base class

class Shape{private:

// Attributesint nXPosition;int nYposition;char strName[50];

public:// MethodsShape(int nX, int nY, char *ptrName){

nXPosition = nX;nYposition = nY;strcpy(strName, ptrName);

}

void Draw(void){

cout << "I am a " << strName << ", I am at " << nXPosition <<" and " << nYposition << endl;

}};

Page 17: CE00314-2 Further Programming Concepts in C++ Lecture 5 Inheritance & Polymorphism.

Square class

class Square:public Shape{private:

// Attributes for Squareint nLength;int nWidth;

public:Square(int nX, int nY, int nL, int nW, char *ptrName):Shape(nX,nY,ptrName){

nLength = nL;nWidth = nW;cout << "Square object created" << endl;

}

void Draw(void){

int nArea;nArea = nLength * nWidth;Shape::Draw();cout << "My area is " << nArea << " square units" << endl;

}

};

Page 18: CE00314-2 Further Programming Concepts in C++ Lecture 5 Inheritance & Polymorphism.

Circle classclass Circle:public Shape{private:

// Attributes for Circleint nRadius;

public:Circle(int nX, int nY, int nR, char *ptrName):Shape(nX, nY, ptrName){

nRadius = nR;cout << "Circle object created" << endl;

}

~Circle(void){

cout << "Circle object being deleted (Circle destructor)" << endl;}void Draw(void){

float nArea;nArea = (float)(PI * nRadius * nRadius); // Note casting to avoid warningShape::Draw();cout << "My area is " << nArea << " square units" << endl;

}};

Page 19: CE00314-2 Further Programming Concepts in C++ Lecture 5 Inheritance & Polymorphism.

Main// ShapesMain.cpp#include <iostream>#include <string.h>#define PI 3.142

using namespace std;

#include "ShapesClasses.h"

int main(void){

Shape MyBase(10, 20, "Base Shape");Square MySquare(20, 50, 162, 75, "Square");Circle MyCircle(70, 26, 24, "Circle");

cout << endl << "-------------------------------------------" << endl;MyBase.Draw();cout << endl << "-------------------------------------------" << endl;MySquare.Draw();cout << endl << "-------------------------------------------" << endl;MyCircle.Draw();cout << endl << "-------------------------------------------" << endl;

return 0;}

Page 20: CE00314-2 Further Programming Concepts in C++ Lecture 5 Inheritance & Polymorphism.

Running the code

Note output fromconstructors

Page 21: CE00314-2 Further Programming Concepts in C++ Lecture 5 Inheritance & Polymorphism.

Constructor Order

Starts with least specialised Works sequentially towards most specialised.

Shape MyBase(10, 20, "Base Shape");

Square MySquare(20, 50, 162, 75, "Square");

Circle MyCircle(70, 26, 24, "Circle");

Page 22: CE00314-2 Further Programming Concepts in C++ Lecture 5 Inheritance & Polymorphism.

Destructors~Shape(void)

{cout << strName << " being deleted (Shape destructor)" << endl;

}---------------------------------------------------~Square(void)

{cout << "Square object being deleted (Square destructor)" << endl;

}---------------------------------------------------~Circle(void)

{cout << "Circle object being deleted (Circle destructor)" << endl;

}

Page 23: CE00314-2 Further Programming Concepts in C++ Lecture 5 Inheritance & Polymorphism.

Destructor Order

Starts with most specialised Works sequentially towards least specialised. Opposite of constructors

Page 24: CE00314-2 Further Programming Concepts in C++ Lecture 5 Inheritance & Polymorphism.

Destructor call

Review As before – when “delete” used

Not included within Main – see slide 19 Only difference within class

Addition of Destructors When application terminates

All associated objects deleted Destructors call automatically

Page 25: CE00314-2 Further Programming Concepts in C++ Lecture 5 Inheritance & Polymorphism.

Example code

See ShapesMain.cpp & ShapesClasses.h Within ShapeTypes.doc

Page 26: CE00314-2 Further Programming Concepts in C++ Lecture 5 Inheritance & Polymorphism.

Tutorial Work

Develop Cheque using inheritance As per diagram within slide 4 Able to transfer money from one account to

another

Extend Shapes to include Triangle Extend Shapes with Triangle

Different types of triangle See next slide

Page 27: CE00314-2 Further Programming Concepts in C++ Lecture 5 Inheritance & Polymorphism.

Triangles

Shape

Circle Square Triangle

Isosceles Equilateral Scalene

Page 28: CE00314-2 Further Programming Concepts in C++ Lecture 5 Inheritance & Polymorphism.

Summary

Basic inheritance Polymorphism Constructors and inheritance Destructors and inheritance Extending the inheritance