Comp 272 Final exam study guide Dordal The final exam will be Wednesday, December 11, 2002, 10:30 am, in our usual room. The following material may be on the exam: General facts about C++ (the stuff in Chapter 2; you probably know this pretty well by now) chapter 7: basics of inheritance derived class inheriting an operation from the base class derived class extending a base-class operation derived class replacing a base-class operation examples of good and bad inheritance; the "is-a" rule 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 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 "explicit" keyword and what confusion it prevents (we never covered "operator" type conversions) 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 5 (strings). 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 =============================================================== The following are some sample questions. Solutions should be available later tonight. Also review the midterm study guide for material from chapters 7 (and maybe 8?). 1. Write member function equals(Shape* s2) for the Group class. Explain why it has to be virtual. The following example may help, but your Group version should be sure to do a "deep" comparison. 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. 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 conversion string => Date. Do not write the function *definition*. 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 (like string +=). ("vector" is the vector "template" type made specific to ints; if you wish, you can assume that the types are "intvector", that works like vector except only for int.)