You will create a C++ class Vector
that implements 3-D vectors (i.e., vectors with x, y, and z components). We have provided a header file, Makefile
and demo program; you will implement the class itself, but the provided header cannot be modified.
We provide starter code, including a Makefile, but you will need to create the Vector.cc
file that will contain the implementation. In this file, you will implement the Vector
class with the following properties:
- The
Vector
class and associated functions should be placed in the namespacevector374
. - 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 aVector
is created and deleted when theVector
is no longer needed or no longer exists. - You will implement 3 constructors: 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
). - The operator
*
should compute the inner product (dot product) of twoVector
s, i.e., ifv1
=(a,b,c) andv2
=(d,e,f), thenv1*v2
should return the scalar value a*d+b*e+c*f. - The operator
*
should be also overloaded so that ifv
is theVector
(a,b,c) andk
is a double, thenv*k
andk*v
should return newVector
s containing the components ofv
multiplied byk
(i.e., (a*k,b*k,c*k)). - The class should define stream output so that
s<<v
will writeVector
v
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.
Note
Several of these functions are required to return new Vector
s. This means actual Vector
values, not pointers or references to Vector
s 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.
Memory Errors¶
Your code should be memory-error free – to test this, you can run valgrind
on the produced hw7
executable, just like how you run valgrind
with C executables: valgrind ./hw7
.