Comp 272 Final exam study guide Dordal The final exam will be Friday, April 16, 8:30 am, in our usual room. The exam will, like the midterm, be open-book. It will require a general cumulative familiarity with C++. The following specific material may be on the exam (I will provide crossreferences to the text soon): Chapter 10 basics of C++ pointers (10.1) native arrays and incrementing pointers (10.2) basics of dynamic memory and new (10.3; now with "delete" too) C strings as char* Chapter 12 Basics of inheritance (12.1) static v dynamic inheritance virtual functions and Shapes hierarchy (12.4 and some 12.5) More 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 Points, Circles, Shapes virtual functions base v derived class heterogeneous collections pointer assignment, slicing, pointer v direct semantics problems with use of "protected:" fields More Pointers: references returning references, reference parameters the implicit parameter, "this" pointer v "direct" naming of objects three uses of pointers: polymorphism, sharing, 0/1 relationships shallow v deep copying; clone() recursive checks for equality operators: operator overloading operator X as a member function v. defined outside the class <<, ==, [], (), =, + versus += type conversion 1-parameter ctors as type conversions "explicit" keyword and what confusion it prevents operator X type conversion defined within class Y to implement conversion Y => X memory management: basic use of new/delete classes with internal pointers to dynamic memory destructors versus delete destructors act on objects; delete acts on pointers copy constructor what it does when one is needed versus clone(); use of copy ctor to implement clone() dtor, copy ctor, operator= why the above are a package deal templates: basics of using template classes why templates are important basics of writing template classes (but no tricky declarations!) 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) and the Polynomial demo. Shapes general polymorphism of draw() Group copy ctor for Group, clone(), shallow v deep copy Group::move() abstract base class Game concrete base class for default response special rooms with specialized responses double dispatch -- recognition of the problem, basic techniques Polynomial/String2 why the copy ctor, operator=, and destructor are typically found together problems with shared structure =============================================================== The following are some sample questions. Solutions should be available Wednesday night. Also review the midterm study guide for material related to inheritance. 1. Write member function equals(Shape* s2) for a GroupShape class. Explain why it has to be virtual. The following example may help, but your GroupShape version should be sure to do a "deep" comparison, comparing each element in turn. Note the use of the typeid() operator (which will not appear on the final) used here to determine if s2 is really a pointer to a PolygonShape; if it's not, then the two are automatically not equal. bool PolygonShape::equals(Shape * s2) { if (typeid(s2)!=typeid(PolygonShape)) return false; Polygon * p2 = (Polygon *) s2; // cast! if (*this != *p2) return false; else return true; } Assume that the GroupShape data consists of a vector of Shape*: class GroupShape { private: vector _shapes; ... }; Ignore things like color and the base location. 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 GroupShape is one of the classes derived from Shape, and is the only one with embedded Shape pointers. Give the implementation of GroupShape::clone(). 3. Write declarations for member functions of a Date class to implement the type conversion string => Date. Do not write the function *definition*. 4. Assume you have a LinkedList class such as the one below. 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 GroupShape G2 (from the Shape class) and then execute GroupShape 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 GroupShape 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 +=). ("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.) (b). Write operator+=(vector v1, vector v2) so that the corresponding *components* of v2 are added to v1, eg: for (i=0; i