Comp 272 Final exam study guide Dordal The final exam will be Tuesday, Dec 14, 2004, at 8: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 typeid (basics) downcasting with dynamic_cast<> shallow v deep copying; clone() recursive checks for equality (basics) double dispatch (simple cases, at least) 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 "operator" type conversions (for converting the other direction) 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 chapter 15: templates (basics, pp 389-395) 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 (string2). 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. Also review the midterm study guide for material from chapter 7. 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; // Polygon == else return true; } (An alternative to the if (typeid(s2)!=typeid(Polygon)) return false; Polygon * p2 = (Polygon *) s2; // cast! is: Polygon * p2 = dynamic_cast (s2); if (p2==0) return false; ) 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 LinkedList class such as the one below (similar to Horstmann's on p 363). 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. Explain why operator[] doesn't allow addition of *new* elements to the list, just access to and update of existing elements. class LinkedList { private: class cell { public: double _data; cell * _next; cell(double d, cell* n) : _data(d), _next(n) {} }; cell * head; public: LinkedList() : _head(0) {} void addtofront(double d) {_head = new cell(d, _head);} }; 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. (a) Write operator+=(vector v1, vector v2) so that the items in v2 are copied to the end of v1 (like string +=). (b). Write operator+=(vector v1, vector v2) so that the corresponding *components* of v2 are added to v1, eg: for (i=0; i