Com S 229 Fall 2011 Problem Set 3 (10 pts) Due at 10:59am Friday, October 28 Name: ___________________________ ID (4-digit) : ___________ 1. (2 points) Run the following program and explain its behavior. (Placing debugging information inside constructors and destructors is a very useful step in developing efficient and correct clases.) #include class A { public: A(int n): xx(n) { cout << "A(int " << n << ") called" << endl; } A(double y): xx(y+0.5) { cout << "A(fl " << y << ") called" << endl; } ~A( ) { cout << "~A( ) with A::xx = " << xx << endl; } private: int xx; }; int main( ) { cout << "enter main\n"; int x = 14; float y = 17.3; A z(11), zz(11.5), zzz(0); cout << "\nOBJECT ALLOCATION LAYOUT\n"; cout << "\nx is at " << &x; cout << "\ny is at " << &y; cout << "\nz is at " << &z; cout << "\nzz is at " << &zz; cout << "\nzzz is at " << &zzz; cout << "\n__________________________\n"; zzz = A(x); zzz = A(y); cout << "exit main" << endl; } 2. (6 points) Consider the class dynamicInt, which has single dynamic data member: class dynamicInt { public: dynamicInt(int m = 0); ... // destructor dynamicInt(const dynamicInt& obj); // copy constructor ... // assignment operator int getData() const; // return value of dynamic member private: int *ptr; // dynamic data member }; (a) Define the destructor for dynamicInt. (b) Define the overloaded assignment opeartor. (c) Implement the copy constructor. Throw the memoryAllocationError exception if no dynamic memory is available. (d) The function f() takes a dynamicInt object as an argument and has a dynamicInt object as its return value. The function is called in the main program. dynamicInt f(dynamicInt obj) { dynamicInt localObj(5 + obj.getData()); return localObj; } void main( ) { dynamicInt obj1(10), obj2; obj2 = f(obj1); cout << obj2.getData() << end; } (i) Indicate the number of times that the copy constructor is used. Comment to the right of the corresponding lines where the calls happen. (ii) Indicate all of the times that the destructor is called. (iii) What is the output value from obj2.getData()? 3. (2 points) Use the following declaration for parts (a)-(d): rectangle *r; where the class rectangle is defined below. class rectangle { public: // constructor rectangle(double len = 0.0, double wid = 0.0) length(len), width(wid) {} // return the area double area() const { return length * width; } // return the perimeter double perimeter() const { return 2 * (length + width); } // change the dimensions of the rectange to len and wid void setSides(double len, double wid) { length = len; width = wid; } // return the length of the rectangle double getLength() const { return length; } // return the width of the rectangle double getWidth(void) const { return width; } private: double length, width; }; (a) Dynamically allocate a six-by-eight rectangle. (b) Use the dereference opeartor, *, and the dot operator to output the perimeter of the rectangle. (c) Use the -> operator to output the area of the rectangle. (d) Using setSides() to, double the length and width of the rectangle.