Exercise 9, spring 2006 ----------------------- The "Law Of The Big Three" says that if a class needs any of: destructor, copy constructor, assignment operator, it generally needs them all. Why is that? In some of the previous exercises there have been classes with destructors but with no copy constructor or no assignment operator, and still they have worked. Well, they do work if one avoids the problems caused by the absence of copy constructor and assignment operator, but maybe the following example explains a bit why such 'law' exists: class Array { private: int* pData; int size; public: Array(int i) : size(i), pData(new int[i]) {} //Constructor. Allocating memory. ~Array() {delete[] pData;} //Destructor. Freeing memory. void Function(Array a) {cout << "Nothing else done here" << endl;} }; int main() { Array a(1); Array b(1); //If there is no assignment operator = defined in the class, what does the following line do? a = b; //What goes wrong? //So assignment operator is needed, but why a copy constructor is needed if a assignment operator //has already been defined correctly? That is because in certain cases, where it might seem quite natural that //the assignment operator is used, instead a copy constructor is used. This happens //for example in initializations and when passing arguments to functions(so quite frequently). Array c = a; //Copy constructor is used here instead of operator=(...). a.Function(c); . . } 1. As an example, add copy constructor to class Vector done in exercise 2 - the code can be found from http://cc.oulu.fi/~tf/tiedostot/pub/atkIII/harjkoodi/h2/ 2. Define operator== for the class. The method should return true if two Vectors are equal, and false if they are not. 3. Add a method At(unsigned int i) to the class, which returns the i:th element if i is within correct range, and throws an exception if it's out of range. Use try-catch to catch the exception, whose type you can decide, in the main program.