/*********************************************************************** MEMORIAL UNIVERSITY OF NEWFOUNDLAND Faculty of Engineering and Applied Science Engineering 3891(Advanced Programming) Assignment #7 Instructor: Michael Bruce-Lockhart Date: 00.11.15 Due: 00.11.24 ***********************************************************************/ /* A different kind of vector problem. */ // Possible error states - CHANGE no memory, index out of range, non positive size enum errors{NOMEM,OUTRANGE,NONPOS}; class Vector{ public: // Constructors Vector(int s=1); // constructor, nonpos error if s<1 // Accessors int ub() const; // The last index (indices run from 0 to size - 1) int howMany() const; // return total no. of Vector objects // Some operator overloads double& operator[](int i); // See notes for discussion Vector operator+(const Vector& rhs); // NOT an error if sizes are different // if v longer, add 1st size components of v // if v shorter, add to only 1st v.size components // outrange is error // The BIG THREE, copy constructor, assignment overload, destructor Vector(const Vector& orig); Vector& operator=(const Vector& rhs); ~Vector(); private: double *p; int size; static int count; // The number of vector objects currently in existence }; /* We repeat the class here with a full specification class Vector{ public: // In the case of an error, or an inability to create the requested vector // because there is not enough space on the heap (which is NOT an error), // construct a NULL object (p and size both set to 0. increase count by 1 // because a NULL object is STILL an object. // The vector constructor does not initialize the vector array itself. Vector(int s=1); // constructor, nonpos error if s<1. // Accessors int ub() const; // The last index int howMany(); // return total no. of Vector objects // Some operator overloads double& operator[](int i); // See notes for discussion // Error if i is not in range. If i is out of range, operator[] will return // a reference to a single safe location (see notes for more detail) // outrange is error ---------------------------------------- Vector operator+(const Vector& rhs); // NOT an error if sizes are different // if v longer, add 1st size components of v // if v shorter, add to only 1st v.size components In the case of unequal lengths, the sum operator should return a Vector equal in length to the longest of the two operand Vectors with addition carried out over only the n leftmost components where n is the size of the shorter Vector. The extra components are just copied from the longer Vector (i.e. treating the shorter one as if it were padded out with zeros) // The BIG THREE, Copy constructor, assignment overload, destructor Vector& operator=(const Vector& rhs); // Again, sizes may be different. the left hand operand will be resized to // the same size as the right. The old storage will be released properly. The // code should work properly for the degenerate case of x = x where x is a // vector object. // Create a NULL object if there is insufficient room on the heap Vector(const Vector& orig); // Copy constructor ~Vector(); // The destructor private: double *p; // Pointer into the heap to the actual vector array int size; // Of the vector static int count; // The number of vector objects currently in existence }; */ void vError(errors e); /* You are NOT TO PROVIDE CODE FOR THIS ERROR FUNCTION! We will do that in our grading routine. However, unlike other error handling mechanisms you may have seen, YOU ARE NOT TO EXIT (which will bring the whole grading program to a screeching halt). Instead you are to call vError() telling it which of the three defined errors was made. The grading program will be looking for your call and so know if you are calling the error routine correctly. After your call, you should - Construct a NULL object (size = 0, p = 0 BUT count still incremented) in the case of an error in either constructor call. Have a static (i.e. permanent) dummy double you can return in the case of an out of range error in a call to the array operator[] overload. This allows you to write something like Vector v(3); // construct a vector 3 elements long v[5] = 7; // write to the 5'th element! harmlessly. The second line involves a call to the array operator. The assignment requires that the number 7.0 by written somewhere. The fact that the operator[] function returns a reference is what allows this write operation. By returning a reference to a safe, dummy location, the 7.0 is written somewhere that it can do no harm. In the case of the +operator, a new sum must be constructed. There may not be enough memory. In that case construct a NULL object as with the constructors. In the case of the assignment operator, the left side may be of a different length than the right in which case a new object will need to be constructed. Same problem and same solution as above. */