Presentation is loading. Please wait.

Presentation is loading. Please wait.

J. Byrne Polymorphism what it means and when it is used.

Similar presentations


Presentation on theme: "J. Byrne Polymorphism what it means and when it is used."— Presentation transcript:

1 J. Byrne Polymorphism what it means and when it is used

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

3 J. Byrne 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 J. Byrne Declare virtual functions F class Parent class Parent { protected: int j,k; public: virtual virtual void vf(){ cout << “vf: parent\n”;} void nvf() {cout <<“nvf: parent\n”;} };

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

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

7 J. Byrne polymorphic behaviour F 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 J. Byrne Pointers and Polymorphism F 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 J. Byrne Array of accounts F Array initialisation int b[5] = {75,25,100,-45,60}; Account acct[3] = {Account(500,5,0.6), Account(), Account(100.0) }; COLLECTIONS

10 J. Byrne An array of various accounts F 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 J. Byrne Arrays and new F Arrays can be created with new F int* p = new int[10]; F elements are accessed using subscripts p[0],p[1]... F similarly for objects: Account * acs = new Account[20];

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

13 J. Byrne ABSTRACT BASE CLASSES F 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 J. Byrne A linked List Node F class Entry{ public: Entry *next; Object *info; Entry( Object *obj ) {info = obj; next = 0; } ~Entry() { if( ! (info==0) ) delete info; } }; infonext Entry attributes: obj

15 J. Byrne 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 J. Byrne Linked list void List::add( Object& newEntry ) { Entry *newElement = new Entry( &newEntry ); newElement->next = head; head = newElement; NoEntries++; } infonext newElement

17 J. Byrne Destructor F List::~List() { while(head != 0) { Entry* temp = head; head = head-> next; delete temp; } infonextinfonext infonext head

18 J. Byrne 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 J. Byrne Tutorial Contd F Rewrite the example using a linked list to hold the accounts


Download ppt "J. Byrne Polymorphism what it means and when it is used."

Similar presentations


Ads by Google