The goal of this exercise is to give you practice with creating and using non-member and friend functions, creating namespaces, and using new and delete to allocate memory in C++. You should also continue to comment your code and follow good style practices laid out in lecture and previous homework.

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 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 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. If v1=(a,b,c) and v2=(d,e,f), then v1*v2 should return the scalar value a*d+b*e+c*f.
  • Operator * should be 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.
  • 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.

Getting started

Begin by grabbing the ex13 starter files: Makefile, header file, and C++ source file:

wget https://courses.cs.washington.edu/courses/cse374/23su/exercises/ex13.tar.gz
tar -xzf ex13.tar.gz
cd ex13_starter_code/

You will see a Makefile, header file and C++ source file. You must create and submit a single file, Vector.cc, which implements the interfaces in the header file. You can test your implementation using the provided ex13 demo program.

Submit on Gradescope here: https://www.gradescope.com/courses/545926/assignments/2983206

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.