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 namespace vector374.
  • The representation of a Vector should be an array containing three floats 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 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 two Vectors, i.e., if v1=(a,b,c) and v2=(d,e,f), then v1*v2 should return the scalar value a*d+b*e+c*f.
  • The operator * should be also overloaded so that if v is the Vector (a,b,c) and k is a double, then v*k and k*v should return new Vectors containing the components of v multiplied by k (i.e., (a*k,b*k,c*k)).
  • The class should define stream output so that s<<v will write Vector v to stream s as (a,b,c), i.e., a left parentheses followed by the x, y, and z components of v separated by commas, and a right parentheses. There should be no added spaces in the output. Thereis no std::endl at 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.