210 likes | 417 Views
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
E N D
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 • learn how to use polymorphism in C++ programs
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
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”;} };
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”;} };
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
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!
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
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) };
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(); }
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];
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;
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; } }
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
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;} };
Linked list void List::add( Object& newEntry ) { Entry *newElement = new Entry( &newEntry ); newElement->next = head; head = newElement; NoEntries++; } info next newElement
Destructor • List::~List() { while(head != 0) { Entry* temp = head; head = head-> next; delete temp; } } info next info next info next head
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
Tutorial Contd • Rewrite the example using a linked list to hold the accounts