TDDD38 Advanced Programming in C++, Computer exam 2013-03-26, Comments to 6-9 ------------------------------------------------------------------------------ 6. In parameters of type string shall be 'const string&'. Grill ------ Abstractness require at least one pure virtual member function, the obvious candidate is clone(). Must have virtual destructor (noexcept) Constructor taking model and price should be protected, to emphasise abstractness. Copy constructor shall be non-public, callable by subclass copy ctors: - C++11: declare copy ctor protected and defaulted. - C++98: define copy ctor protected; initialize data members properly. Copy assignment operator shall be eliminated: - C++11: declare deleted. - C++98: declared private, no definition. clone(): const; pure virtual; return Grill*. get_model(): const; non-virtual; return string. get_price(): const; non-virtual; return string. set_price(): take double; non-virtual; return void. Data member for model should be const. Grill_Charcoal -------------- Grill shall be public base. Compiler generated destructor is fine. C++11: could be defaulted, noexcept. Public constructor taking model and price; model and price are passed to Grill ctor. Copy constructor declared as for Grill; don't allow public version to be generated by compiler). Copy assignment operator eliminated as for Grill; don't allow public version to be generated by compiler). clone(): override to create a new Grill_Charcoal object dynamically, using the copy ctor, and return pointer (Grill* or Grill_Charcoal*) to that object. Grill_LPG --------- Grill shall be public base. Compiler generated destructor is fine. C++11: could be defaulted, noexcept. Public constructor taking model, price, and lpg (default "Propane"); model and price are passed to Grill ctor. Copy constructor declared as for Grill_Charcoal. Copy assignment operator eliminated as for Grill_Charcoal. get_lpg(): const, non-virtual, return string. clone(): override to create a new Grill_LPG object dynamically, using the copy ctor, and return pointer (Grill* or Grill_LPG*) to that object. Data member for lpg: const; private, "Propane" (optional). Grill_LPG_Side -------------- Grill_LPG shall be public base. Public constructor taking model, price, and lpg (default "Prpoane"); model, price, and lpg are passed to Grill_LPG ctor. Copy constructor declared as for Grill_Charcoal. Copy assignment operator eliminated as for Grill_Charcoal. clone(): override to create a new Grill_LPG_Side object dynamically, using the copy ctor, and return pointer (Grill* or Grill_LPG*) to that object. Test program ------------ Create dynamically one of each of Grill_Charcoal, Grill_LPG and Grill_LPG_Side. For each object, make a copy using clone(), and create output from that copy. Class specific operations require dynamic_cast and/or typid expressions/ typeinfo objects. Delete each copy. Delete the dynamic objects in the vector. ------------------------------------------------------------------------------ 7. No hand-written loops are required. Use a vector for storing the values. 1: OK - Check argc to be 3; open an ifstream for argv[1], check stream state. 2: OK - declare istream_iterators and pass to the vector constructor taking two iterators, or use algorithm copy(), istream_iterators, and back_insert_iterator. 3: OK - use copy and ostream_iterator. 4: OK - use algorithm sort (basic version). 5: OK: same as 3. 6: OK: use sort and function object, or lambda expression, taking both length and lexicographical order into account, or use stable_sort and function object, or lambda expression, just taking length into account. 7: OK: same as 3 and 5. 8: OK: use unique_copy and ostream_iterator. ------------------------------------------------------------------------------ 8. Policy classes -------------- Two alternatives: 1: a template struct, parametrized on a type (T), with member function construct, taking 'const T&', and returning string. 2: an ordinary struct with member function convert as a static, template function, parametrized on a type (T), taking 'const T&', and returning string. A bit better - we take advantage of function overload deduction to instantiate convert automatically. Wrapper ------- Template with two template type parameter for data member, and for conversion policy. Constructor shall take 'const T&', and initialize value_, default value T(). get_value() shall return T. set_value shall take 'const T&', and assign value_. str() shall call policy member convert(), and return the string convert() returns. ------------------------------------------------------------------------------ 9. Function_Cache -------------- Template class with two template type parameters, for result type (Result) and parameter/argument type (Argument). Data member to store pointer to function taking Argument and returning Result, i.e. 'Result(*)(Argument)'. Constructor taking such a function pointer, and assigning the function pointer member; should be explicit. Data member std::map (obvious choise) to store Argument/Result pairs. Unary operator() shall take Argument and return Result, if given argument is found in the map, the corresponding result stored in the map is returned, otherwise the stored function is called, the result is stored in the map, and then returned; should be const (map the need to be mutable). Copy ctor and copy assignment operator shall be eliminated. Nested types result_type and argument_type should be defined (complete unary function object). Trace output in operator() showing what happens. ------------------------------------------------------------------------------