CSE2050 Final Exam, open book, open notes. Name ________________________ 1. Circle True or False (2 pts each). F In the expression sin(x)+cos(x), sin(x) is always computed first. (the order of evaluation is undefined, but in this case the result is the same because sin(x) and cos(x) have no side effects) T The type of 2*4.5 is double. (It is 9.0, a double) F When an exception is caught, the program terminates. (execution continues from the catch statement) F If v is a vector, then *v.end() == v.back() ( *(v.end()-1) == v.back() ) T The copy() function is defined in in namespace std. F If m is a map then m.find(3) returns an int. (It returns map::iterator, which points to a pair ) T A random iterator supports all of the operations of an input iterator. (in addition to all other iterator types) T A pointer is an iterator. (in fact, it is a random iterator) T new int[10] returns type int* F When overloading an operator, its precedence order changes. (Precedence order cannot be changed) F Class member functions must always be public. (Only those intended to be part of the interface should be public) T If you do not write a class constructor, the compiler will generate one. (which default-initializes all data members and base classes) T std::vector is a templated class. T Classes that allocate memory using new usually need a destructor. (to delete the memory) T A constructor taking one argument defines an implicit conversion. F A virtual member function cannot be overridden. (in fact, it is common to override them) F A static member function must return *this (static member functions CANNOT access *this or any non-static members) F A reference of type T& may refer to an object of a base class of T. (but it may refer to a derived class) T If one of two member functions is pure virtual, the class is abstract. (because no objects of the class can be created) F A derived constructor should initialize inherited data members. (It should initialize the base class instead) F If an object is not const, you cannot call its const member functions. (But if an object is const, you can only call const members) T A header file may contain templated or inlined function definitions. (But not ordinary function definitions) F Passing parameters by value is faster than passing by const reference. (It is slower) F Only member functions can be friends. (Normally friends are non-member functions) F If p is type const char*, then p=0 is an error. (It is *p, not p, that is const) 2. Write the templated abstract base class B of class D below (15 pts): template class D: public B { T t; public: T& get() {return t;} D(const T& x): t(x) {} }; // Answer template class B { public: virtual T& get() = 0; virtual ~B() {} // Protected constructor is optional. The class is abstract without it. protected: B() {} }; 3. Write a program that reads (from standard input) a list of names and numbers (or points), and outputs the list in alphabetical order. If a name appears more than once, then your program should add up the points for each time the name occurs, and output the name only once with the sum of the point value. Example below. Hint: you can use a map. (35 pts). If the input is: Sam 3 Then the output is: Alice 4 Mike 5 Joe 2 Sam 6 Mike 15 Joe 2 Sam 11 Alice 4 Mike 10 Sam 2 // Answer (one of many) #include #include #include using namespace std; int main() { string name; int points; map m; while (cin >> name >> points) m[name] += points; for (map::const_iterator p = m.begin(); p != m.end(); ++p) cout << p->first << " " << p->second << endl; return 0; }