Com S 229 Fall 2011 Problem Set 2 (10 pts) Due at 10:59am Wednesday, October 5 Name: ___________________________ ID (4-digit) : ___________ 1. (1 point) Find the greatest common divisor of 462 and 1260. 2. (2 points) Correct all the errors in the definitions and usage of the two classes Circle and Rectangle below. You should make as few changes to the original code as possible. (You may cross out erroneous portions of the code and show their corrections, if any, beside in clear handwriting.) const float PI = 3.14159; class Circle { public: int Circle( float r ) : radius(r); Circle( float r1 = 0 ) { radius = r1; } float Area(void) const; float getRadius() { return radius; } private: float area, radius; } float Area(void) { area = PI * radius * radius; return area; } class Rectangle { public: Rectangle (float l; float w) : length(l), width(w) {} float Area() const { return length * width; } private: float length, width; }; void main() { Circle a(3.1); Rectangle b; b = Rectangle(5, 4); cout << "Circle a has radius " << a.radius << "and area " << a.Circle::Area() << "." << endl; cout << "Rectangle b has area " << b.Circle::Area() << "." << endl; } 3. (2 points) The box class provides measurement of a box that is defined by its length, width, and height. Store the base of the box as a rectangle object using object composition. class box { public: box(double l, double w, double h): ________________, height(h) {} double volume() // length * width * height { ____________________________ } double getHeight() { return ________________________ } private: Rectange base; // use the Rectangle class defined in the notes double height; }; (a) Complete the initialization list for the constructor so that the Rectangle object base gets initialized. (b) Give the return value for getHeight(). (c) Implement the member function volume. 4. (5 points) Begin with the following declaration and inline implementation of the accumulator class. class accumulator { public: // constructor initializes total accumulator (int value = 0) : total(value) {} // access function int getTotal() { return total; } // update the data member total void addValue(int value) { total += value; } private: // total accumulated by the object int total; }; (a) Add the overloaded operator += that has the same function as addValue(). Include both the prototype for the operator and its implementation as an external function. (b) Add the negation operator, -, to the class. The operator changes the sign of the current value for total. Give the function prototype and the implementation as an external function. (c) Add the + operator as a friend function. The operator takes two accumulator objects as arguments and returns a new accumulator object whose value is the sum of the data members. Include both the prototype and the implmentation as an external function. (d) Add an overloaded version of the output operator <<. Include both the prototype and the implementation as an external function. (e) What is the output in the following code segment? accumulator x(5), y(3), z(-9), w; x += 3; w = x + y; z = -z; cout << w << " " << z << endl;