Comp 1672, Winter 2003 Homework 6 Solutions 1. class declaration: class CC { public: CC(); // line 1 CC(int); // line 2 CC(int, int); // line 3 CC(double, int); CC(const CC&); const CC& operator=(const CC &); private: int u; double v; } a. i. CC one; // line 1, default constructor ii. CC two(5, 6); // line 3, initialize both values with ints iii. CC three(3.5, 8) // line 4, initialize with a double and an int b. CC::CC() // default constructor : u(0), v(0) { c. CC::CC(int i) : u(i), v(0) { } d. CC::CC(int uvalue, int vvalue) : u(uvalue), v(vvalue) { } e. CC::CC(double vvalue, int uvalue) : u(uvalue), v(vvalue) { } f. const CC & operator=(const CC&rhs) { if (&rhs != this) { this.u = rhs.u; this.v = rhs.v; } return *this; } 2. a. F b. F c. F d. T e. T g. F h. F 3. 35 78 78 78 4. 78 78 The code has a memory leak - the allocated memory is never released. To fix this, it must be deleted, and at exactly the correct point in the program. That is, delete the memory after you are done using it and before you re-use the pointer that ponts to it. int *p; int *q p = new int; // p points to the first allocated integer *p = 43; // set the value of first allocated integer q = p; // now q also points to the first allocated integer *q = 52; // change the value of first allocated integer p = new int; // p now points to the second allocated integer *p = 78; // set the value of the second allocated integer delete q; // We're done with the first integer - release it q = new int; // allocate a third integer *q = *p; // copy the value from the second integer to the third cout << *p << " " << *q << endl; // output 2nd and 3rd values - both are 78 delete p; // release the 2nd integer delete q; // release the 3rd integer