1 / 5

Inheritance: runtime vs. compile time binding

Mammal. gestate() giveBirth(). human. elephant. rabbit. Inheritance: runtime vs. compile time binding. virtual functions which function is actually called? determined at run-time must call through pointer or reference

Jimmy
Download Presentation

Inheritance: runtime vs. compile time binding

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. Mammal gestate() giveBirth() human elephant rabbit Inheritance: runtime vs. compile time binding • virtual functions • which function is actually called? determined at run-time • must call through pointer or reference • word “virtual” not needed in derived classes, but good idea for documenting code • non-virtual functions are bound at compile-time • base classes must have virtual destructor void bearYoung(Mammal * m) { m->gestate(); m->giveBirth(); }

  2. Trees: solving problems • Store keys, support map aka dictionary operations • insert • find • delete • We’ve used arrays/vectors, linked-lists, search trees how find insert delete vector linked list search tree • trees used for directories, expressions, games • class or struct? template or not?

  3. Tree cloning • Return a copy of a tree (who clones?) Tree * clone(Tree * t) { } • can a Tree node clone itself? delete itself? issues? • what about a mirror copy?

  4. Twenty questions (tree applications) Does it have feathers? No Is it a feline? Yes Is it the king of beasts? No Is it a tiger? No I give up, what animal are you thinking of? cheetah Ask a question that has a “yes” answer for a tiger and a “no” answer for a cheetah: does it have stripes … • how to store questions? answers? add to knowledge?

  5. Does it fly platypus robin robin Animal trees • from one node tree to many node tree • different behavior: internal nodes vs. leaves • how can tree be saved to a file (to be read later)? • Who is responsible for asking questions, printing,...

More Related