ce00314-2 further programming concepts in c++ lecture 5 inheritance & polymorphism
TRANSCRIPT
![Page 1: CE00314-2 Further Programming Concepts in C++ Lecture 5 Inheritance & Polymorphism](https://reader030.vdocuments.site/reader030/viewer/2022032607/56649ec55503460f94bcfb22/html5/thumbnails/1.jpg)
CE00314-2Further Programming Concepts in C++
Lecture 5
Inheritance & Polymorphism
![Page 2: CE00314-2 Further Programming Concepts in C++ Lecture 5 Inheritance & Polymorphism](https://reader030.vdocuments.site/reader030/viewer/2022032607/56649ec55503460f94bcfb22/html5/thumbnails/2.jpg)
Introduction
Basic inheritance Polymorphism Constructors and destructors Extending inheritance
![Page 3: CE00314-2 Further Programming Concepts in C++ Lecture 5 Inheritance & Polymorphism](https://reader030.vdocuments.site/reader030/viewer/2022032607/56649ec55503460f94bcfb22/html5/thumbnails/3.jpg)
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](https://reader030.vdocuments.site/reader030/viewer/2022032607/56649ec55503460f94bcfb22/html5/thumbnails/4.jpg)
Accounts relationships
Account
ChequeSavings
Investment
![Page 5: CE00314-2 Further Programming Concepts in C++ Lecture 5 Inheritance & Polymorphism](https://reader030.vdocuments.site/reader030/viewer/2022032607/56649ec55503460f94bcfb22/html5/thumbnails/5.jpg)
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](https://reader030.vdocuments.site/reader030/viewer/2022032607/56649ec55503460f94bcfb22/html5/thumbnails/6.jpg)
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](https://reader030.vdocuments.site/reader030/viewer/2022032607/56649ec55503460f94bcfb22/html5/thumbnails/7.jpg)
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](https://reader030.vdocuments.site/reader030/viewer/2022032607/56649ec55503460f94bcfb22/html5/thumbnails/8.jpg)
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](https://reader030.vdocuments.site/reader030/viewer/2022032607/56649ec55503460f94bcfb22/html5/thumbnails/9.jpg)
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](https://reader030.vdocuments.site/reader030/viewer/2022032607/56649ec55503460f94bcfb22/html5/thumbnails/10.jpg)
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](https://reader030.vdocuments.site/reader030/viewer/2022032607/56649ec55503460f94bcfb22/html5/thumbnails/11.jpg)
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](https://reader030.vdocuments.site/reader030/viewer/2022032607/56649ec55503460f94bcfb22/html5/thumbnails/12.jpg)
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](https://reader030.vdocuments.site/reader030/viewer/2022032607/56649ec55503460f94bcfb22/html5/thumbnails/13.jpg)
Polymorphism
Shape
Circle Square Triangle
![Page 14: CE00314-2 Further Programming Concepts in C++ Lecture 5 Inheritance & Polymorphism](https://reader030.vdocuments.site/reader030/viewer/2022032607/56649ec55503460f94bcfb22/html5/thumbnails/14.jpg)
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](https://reader030.vdocuments.site/reader030/viewer/2022032607/56649ec55503460f94bcfb22/html5/thumbnails/15.jpg)
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](https://reader030.vdocuments.site/reader030/viewer/2022032607/56649ec55503460f94bcfb22/html5/thumbnails/16.jpg)
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](https://reader030.vdocuments.site/reader030/viewer/2022032607/56649ec55503460f94bcfb22/html5/thumbnails/17.jpg)
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](https://reader030.vdocuments.site/reader030/viewer/2022032607/56649ec55503460f94bcfb22/html5/thumbnails/18.jpg)
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](https://reader030.vdocuments.site/reader030/viewer/2022032607/56649ec55503460f94bcfb22/html5/thumbnails/19.jpg)
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](https://reader030.vdocuments.site/reader030/viewer/2022032607/56649ec55503460f94bcfb22/html5/thumbnails/20.jpg)
Running the code
Note output fromconstructors
![Page 21: CE00314-2 Further Programming Concepts in C++ Lecture 5 Inheritance & Polymorphism](https://reader030.vdocuments.site/reader030/viewer/2022032607/56649ec55503460f94bcfb22/html5/thumbnails/21.jpg)
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](https://reader030.vdocuments.site/reader030/viewer/2022032607/56649ec55503460f94bcfb22/html5/thumbnails/22.jpg)
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](https://reader030.vdocuments.site/reader030/viewer/2022032607/56649ec55503460f94bcfb22/html5/thumbnails/23.jpg)
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](https://reader030.vdocuments.site/reader030/viewer/2022032607/56649ec55503460f94bcfb22/html5/thumbnails/24.jpg)
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](https://reader030.vdocuments.site/reader030/viewer/2022032607/56649ec55503460f94bcfb22/html5/thumbnails/25.jpg)
Example code
See ShapesMain.cpp & ShapesClasses.h Within ShapeTypes.doc
![Page 26: CE00314-2 Further Programming Concepts in C++ Lecture 5 Inheritance & Polymorphism](https://reader030.vdocuments.site/reader030/viewer/2022032607/56649ec55503460f94bcfb22/html5/thumbnails/26.jpg)
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](https://reader030.vdocuments.site/reader030/viewer/2022032607/56649ec55503460f94bcfb22/html5/thumbnails/27.jpg)
Triangles
Shape
Circle Square Triangle
Isosceles Equilateral Scalene
![Page 28: CE00314-2 Further Programming Concepts in C++ Lecture 5 Inheritance & Polymorphism](https://reader030.vdocuments.site/reader030/viewer/2022032607/56649ec55503460f94bcfb22/html5/thumbnails/28.jpg)
Summary
Basic inheritance Polymorphism Constructors and inheritance Destructors and inheritance Extending the inheritance