You will create a C++ class Vector
that implements 3-D vectors. We have provided a header file, Makefile
and demo program; you will implement the class itself, but the provided header cannot be modified.
In file Vector.cc
implement the Vector class with the following properties:
- The representation of a
Vector
should be an array containing threefloat
s giving the magnitudes in the x, y, and z directions. The array should be dynamically allocated on the heap when a Vector is created and deleted when the Vector no longer exists. - There should be a default (0-argument) constructor that initializes a Vector to (0,0,0), a constructor with 3 floats as parameters giving initial values for the x, y, and z magnitudes, and a copy constructor.
- There should be a destructor that does whatever work is needed when a Vector object is deleted. If no work is needed, the body of the destructor can be empty.
- The class should define assignment for vectors (
u=v
). - Operator
*
should compute the inner product (dot product) of two Vectors. Ifv1
=(a,b,c) andv2
=(d,e,f), thenv1*v2
should return the scalar value a*d+b*e+c*f. - Operator
*
should be overloaded so that ifv
is the Vector (a,b,c) and k is a double, thenv*k
andk*v
should return new Vectors containing the components of v multiplied byk
(i.e., (a*k,b*k,c*k)). - The class should define stream output so that
s<<v
will write Vectorv
to streams
as(a,b,c)
, i.e., a left parentheses followed by the x, y, and z components ofv
separated by commas, and a right parentheses. There should be no added spaces in the output. Thereis nostd::endl
at the end. - The Vector class and associated functions should be placed in a namespace vector374.
Note
Several of these functions are required to return new Vectors. This means actual Vector values, not pointers or references to Vectors that have been allocated elsewhere.
LintingΒΆ
We have included cpplint.py
which you should run to style check your C++ code. The autograder will run it on your code as well.