CSE390C Sample Final, Spring 2022 handout #7 1. Inheritance Mystery (15 points). Assuming that the following classes have been defined: class foo { public: virtual void f1() { cout << "foo 1" << endl; } void f2() { cout << "foo 2" << endl; } void f3() { cout << "foo 3" << endl; } }; class bar : public foo { public: virtual void f2() { cout << "bar 2" << endl; } void f3() { cout << "bar 3" << endl; } }; class baz : public foo { public: void f1() { cout << "baz 1" << endl; } virtual void f2() { cout << "baz 2" << endl; } void f3() { cout << "baz 3" << endl; } }; class mumble : public baz { public: void f1() { cout << "mumble 1" << endl; } void f2() { cout << "mumble 2" << endl; } }; And assuming the following variables have been defined: foo * var1 = new mumble(); baz * var2 = new mumble(); foo * var3 = new bar(); foo * var4 = new baz(); baz * var5 = new baz(); In the table below, indicate in the right-hand column the output produced by the statement in the left-hand column. Statement Output ------------------------------------------------------------ var1->f1(); ____________________________ var2->f1(); ____________________________ var3->f1(); ____________________________ var4->f1(); ____________________________ var5->f1(); ____________________________ var1->f2(); ____________________________ var2->f2(); ____________________________ var3->f2(); ____________________________ var4->f2(); ____________________________ var5->f2(); ____________________________ var1->f3(); ____________________________ var2->f3(); ____________________________ var3->f3(); ____________________________ var4->f3(); ____________________________ var5->f3(); ____________________________ 2. Class definition (25 points). Write a version of the point class for storing two ints called x and y where the ints are heap-allocated by calling new. Your class should have the following member functions: point(x, y) construct a point from values x and y (default 0) point(other_p) construct a point from another point get_x() returns x value get_y() returns y value set_location(x, y) changes the x/y location to new ints x/y Your class should have a destructor and should override the assignment operator. You must write your class in such a way that all memory allocated by your point objects is freed up by calls on delete. You may assume that this class is not part of an inheritance hierarchy. 3. STL Programming (10 points). Write a function called remove_3s that takes a set of int values as a parameter and that removes all multiples of 3 from the set. For example, if a set s contains these values: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12] and you make the call: remove_3s(s); Then s should store these values after the function call: [1, 2, 4, 5, 7, 8, 10, 11] 4. STL Programming (15 points). Write a function called string_indexes that takes a vector of string values as a parameter and that returns a map whose keys are the strings from the list and whose values are sets containing the indexes of where those strings appeared in the list. For example, if a vector called words contains this sequence: [to, be, or, not, to, be, could, be, a, or, not, be, how, much, wood, can, a, woodchuck, chuck, if, a, woodchuck, could, chuck, wood] then the call string_indexes(words) should return a map with the following pairs: (a, [8, 16, 20]), (be, [1, 5, 7, 11]), (can, [15]), (chuck, [18, 23]), (could, [6, 22]), (how, [12]), (if, [19]), (much, [13]), (not, [3, 10]), (or, [2, 9]), (to, [0, 4]), (wood, [14, 24]), (woodchuck, [17, 21]) 5. STL Programming (10 points). Write a function called sort_reverse that takes a vector of int values as a parameter and that sorts the first half of the vector and that reverses the order of the second half of the vector. If the vector has an odd length, then include the middle element in the first half that is to be sorted. For example, if a vector v contains: [3, 8, 4, 5, 12, 17, 2, 8, 5, 19, 27, 13, 6] | | | | +-------------------+ +-----------------+ first half second half and you make the call: sort_reverse(v); Then after the call v should contain this sequence: [2, 3, 4, 5, 8, 12, 17, 6, 13, 27, 19, 5, 8] | | | | +------------------+ +-----------------+ sorted reversed Notice that the value 2 that was in the middle of the vector of 13 values was included in the group of values that was put into sorted order and not the group of values that was reversed. 6. Inheritance (20 points) Write a base class called sequence that can be used to manipulate a sequence of integers and a derived class called sum that extends the base class to implement a summation. The idea is that we might want to keep track of different kinds of sequences that can be reduced to a single int with different operations. For example, if the sequence stores: [8, 42, 30, 74, 98] A sum object would add up these values: 8 + 42 + 30 + 74 + 98 Other kinds of classes could be defined. For example, we might define a product class that multiplies the values. The sequence class should have the following public member functions: sequence(empty) constructs an empty sequence with given empty value to return if reduce is called on the empty sequence add(value) appends this value to the end of the sequence f(a, b) abstract function for reducing two ints to one reduce() result of applying f to reduce the sequence to a single int The various subclasses should provide a value to use for an empty reduce call and should define function f. For the sum class, it should indicate that 0 is the appropriate value to return for an empty reduce and the function f should be addition. The reduce operation should be performed left-to-right. For example, if a sequence stores: [3, 8, 19, 42, 207] then reduce should compute its answer as: f(f(f(f(3, 8), 19), 42), 207) For example, given the following client code: sequence * s = new sum(); s->add(8); s->add(42); s->add(30); cout << s->reduce() << endl; delete s; we would expect it to print that the result is 58. Your class should be designed so that it does not introduce any memory leaks. 7. Short answer (5 points). Consider the following function: int & mystery(int & x) { int y = 2 * x + 1; return y; } C++ will not allow this function to compile. Why not?
Stuart Reges
Last modified: Thu Jun 2 08:24:44 PDT 2022