Com S 229 Fall 2011 Problem Set 4 (16 pts) Due at 10:59am Friday, November 11 Name: ___________________________ ID (4-digit) : ___________ 1. (2 points) Convert the following infix (or postfix) expressions to postfix (or infix): (a) (a + b) / (d - e) (b) (b^2 - 4 * a * c) / (2 * a) (c) a b c + * (d) a b c d e + + * * e f - * 2. (2 points) Write a function template void split(const list& aList, list& list1, list& list2); that takes a list aList and creates two new lists, list1 and list 2. The object list1 contains the first, third, fifth, and successive odd-numbered elements; list2 contains the even-numbered elements. 3. (4 points) (a) Write a function template T second (stack& s); that uses stack operations to return the second element from the top on the stack. If s.size() < 2, throw the exception underflowError (which you may assume has been defined). (b) Write a function template void n2top(stack& s, int n); that moves the nth element (counting from the top, which is the 1st element) of the stack to the top, leaving the order of all other elements unchanged. For instance, below is an integer stack before the action of n2top() on n = 4: 8 5 17 4 3 (top) After the action, it has the content: 8 17 4 3 5 (top) 4. (4 points) What is the output of the following program #include #include using namespace std; class baseCL { public: baseCL(string s, int m = 0): msg(s), n(m) {} void output() { cout << n << " " << msg << endl; } private: string msg; protected: int n; }; class derivedCL: public baseCL { public: derivedCL(int m = 1): baseCL("Base", m-1), n(m) {} void output() { cout << n << endl; baseCL::output(); } private: int n; }; int main() { baseCL bObj("Base Class", 1); derivedCL dObjA(4), dObjB; bObj.output(); dObjA.output(); dObjB.output(); return 0; } 5. (4 points) Consider the following classes: class baseCL { public: baseCL(int a): one(a) {} virtual void identify() { cout << one << endl; } protected: int one; }; class derivedCL: public baseCL { public: derivedCL(int a, int b): baseCL(a), two(b) {} virtual void identify() { cout << one << " " << two << endl; } protected: int two; }; The following functions are used to identify the classes: void announce1(baseCL x) { x.identify(); } void announce2(baseCL *x) { x->identify(); } (a) (2 points) Give the output of the following code segment: baseCL baseObj(7), *p; derivedCL derivedObj(1, 2); announce1(baseObj); announce2(&derivedObj); p = &derivedObj; p->identify(); (b) (2 points) Give the output of the following code segment baseCL *arr[3]; derivedCL derivedObj(3, 5); announce1(derivedObj); announce2(&derivedObj); for (int i=0; i<3; i++) if (i==1) arr[i] = new baseCL(7); else arr[i] = new derivedCL(i, i+1); for (i=0; i<3; i++) arr[i]->identify();