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
Vectorclass and associated functions should be placed in the namespacevector374. - The representation of a
Vectorshould be an array containing threefloats giving the magnitudes in the x, y, and z directions. The array should be dynamically allocated on the heap when aVectoris created and deleted when theVectoris no longer needed or no longer exists. - You will implement 3 constructors: a default (0-argument) constructor that initializes a
Vectorto (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
Vectorobject 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 twoVectors, i.e., ifv1=(a,b,c) andv2=(d,e,f), thenv1*v2should return the scalar value a*d+b*e+c*f. - The operator
*should be also overloaded so that ifvis theVector(a,b,c) andkis a double, thenv*kandk*vshould return newVectors containing the components ofvmultiplied byk(i.e., (a*k,b*k,c*k)). - The class should define stream output so that
s<<vwill writeVectorvto streamsas(a,b,c), i.e., a left parentheses followed by the x, y, and z components ofvseparated by commas, and a right parentheses. There should be no added spaces in the output. Thereis nostd::endlat the end.
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.
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.