CSE2050 Fall 2004 Final Exam, open book, open notes. Name _________________ 1. What does the following print? (2 pts each) ANSWERS char a[]="mouse"; cout << a+2; use cout << *(a+2); u cout << char(*a+2); o ('m'+2) cout << &a[2]-a; 2 (*a)--; cout << a; louse 2. Write a templated function that will print the elements of a vector, list, or deque (assuming printable elements) using the normal << notation. Elements should be separated by spaces. For example: (30 pts) char *s = "abc"; list l(s, s+3); vector v(l.begin(), l.end()); // ASCII codes of a, b, c deque d(v.begin(), v.end()); cout << l << v << d; // a b c 97 98 99 97.0000 98.0000 99.0000 // ANSWER template ostream& operator << (ostream& out, const T& x) { for (typename T::const_iterator p=x.begin(); p!=x.end(); ++p) out << *p << ' '; // cannot use x[i] on a list return out; } 3. Class D lacks some of the methods required by the rule of 3. Write the base class B so that the compiler will disallow attempts to use these faulty operations in D. Make B abstract. (30 pts) class D: public B { int *p; public: D(int i): p(new int[i]) {} int& get(int i) {return p[i];} ~D() {delete[] p;} }; // ANSWER class B { public: virtual int& get(int i) = 0; // pure virtual virtual ~B() {} // NOT pure protected: B() {} // optional if there is at least one pure virtual function private: // The default copy constructor and assignment operator for D will // call the corresponding operations on B. If they are private in B, // then an attempt to use them on a D will cause a compiler error. B(const B&); // NOT virtual, implementation is optional B& operator=(const B&); // NOT virtual, implementation is optional }; 4. Write a program that takes real numbers as arguments and prints the number of arguments, not counting duplicates. (Hint, use a map). For example: (30 pts) count 5.2 4.99 5.2 -8 -8.0 3 // ANWWER #include #include #include // for atof() using namespace std; int main(int argc, char** argv) { map m; // or map for (int i=1; i