Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "CE00314-2 Further Programming Concepts in C++ Lecture 5 Inheritance & Polymorphism."— Presentation transcript:

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

2 Introduction Basic inheritance Polymorphism Constructors and destructors Extending inheritance

3 Bank accounts “Normal” accountAccount  Already considered  Owner details, balance, methods etc. TransferCheque  Technique to transfer money to other account  Charge for handling Savings Savings  Deposit, withdrawal & interest InvestmentsInvestment  Only interest can be withdrawn

4 Accounts relationships Account Cheque Savings Investment

5 Declarations - abridged Within header file class Account { protected: double dBalance; Public: Account(double dBal = 0) { dBalance = dBal; } double GetBalance(void) { return dBalance; } };

6 Default values - revisited Use can be esoteric  Especially when removing implementation from class definition See DefaultValues.cpp & DefaultValues.h

7 Protected New keyword Allows derived classes / objects to access.  “Family” Not available to “outsiders” Private members  Only within  Or by friends

8 Savings Account class Savings:public Account { protected: double dRate; public: Savings(double = 0.0, double = 0.5); double Compound(void); double Withdraw(double); };

9 Savings Account See Savings.cpp & Savings.h Acc2 is Savings  Inherited from base Account Attributes Methods  Adds new as required for speciality AttributesInterest Rate – dRate MethodsOwn constructor(s) Own method(s) – Compound()

10 More Inheritance - Investment class Investment:public Savings { protected: double dFundsAvailable; public: Investment(double = 0.0, double = 1.5, int = -99); double Compound();// redefinition double Withdraw(double);// redefinition double GetAvailable(); };

11 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

12 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

13 Polymorphism Shape CircleSquareTriangle

14 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

15 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

16 Shape base class class Shape { private: // Attributes int nXPosition; int nYposition; char strName[50]; public: // Methods Shape(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; } };

17 Square class class Square:public Shape { private: // Attributes for Square int 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; } };

18 Circle class class Circle:public Shape { private: // Attributes for Circle int 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 warning Shape::Draw(); cout << "My area is " << nArea << " square units" << endl; } };

19 Main // ShapesMain.cpp #include #define PI 3.142 using namespace std; #include "ShapesClasses.h" int main(void) { ShapeMyBase(10, 20, "Base Shape"); SquareMySquare(20, 50, 162, 75, "Square"); CircleMyCircle(70, 26, 24, "Circle"); cout << endl << "-------------------------------------------" << endl; MyBase.Draw(); cout << endl << "-------------------------------------------" << endl; MySquare.Draw(); cout << endl << "-------------------------------------------" << endl; MyCircle.Draw(); cout << endl << "-------------------------------------------" << endl; return 0; }

20 Running the code Note output from constructors

21 Constructor Order Starts with least specialised Works sequentially towards most specialised. ShapeMyBase(10, 20, "Base Shape"); SquareMySquare(20, 50, 162, 75, "Square"); CircleMyCircle(70, 26, 24, "Circle");

22 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; }

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

24 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

25 Example code See ShapesMain.cpp & ShapesClasses.h  Within ShapeTypes.doc

26 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

27 Triangles Shape CircleSquareTriangle IsoscelesEquilateralScalene

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


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

Similar presentations


Ads by Google