CSE2050 Fall 2003 Final Exam, open book, open notes. Name _________________ 1. Circle T for true or F for false (2 pts each). cout is a global object of type ostream. T A vector has random access iterators. T The copy() algorithm adjusts the size of the destination to fit. F The type of expression 1/3 is double. F Two functions declared in the same scope may not have the same name. F The expression a*b means either operator*(a,b) or a.operator*(b) T A member function is automatically const if it does not change the value of any data members. F A class with a virtual destructor is abstract. F A base class should always have virtual constructors. F Member functions should always be public. F Function code should not appear in header files unless templated or inlined. T When an exception is thrown, the program always terminates. F Memory allocated with new is automatically deleted when the pointer to it goes out of scope. F A const reference to base type may refer to a non-const derived object. T If p is a random access iterator, then p[i] means *(p+i) T 2. Derive templated class Array from vector such that Array supports concatenation using + as well as all vector operations (like [], size(), resize(), push_back(), iterators, etc). An Array may be initialized with a size which defaults to 0. For example: (35 pts). Array a(5), b(7), c; c.push_back("hi"); cout << (a+b+c).size(); // 13 cout << (b+c)[7]; // hi // ANSWER (there are many ways to write operator+. Two are shown below.) template class Array: public vector { public: Array(int i=0): vector(i) {} Array operator + (const Array& b) const { // As a member Array result = *this; for (int i=0; i Array operator+(Array a, const Array& b) { a.resize(a.size()+b.size()); copy(b.begin(), b.end(), a.end()-b.size()); return a; } // Notes: constructor has to initialize vector as shown // There should be no new data members // Everyhing from vector is inherited, no need to rewrite it // operator+ should return by value // Parameters should not be passed by reference // If operator+ is a member, it should be const 3. Write a program that reads words from standard input, then prints them sorted by length. Words of the same length should be printed in their original order. For example, if the input is "this is a test", the output is "a is this test ". Words are delimited on white space. There should be no limit on the length or number of words. Hint: store the input in a map. (35 pts). // ANSWER (one of many) #include #include #include using namespace std; int main() { string s; map m; // n --> concatenation of words of length n while (cin >> s) m[s.size()] += s + " "; for (map::const_iterator p=m.begin(); p!=m.end(); ++p) cout << p->second; cout << endl; // optional return 0; } // Another solution is to store words in a vector rather than concatenating #include ... map > m; // n --> vector of words of length n while (cin >> s) m[s.size()].push_back(s); for (map >::iterator p=m.begin(); p!=m.end(); ++p) for (int i=0; isecond.size()); ++i) cout << p->second[i] << ' '; // You can NOT sort a map. A map is already sorted by its key (shortest // to longest words)