CE00314-2 Further Programming Concepts in C++ Lecture 5 Inheritance & Polymorphism
Jan 01, 2016
CE00314-2Further Programming Concepts in C++
Lecture 5
Inheritance & Polymorphism
Introduction
Basic inheritance Polymorphism Constructors and destructors Extending inheritance
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
Accounts relationships
Account
ChequeSavings
Investment
Declarations - abridged
Within header file
class Account{protected:
double dBalance;Public:
Account(double dBal = 0){
dBalance = dBal;}
double GetBalance(void){
return dBalance;}
};
Default values - revisited
Use can be esoteric Especially when removing implementation from
class definition
See DefaultValues.cpp & DefaultValues.h
Protected
New keyword Allows derived classes / objects to access.
“Family” Not available to “outsiders”
Private members Only within Or by friends
Savings Account
class Savings:public Account{protected:double dRate;
public:Savings(double = 0.0, double = 0.5);double Compound(void);double Withdraw(double);
};
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()
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();
};
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
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
Polymorphism
Shape
Circle Square Triangle
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
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
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;
}};
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;
}
};
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;
}};
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;}
Running the code
Note output fromconstructors
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");
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;
}
Destructor Order
Starts with most specialised Works sequentially towards least specialised. Opposite of constructors
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
Example code
See ShapesMain.cpp & ShapesClasses.h Within ShapeTypes.doc
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
Triangles
Shape
Circle Square Triangle
Isosceles Equilateral Scalene
Summary
Basic inheritance Polymorphism Constructors and inheritance Destructors and inheritance Extending the inheritance