1 / 19

Polymorphism

Polymorphism. what it means and when it is used. Introduction. Last lecture we reviewed the concept of inheritance discussed how to implement inheritance in C++ This lecture we will introduce polymorphism explain virtual and pure virtual functions

adora
Download Presentation

Polymorphism

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 what it means and when it is used

  2. Introduction Last lecture we • reviewed the concept of inheritance • discussed how to implement inheritance in C++ This lecture we will • introduce polymorphism • explain virtual and pure virtual functions • learn how to use polymorphism in C++ programs

  3. Meaning Greek origins – “poly - many morph-form” The ability to have many forms – objects with different internal structures respond to a similar external interface. It is sometimes useful to be able to send a group of objects a particular message eg ‘display()’ and get them to respond in their own way It would also be useful to not be restricted at compile time by the type(class) of the objects but to get this sorted at run time eg. displaying a list of graphical objects

  4. Declare virtual functions • class Parent class Parent { protected: int j,k; public: virtual void vf(){ cout << “vf: parent\n”;} void nvf() {cout <<“nvf: parent\n”;} };

  5. inherit the class • class Child class Child: public Parent{ int m,n; public: void vf(){cout << “vf:child\n”;} void nvf(){cout << “nvf:child\n”;} };

  6. Using the classes • within the main() function: Parent prnt; vf: parent Child chld; prnt.vf(); prnt.nvf(); chld.vf(); chld.nvf(); Parent *pp = &prnt; pp->vf(); pp->nvf(); nvf: parent vf:child nvf:child

  7. polymorphic behaviour • pp = &chld; pp -> vf(); pp->nvf(); produces : vf:child nvf:parent for a virtual function the class of the object pointed to determines which function definition is used!

  8. Pointers and Polymorphism • The names of virtual functions are bound at RUN TIME, specifically at the time of each function call (LATE BINDING).The binding is determined by the class of the object pointed to by the pointer variable at the time of the function call

  9. COLLECTIONS Array of accounts • Array initialisation int b[5] = {75,25,100,-45,60}; Account acct[3] = {Account(500,5,0.6), Account(), Account(100.0) };

  10. An array of various accounts • main() { Savings acc1(100.0); Time acc2(500.0, 100.0); Investment acc3(101.5, 6.5); Account* staffacct[3] = { &acc1, &acc2, &acc3} for(i = 0; i<3; i++) { staffacct[i] -> print(); }

  11. Arrays and new • Arrays can be created with new • int* p = new int[10]; • elements are accessed using subscripts p[0],p[1] ... • similarly for objects: Account * acs = new Account[20];

  12. Deleting Objects • Once an object created with new is finished with it can be destroyed using delete • delete ac1; • If the pointer points to an array it is necessary to tell C++ this by writing: • delete [ ] p;

  13. ABSTRACT BASE CLASSES • class Object { public: Object(){;} virtual ~Object(){;} virtual ostream& printOn(ostream&) const = 0; friend ostream& operator<<(ostream& out, const Object& ob) { ob.printOn(out); return out; } }

  14. A linked List Node • class Entry{ public: Entry *next; Object *info; Entry( Object *obj ) {info = obj; next = 0; } ~Entry() { if( ! (info==0) ) delete info; } }; info next Entry attributes: obj

  15. Use this as data for linked list class List { private: Entry *head; int NoEntries; public: List() { head = 0; NoEntries=0;} ~List(); Entry* returnHead() const { return head ;} void add( Object& ); int isEmpty(){return head == 0;} };

  16. Linked list void List::add( Object& newEntry ) { Entry *newElement = new Entry( &newEntry ); newElement->next = head; head = newElement; NoEntries++; } info next newElement

  17. Destructor • List::~List() { while(head != 0) { Entry* temp = head; head = head-> next; delete temp; } } info next info next info next head

  18. Tutorial Create an array of pointers to accounts. Enter addresses of various account objects into this array - include a pointer to an object of each type of account previously developed. Send each object pointed to suitable messages to illustrate the polymorphic effect. Remove the virtual keyword and note the new behaviour

  19. Tutorial Contd • Rewrite the example using a linked list to hold the accounts

More Related