CSE 333 Exercise 9
Due: Monday, February 2 by 11 AM
Rating: 4 (note)
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 classVectorwith the following properties:-
The representation of a
Vectorshould be threedoubles giving the magnitudes in the x, y, and z directions. -
The following constructors should be present:
-
A default (0-argument) constructor that initializes a
Vectorto (0,0,0). -
A constructor with three
doubleparameters giving initial values for the x, y, and z magnitudes (in that order). - A copy constructor.
-
A default (0-argument) constructor that initializes a
-
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. -
There should be three accessor functions
get_x(),get_y(), andget_z()that return the values of the x, y, and z coordinates of theVector. -
The class should define assignment for Vectors
(
u = v). -
The class should define updating assignment operators
(
u += vandu -= v) that perform element-by-element addition or subtraction of Vector components. -
Operators
+and-should be overloaded so thatu + vandu - vreturn new Vectors that are the sum and difference of Vectorsuandv, respectively. -
Operator
*should compute the inner (dot) product of two Vectors. Ifv1 = (a,b,c)andv2 = (d,e,f), thenv1 * v2should return the scalar valuea*d + b*e + c*fas adouble. -
Operator
*should also be overloaded so thatv * kandk * vreturn new Vectors containing the components ofvmultiplied byk. -
The header should define stream output so that
s << vwrites Vectorvto streamsas (a,b,c), with no added spaces. -
The
Vectorclass and associated functions should be placed in a namespacevector333.
-
The representation of a
-
Vector.cc: A file containing the implementation of theVectorclass. -
ex9.cc: A file containing amainfunction that demonstrates correct behavior of yourVectorclass. 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.
Vector objects. These must be returned by value, not as
pointers or references to dynamically allocated objects.
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.
operator<<) early so you can use
cout << v to print Vector values as you add
functionality.
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.hex9/Vector.ccex9/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++andvalgrind). -
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
.ccand.hfile 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.pymay 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.