cs31 lecture 15

X we have a class b a x we have we b b a a b b x iin

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: does it work? x We have a class B : A x We have We B b; A a = b; b; x IIn A, we have a virtual method foo(), and in B we have reimplemented n a method foo() method x Without dynamic dispatch, a.foo() is the implemementation in A x With dynamic dispatch, a.foo() is either A::foo() or B::foo(), With depending on what a is storing depending No dynamic dispatch: A a = A(); With dynamic dispatch: A* a = new B(); // or new A(); 6 Examples 7 Pointers versus references x In C++, one key reason of constructors/destructors is In when pointers are used as attribute when x References give you less freedom than pointers x Pointers are more error-prone than references x std::vector<A&> is not possible, but std::vector<A*> is std::vector<A&> possible possible 8 A new data structure: trees x A tree is made of nodes, which have a unique parent, and tree possibly many children possibly x A good example: a document tree (a HTML page) x Design: Design: Each node is a Node class Each particular node type inherits from Node Each particular node has its own print function We can make vector of Node We Printing a document is about traversing the tree, and calling the Printing actual print method of each node actual 9...
View Full Document

This note was uploaded on 04/03/2014 for the course CS 31 taught by Professor Melkanoff during the Fall '00 term at UCLA.

Ask a homework question - tutors are online