Vector N
Vector N
Provide code to complete a class to manipulate n-dimensional vectors. Don't try to picture this type
of vector in your mind; just think of them as lists of double numbers.
• vectorn.cpp – contains member function and friend function definitions for the vectorn
class.
The definition of the vectorn class is provided to you in the file vector.h. A vectorn object
has two data members. The first is a pointer to a dynamically-allocated array of double elements.
The second is the capacity of the vector array, which is data type size_t (an unsigned integer).
The size of the vector will always be the same as the capacity of its array; there will never be any
empty, unused array elements.
The file vectorn.cpp has skeleton code for all of the required member functions and friends
functions. In addition, some of the functions already have complete definitions provided, including
the constructors, the destructor, the copy assignment operator, the stream insertion operator, and
the size() and clear() member functions.
Your task for this lab project is to complete the definitions for the remaining eight member and
friend functions described below:
This operator has been overloaded as a member function. The operator takes two vectorn
objects and adds them to produce a new vectorn object. The parameters are not altered.
The components of the result should be computed by simply adding the components of the
operands. For example (1, 2, 3) + (4, 5, 6) should give a result of (5, 7, 9).
if the two vectors are of different capacities, then only the first n components of each vector
should be used in the result, where n is the capacity of the smaller of the two vectors. For
example, (1, 2, 3) + (4, 5) should give a result of (5, 7).
This operator has been overloaded as a member function. The operator takes two vectorn
objects and subtracts them to produce a new vectorn object. The parameters are not
altered.
The components of the result should be computed by simply subtracting the components of
the operands. For example (1, 2, 3) - (4, 6, 8) should give a result of (-3, -4,
-5). As with addition, the result should have the capacity of the smaller vector.
• double vectorn::operator*(const vectorn& rhs) const
This member function computes what is called the "scalar product" of two vectors. The
operator takes two vectorn objects and returns a single double value. The parameters
are not altered.
The scalar product is computed by individually multiplying the components of the two
vectors and then adding the results. For example, the scalar product of (1, 2, 3) * (4,
5, 6) is (1 * 4) + (2 * 5) + (3 * 6) which equals 4 + 10 + 18, giving a final
result of 32.
If the two vectors are of different capacities, then only the first n components of each vector
should be used in the product where n is the capacity of the smaller of the two vectors. For
example, (2, 5) * (4, 7, 3) is (2 * 4) + (5 * 7) which equals 8 + 35, giving a
final result of 43.
This member function multiplies a vectorn by a double value to produce a new vectorn.
The parameters are not altered.
For example, multiplying the vector (1, 2, 3) by 3 results in the vector (3, 6, 9).
This friend function multiplies a double value by a vectorn to produce a new vectorn.
The parameters are not be altered.
For example, multiplying 2 by the vector (3, 5, 2) results in the vector (6, 10, 4).
The equality operator has been overloaded as a member function to compare two vectorn
objects and return a bool. The parameters are not altered.
Two vectorn objects are considered equal only if they have the same capacity and are
component-wise equal. For example, (1, 2, 3) is equal to (1, 2, 3), but not to (1,
2) or to (1, 2, 4). All components of the two vectors must be equal.
The subscript operator has been overloaded as a pair of member functions to provide
access to the components of the vector. The parameter n is the position of the element of
the vector array that should be returned. Subscript values outside the bounds of the vector
array will produce undefined results. For speed, no error checking needs to be done.
This operator must be overloaded as a pair of member functions, one that can be called for
a constant vectorn object and one that can be called for a non-constant vectorn object.
However, the code in the body of these two functions will be identical.
• A makefile and a sample main program (main.cpp) to test your solution code for the
vectorn class.
• vectorn.h, which contains the definition for the vectorn class. You are required to use
the data member names and function prototypes defined in this file.
• A partially completed vectorn.cpp file, as described above.
File You Must Submit: Complete the function definitions in vectorn.cpp. This will be the only
file that you submit to the auto-grader.