CSE 333 Exercise 9

Out:   Friday, January 30
Due:   Monday, February 2 by 11 AM
Rating:   4 (note)
CSE 333 Exercise Rating Scale

Each exercise this quarter is rated on a integer scale of 1 – 5, inclusive, with 1 being the "least time-consuming" and 5 being the "most time-consuming".

This difficulty scale is meant as a rough guide for you in predicting the amount of time to set aside for each exercise as you balance the work required for 333 with your other obligations. However, it is necessarily imperfect as everyone's set of circumstances and experiences with the exercises differ. If your experience with an exercise does not align with its rating, that is not a reflection of you or your abilities.

Goals

  • Implement a complete C++ class for a 3-D Vector abstraction, including constructors, a destructor, operator overloading, and stream output.
  • Use a simple user-defined namespace to organize the Vector abstraction.
  • Write a test program that demonstrates correct Vector behavior.

Background

A major addition in C++ compared to C is the introduction of classes! However, there are lots of new stylistic considerations to properly Encapsulate these classes, including:

  • Const and non-const variables, parameters, and return values,
  • Reference vs. non-reference parameters and return values,
  • The presence or absence of constructors, destructors, and operators, and
  • Member vs. non-member functions. [saved for Exercise 10]

In this exercise, we will put most of these pieces together while writing your first C++ class; carefully consider why each of these different pieces is important!

Problem Description

Create a C++ class Vector that implements 3-D vectors.

  • Vector.h: A header file that declares a class Vector with the following properties:
    • The representation of a Vector should be three doubles giving the magnitudes in the x, y, and z directions.
    • The following constructors should be present:
      1. A default (0-argument) constructor that initializes a Vector to (0,0,0).
      2. A constructor with three double parameters giving initial values for the x, y, and z magnitudes (in that order).
      3. 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.
    • There should be three accessor functions get_x(), get_y(), and get_z() that return the values of the x, y, and z coordinates of the Vector.
    • The class should define assignment for Vectors (u = v).
    • The class should define updating assignment operators (u += v and u -= v) that perform element-by-element addition or subtraction of Vector components.
    • Operators + and - should be overloaded so that u + v and u - v return new Vectors that are the sum and difference of Vectors u and v, respectively.
    • Operator * should compute the inner (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 as a double.
    • Operator * should also be overloaded so that v * k and k * v return new Vectors containing the components of v multiplied by k.
    • The header should define stream output so that s << v writes Vector v to stream s as (a,b,c), with no added spaces.
    • The Vector class and associated functions should be placed in a namespace vector333.
  • Vector.cc: A file containing the implementation of the Vector class.
  • ex9.cc: A file containing a main function that demonstrates correct behavior of your Vector class. The output format is up to you, but it should be neatly labeled, concise, and easy to read. It should show both the operands and results of Vector operations (for example, (1,-2,3) + (1,1,1) = (2,-1,4)) so the reader can verify that the operations work properly.

Implementation Notes

Memory

No dynamic allocation should be necessary for the completion of this exercise (i.e., do NOT use new, delete, malloc, or free).

Testing

In ex9.cc, write test code that exercises each constructor, operator, and public member function of your Vector class. Your tests should verify that Vector components contain the expected values after each operation.

You will find this exercise goes much faster if you implement operators and write test code for them one at a time, instead of trying to do everything all at once.

Namespace

Place the Vector class and associated functions in the vector333 namespace.

In your test program, you may find it convenient to add using statements so that you do not need to type vector333:: repeatedly.

Do not use using namespace directives in header files. Header files are included by other source files, and introducing a using namespace directive in a header would pollute the global namespace for all files that include it.

Style Focus

Classes

This is your first exercise with classes, and you should be sure you are following the best standards with commenting and organizing your classes as laid out in lecture. Make sure that you have comments with their declaration in the header file, and have an overall comment describing the behavior of theVectorclass.

Const

Functions, parameters, and variables should be labeled with const appropriately throughout your program. Refer to the lecture material for best practices.

Submission

Submit the following file(s) by creating an ex9-submit tag in your exercise repository before the assignment deadline. The file(s) should be located in the exact directory listed below, including capitalization:

  • ex9/Vector.h
  • ex9/Vector.cc
  • ex9/ex9.cc

Your code must:

  • Compile without errors or warnings on CSE Linux machines (lab workstations, attu, or CSE home VM).
  • Have no runtime errors, memory leaks, or memory errors (use g++ and valgrind).
  • Be contained in the files listed above and compile with the following command:
    $ g++ -Wall -g -std=c++17 -o ex9 Vector.cc ex9.cc
  • Have a comment at the top of each .cc and .h file with your name(s) and CSE or UW email address(es).
  • Be pretty: the formatting, modularization, variable and function names, use of const, and commenting should follow class style guidelines. If in doubt, follow the Google C++ style guide. (cpplint.py may be helpful.)
  • Be robust: your code should handle hard-to-handle cases gracefully. If there is any user input, bogus input should be handled cleanly.