CSE2050 Spring 2004 Final Exam, open book, open notes. Name ________________ 1. True or false? Circle T or F (2 pts each). ANSWERS (with explanations in parenthesis) T std is a namespace. F cout is a class. (it is an object) T The . operator has higher precedence than = F 10 * 0.1 == 1 exactly. (due to roundoff error, 0.1 has no exact representation) F The following sorts array x: int x[10]; sort(x.begin(), x.end()); (sort(x, x+10);) F An uncaught exception has no effect on program execution. (the program terminates) F If a and b are vectors, then copy(a.begin(), a.end(), b.begin()); changes b.size() to a.size(). (if b is smaller then copy() writes out of bounds) F Expressions can only be passed to a function by reference. (they can only be passed by value or const reference) F map,int>::iterator is a random access iterator type. (bidirectional) F If m is a map and m[x] == y then m.find(x) == y (find() returns an iterator, m.find(x)->second == y) T swap() is a templated function in . T An algorithm that accepts input iterators will also accept pointers. T If all base class constructors require arguments, then all constructors in derived classes must have initialization lists. F If X and X::Y are classes, then X::f() may access protected data in X::Y T If x1 and x2 are type X, then x1.f() may access private data in x2. T The following may cause a segmentation fault: char *s; cin >> s; (because s points to a random place in memory where cin writes) F delete p; throws bad_alloc if p == 0 (delete 0; has no effect) T Every class has at least one constructor even if you don't write any. (it has a copy constructor, also a default constuctor if you don't write one) F If X is a class, then X::X():f(0){} means to call function f with argument 0 whenever an X object is created. (it means to initialize the data member or base class f with 0) F A constructor taking no arguments defines an implicit conversion. (but a constructor taking 1 argument does) F A default assignment operator does a bitwise copy of all data members. (it uses the appropriate assignment operator for each data member) F A derived class should initialize protected inherited data members. (the base class should do this) F A derived class should always declare a virtual destructor. (but a base class with virtual functions should) T All code in a header file should be templated or inlined. F A reference of type T& is not allowed if T has pure virtual functions. (in fact this is normal practice) 2. Write an interface for class Person. Add member functions required by the rule of 3 (if any) to both classes (50 pts). class Person: public Printable { string* name; // 2 element array containing first and last name public: void print() const {cout << name[0] << " " << name[1];} Person(const char* first, const char* last=""): name(new string[2]) { name[0]=first; name[1]=last; } // ANSWER ~Person() {delete[] name;} // don't forget the [] Person(const Person& p): name(new string[2]) { // copy constructor name[0] = p.name[0]; name[1] = p.name[1]; } // No need to check for assignment to self since size doesn't change Person& operator = (const Person& p) { name[0] = p.name[0]; name[1] = p.name[1]; return *this; } }; // Interface (would normally appear before class Person) class Printable { public: virtual void print() const = 0; virtual ~Printable() {} };