190 likes | 299 Views
C++ Programming for Graphics Polymorphism. what it means and when it is used. Meaning. 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
E N D
C++ Programming for GraphicsPolymorphism what it means and when it is used
Meaning 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 { protected: int j,k; public: virtual void vf(){ cout << “vf: parent\n”;} void nvf() {cout <<“nvf: parent\n”;} };
inherit the class • class Child: public Parent { int m,n; public: void vf(){cout << “vf:child\n”;} void nvf(){cout << “nvf:child\n”;} };
Using the classes • 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 behavior • 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
Array of accounts COLLECTIONS • 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]; cout<< acs[0]->balance();
Deleting Objects • Once an object created with new is finished 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 class Entry{ public: Entry *next; Object *info; Entry( Object *obj ) {info = obj; next = 0; } ~Entry() { if( ! (info==0) ) delete info; } };
Use this as data for linked list class List { private: Entry *head; int NoEntries; public: List() { head = 0; NoEntries=0;} ~List();
linked list cont. 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++; }
Destructor • List::~List() { while(head != 0) { Entry* temp = head; head = head-> next; delete temp; } }
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 Cont. • Rewrite the example using a linked list to hold the accounts