1 / 28

CE00314-2 Further Programming Concepts in C++

CE00314-2 Further 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.

adah
Download Presentation

CE00314-2 Further Programming Concepts in C++

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. Introduction • Basic inheritance • Polymorphism • Constructors and destructors • Extending inheritance

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

  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 • Attributes Interest Rate – dRate • Methods Own 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 Circle Square Triangle

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

  20. Note output from constructors Running the code

  21. 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");

  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 Circle Square Triangle Isosceles Equilateral Scalene

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

More Related