Comp 272 Final exam study guide Dordal The final exam will be Friday, May 4, at 8:00 in our usual room. The following material may be on the exam: chapter 8: all, especially: virtual functions base v derived class heterogeneous collections pointer assignment, slicing, pointer v direct semantics chapter 10: references returning references, reference parameters the implicit parameter, "this" pointer v "direct" naming of objects const, casting away constness three uses of pointers: polymorphism, sharing, 0/1 relationships Testing for a type match with typeid shallow v deep copying; clone() recursive checks for equality You do *not* need to know about the chapter-10 topics of testing for downcasting, or virtual constructors, or "reflection", or static/dynamic binding. chapter 11: Won't be on the exam, but know what :: is. chapter 12: operator overloading operator X as a member function v. defined outside the class <<, ==, [], (), =, + versus += type conversion 1-param ctors as type conversions operator type conversions "explicit" keyword and what confusion it prevents chapter 14: basic use of new/delete classes with internal pointers to dynamic memory destructors versus delete dtor, copy ctor, operator= why the above are a package deal In addition, you should be familiar with the Shapes project and the Game project -- in particular how polymorphism was used in these -- and also with the memory-management issues implicit in program 6. Shapes general polymorphism of plot() Group copy ctor for Group, clone(), shallow v deep copy dtor, operator= for Group abstract base class Game concrete base class for default response special rooms with specialized responses word interpretation lookup =============================================================== The following are some sample questions. Solutions should be available wednesday. Also review the midterm study guide for material from chapter 8. 1. Write member function equals(Shape* s2) for the Shape class. Explain why it has to be virtual. For most derived classes, it suffices to follow the example bool Polygon::equals(Shape * s2) { if (typeid(s2)!=typeid(Polygon)) return false; Polygon * p2 = (Polygon *) s2; // cast! if (*this != *p2) return false; else return true; } Explain why typeid() is used here. Give Group::equals(Shape* s2) and explain why it does not fit the above rule. 2. State what has to be provided for a Shape::clone() operation that returns a Shape* that represents a deep copy of the original. Assume that Group is one of the classes derived from Shape, and is the only one with embedded Shape pointers. Give the implementation of Group::clone(). 3. Write declarations for member functions of a Date class as described in Horstmann for conversions Date => String and String => Date. Do not write the function *definitions*. 4. Assume you have a List class like that on p. 369 of Horstmann. Give a full declaration and implementation of operator[] so that L[n] returns a reference to the nth element (starting at 0) of list L. Explain the difference between returning a reference to the nth element, and returning the nth element's value. 5. Suppose we have a Group G2 (from the Shape class) and then execute Group G = G2; G.move(100,0); // move entire group 100 units to the right G.plot(g); G2.plot(g); How will the output of the two plot() calls compare if the copy constructor for Group does a (a) shallow copy? (b) deep copy? 6. For classes with dynamically allocated memory, why do you need operator= separately from the copy constructor? 7. Write operator+=(vector v1, vector v2) so that the items in v2 are copied to the end of v1.