CSE2050 Final Exam, open books, open notes. Name _________________________ 1. What operator is defined for bidirectional iterators but not for forward iterators? (2 pts each) -- What type of iterator is char* ? Random For a map, what operator must be defined for K but not V? < (A map is sorted on the key K). Given 3 classes Car, Vehicle, Wheel, which would be a base class? Vehicle (A Car IS A Vehicle, Car is derived). 2. What does the following print? (2 pts each) char s[]="STRING", *p=s+3; cout << p; // ING (type is char*, prints up to '\0') cout << p[-1]; // R (means *(p-1), type is char) cout << --*++p; // M (decrements the 'N') 3. TRUE OR FALSE (1 pt each) Circle one If a parameter is passed by reference, the argument must be const. F (it must be a variable that can be assigned to). A class with an empty virtual destructor needs a copy constructor and an assignment operator. F (rule of 3 applies to non-empty destructors). A class is abstract if all of its constructors are protected. T A class is abstract if it has virtual functions. F (it would be abstract with pure (=0) virtual functions) If a constructor does not have an initialization list, then all data members of that class must have default constructors. T (the compiler will insert default initializations). A polymorphic data structure is one in which all of its elements have the same type. F A member function that returns a reference to a data member should be const. F (that would allow users to modify const objects through the reference). When an exception is caught, execution resumes at the point where it was thrown. F (it resumes at the catch clause). 4. Given a program in two source code files, prog1.cpp and prog2.cpp, that share a common header file, prog.h, write the 2 UNIX/Linux commands to compile and run the program, reporting the execution time to run. (10 pts) g++ prog1.cpp prog2.cpp (NOT prog.h) time a.out 5. Write code to copy the array into the vector. (10 pts) int a[5] = {1,6,3,5,4}; vector v; // empty for (int i=0; i<5; ++i) v.push_back(a[i]); or copy(a, a+5, back_inserter(v)); or v = vector(5); copy(a, a+5, v.begin()); or v = vector(a, a+5); 6. Write a templated function that takes a vector of any type and returns the largest element. For example, with v above: (Use back of page, 10 pts) cout << largest(v) << endl; // prints 6 // Assume v is not empty, and < is defined for T template const T& largest(const vector& v) { // May also pass/return by value return *max_element(v.begin(), v.end()); } or template T largest(vector v) { // pass by value or make local copy to sort sort(v.begin(), v.end()); return v[v.size()-1]; } or template T largest(const vector& v) { // return by value T result=v[0]; // NOT =0, T might not be int, or all elements negative for (int i=1; i T largest(const vector& v) { // may also return const T& vector::const_iterator result=v.begin(), p; for (p=v.begin(); p!=v.end(); ++p) if (*result < *p) result = p; return *result; } 7. A square mile is 640 acres. The classes Miles and Acres are derived from class Units, shown below. All classes have constructors taking double. Write class Acres. Overload * to multiply Miles * Miles and return Acres. Overload << to print Acres as below. (Use back of page, 25 pts) class Units { private: double value; public: Units(double x): value(x) {}; double to_double() const {return value;} }; Miles m2=2.0, m5=5.0; cout << m2*m5 << endl; // Prints "6400 acres" class Acres: public Units { // inherits value, to_double() public: Acres(double x): Units(x) {} // not inherited, must init base class }; // * is not a member function (if it was, it would be a member of Miles) Acres operator * (const Miles& m1, const Miles& m2) { // return by value return m1.to_double() * m2.to_double() * 640; // value is private } // Not a member function ostream& operator << (ostream& out, const Acres& a) { return out << a.to_double() << " acres"; }