1 / 19

C++ Programming for Graphics Polymorphism

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

wenda
Télécharger la présentation

C++ Programming for Graphics 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. C++ Programming for GraphicsPolymorphism what it means and when it is used

  2. 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

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

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

  5. 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

  6. 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!

  7. 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

  8. 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) };

  9. 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(); }

  10. 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();

  11. 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;

  12. 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; } }

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

  14. Use this as data for linked list class List { private: Entry *head; int NoEntries; public: List() { head = 0; NoEntries=0;} ~List();

  15. linked list cont. 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++; }

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

  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 Cont. • Rewrite the example using a linked list to hold the accounts

More Related