CS3132 Quiz 1 Name ________________________ Login ______________ B00___________ 1) Consider the following. #include class V { double *data; int n; friend ostream & operator << (ostream& os, const V & v) { os << '<'; for(int i = 0; i < v.n; i++) os << v.data[i] << ' '; return os << '>'; } public: V(double d[], int m): data(d), n(m) {/*data=d; n=m;*/ } V& operator+ (const V& v) { double *d= new double[ n ]; for(int i = 0; i < n; i++) d[i] = data[i] + v.data[i]; return V( d, n ); } int size() { return n; } double operator* (const V& v){ double sum = 0.0; for(int i = 0; i < n; i++) sum += data[i] * v.data[i]; return sum; } }; main() { double a[] = {-1,0,1}, b[]={1,1,1}; V v(a,3), u(b,3), w(u + v); cout << v << ", " << w << v.size() << u*v << endl; } What would the output be? (Show results beside the line where it occurs and show each space clearly.)[3 marks] 2a) In general what would you put in the destructor for a class?[2] b) Does every class need a destructor? Explain briefly.[1] c) What is the "Law of the Big 3"?[2] d) What is a "memory leak"?[1] 3) You are to define a class PL with the methods outlined below. A PL is defined be a sequence of (x,y) points which are considered to be connected by straight-line segments. The x-values must be strictly increasing. Between the first and last point a PL is continuous and piecewise linear. It must have 2 or more points. The following illustrates the constructor. double x[] = {1.0, 2.0, 3.0, 4.0}, y[] = {1, -3, 3, -1}; PL pl (x, y, 4), single(x,y,2); The constructor should make a copy of the given arrays of points (and not just a copy of the reference to the array). This is called making a "deep copy". In the single example the last argument specifies how many values from the arrays are to be stored in the object. The constructors should throw an Exception if there are fewer than 2 points or the x values are not in ascending order. If you wish you may print a message to cerr instead. In order to be able to view such objects you should have a << method (which prints to an ostream os). E.g.: cout << pl << endl; // (1,1):(2,-3):(3,3):(4,-1) cerr << single; // (1,1):(2,-3) The comments show the expected output. Note that << methods (and the equivalent toString() methods in Java) should not include unnecessary newlines as this would preclude printing two objects on the same line. Also write the following methods. pl.points() // number of points for this PL (in this case, 4) pl.value(double x)// y value at x which is 0.0 if x out of range [12 marks] Include the "Big 3" methods. Write PL.h then PL.cc on the next page. #ifndef PL.cc