1 / 23

Polymorphism and Virtual Functions

Polymorphism and Virtual Functions. class Ball : public Sphere. Still Sphere’s version of displayStatistics(). Sphere’s version of displayStatistics(). Problems in Function Redefinition. int main() { Sphere mySphere(); Ball myBall(); Sphere *spherePtr; spherePtr = &mySphere;

morganq
Download Presentation

Polymorphism and Virtual Functions

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. Polymorphism and Virtual Functions

  2. class Ball : public Sphere

  3. Still Sphere’s version of displayStatistics() Sphere’s version of displayStatistics() Problems in Function Redefinition int main() { Sphere mySphere(); Ball myBall(); Sphere *spherePtr; spherePtr = &mySphere; spherePtr->displayStatistics(); … … spherePtr = &myBall; spherePtr->displayStatistics(); }

  4. Virtual Functions • Virtual functions are the answer • Tells compiler: • “Wait until used in program” • “Then get implementation from objectinstance” • Called late binding or dynamic binding • Virtual functions implement late binding • Class that define virtual functions are extensible

  5. Polymorphism • Polymorphism refers to the ability to associate many meanings to one function by means of the late-binding mechanism. Thus, polymorphism, late binging, and virtual functions are really all the same topic.

  6. Use Virtual Functions class Sphere { public: virtual void displayStatistics(){ cout << “It is Sphere\n”; } }; class Ball : public Sphere { public: void displayStatistics(){ cout << “It is Ball\n”; } }; int main() { Ball myBall(); Sphere *spherePtr; spherePtr = &myBall; spherePtr-> displayStatistics(); } OUTPUT: It is Ball

  7. Virtual: How? • To write C++ programs: • Assume it happens by ‘magic’! • But explanation involves late binding • Virtual functions implement late binding • Tells compiler to ‘wait’ until function is used in program • Decide which definition to use based oncalling object • Very important OOP principle

  8. Overriding • Virtual function definition changed in aderived class • We say it’s been ‘overridden’ • So: • Virtual functions changed: overridden • Non-virtual functions changed: redefined

  9. Virtual Functions: Why Not All? • Clear advantages to virtual functions aswe’ve seen • One major disadvantage: overhead! • Uses more storage • Late binding is ‘on the fly’, so programs runslower • So if virtual functions not needed, shouldnot be used

  10. Subtle Example getArea is not virtual; myBall.displayStatistics() calls Sphere::getArea

  11. Subtle Example getArea is virtual; (a) mySphere.displayStatistics() calls Sphere::getArea;

  12. Subtle Example (b) myBall.displayStatistics() calls Ball::getArea

  13. Not All Definitions are Useful • Base class might not have ‘meaningful’definition for some of it’s members! class Employee { public: Employee(string tName, sring tSsn); … void printCheck() const; }; Employee::printCheck() { cout << “ERROR: Undifferentiated employee\n” exit(0); }

  14. pure virtual function Pure Virtual Functions class Employee { public: Employee(); Employee(string tName, sring tSsn); string getName() const; string getSsn() const; double getNetPay() const; void setName(string newName); void setSsn(string newSsn); void setNetPay(double newNetPay); virtual void printCheck() = 0; private: string name; string ssn; double netPay; };

  15. Abstract Base Classes • Pure virtual functions require no definition • Forces all derived classes to define ‘theirown’ version • Class with one or more pure virtualfunctions is: abstract base class • Can only be used as base class • No objects can ever be created from it • Since it doesn’t have complete ‘definitions’ of allit’s members! • If derived class fails to define all pure’s: • It’s an abstract base class too

  16. Multiple Inheritance class Base { public: virtual void print()=0; }; class DerivedOne : public Base { public: void print() {cout<<“DerivedOne”;} }; class DerivedTwo : public Base { public: void print() {cout<<“DerivedTwo”;} };

  17. Base Base DerivedOne DerivedTwo Multiple Multiple Inheritance class Multiple : public DerivedOne,public DerivedTwo { public: void print() {DerivedTwo::print();} };

  18. Multiple Inheritance int main(){ Multiple both; DerivedOne one; DerivedTwo two; Base *array[3]; array[0] = &both; array[1] = &one; array[2] = &two; for (int i=0; i<3; i++) array[i]->print(); return 0; } Error C2594: ‘=‘: ambiguous conversion from ‘class Multiple *’ to ‘class Base *’

  19. Virtual Base Class class Base { public: virtual void print()=0; }; class DerivedOne : virtual public Base { public: void print() {cout<<“DerivedOne”;} }; class DerivedTwo : virtual public Base { public: void print() {cout<<“DerivedTwo”;} };

  20. Base DerivedOne DerivedTwo Multiple Virtual Base Class class Multiple : public DerivedOne,public DerivedTwo { public: void print() {DerivedTwo::print();} };

  21. Virtual Base Class int main(){ Multiple both; DerivedOne one; DerivedTwo two; Base *array[3]; array[0] = &both; array[1] = &one; array[2] = &two; for (int i=0; i<3; i++) array[i]->print(); return 0; } Derived Two Derived One Derived Two

  22. class Ball : public Sphere

  23. Error: Sphere has no function called getName() “Downcasting” to a Ball pointer Now it works Downcasting int main() { Ball myBall(); Sphere *spherePtr; spherePtr = &myBall; spherePtr->getName(); Ball *ballPtr; ballPtr = dynamic_cast <Ball *> (spherePtr); ballPtr->getName(); …

More Related