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.
A major addition in C++ compared to C is the introduction of classes! However, there are lots of new stylistic considerations to properly these classes, including:
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!
Create and test a C++ class Vector that implements
3-D vectors in the following two files:
Vector.h: A header file that declares a
class Vector with the following properties:
Vector should be three
floats giving the magnitudes in the x, y, and z
directions.Vector to (0,0,0),Vector object is deleted.
If no work is needed, the body of the destructor can be
empty.u = v).get_x,
get_y and get_z that return the
corresponding values.u += v and u -= v) that perform
element-by-element addition or subtraction of the
Vector components.Vectors
as a float.
If v1 = (a,b,c) and v2 = (d,e,f),
then v1 * v2 should return the scalar value
a*d + b*e + c*f.Vector.cc: A file containing
the implementation of the Vector class.
In addition, you should create the following two files for testing
and compiling your Vector class:
Makefile: The command make
should compile the source files as needed to create an executable
program named ex6.
The command make clean should remove the
ex6 executable file, all .o files, and
any editor or other backup files whose names end in
~ (e.g., ex6.cc~).ex6.cc: A file containing a
main function that tests the Vector
object.
There are two types of tests you should do in this file:
Vector class.
For this part of the tests, make sure you utilize the status
code to perform proper automated testing as you did in
Exercise 3.Increment(), which accepts an instance of
a Vector (not a pointer or reference to a
Vector) as an argument, and increments the
values stored in the vector by one.VerifyAddress(), which accepts a
Vector reference and a void* as
arguments, and returns true if the address
of the aliased Vector has the same numeric
value as the passed-in void*.
It should return false if they are not the
same integral value.
For these tests, you should create an instance of
Vector and then:
Increment() to determine whether the
Vector object itself was passed by value or
passed by reference to Increment()
(i.e., your code should make this determination
based on the result of the call;
don't hard-code the output).
Vector: pass-by-valueVector: pass-by-referenceVerifyAddress() to determine whether
the main() instance of Vector
is the same instance as the one visible to
VerifyAddress by comparing their addresses.
VerifyAddress returned
true), print out on a new line:
Ref: same addressfalse is returned, print out on a
new line:
Ref: different addressIncrement() nor
VerifyAddress() should print the tests
results; they should be printed from main
as the last two lines of your output.
No dynamic allocation should be necessary for the completion of
this exercise (i.e., do NOT use new,
delete, malloc, or free).
For the purposes of this exercise, it is enough for you to write
code that makes use of each of the constructors, operators, and
other functions that you wrote and verifies that the
Vector members contain the correct values at each
step.
Be sure that if an error is encountered during testing, that the
testing code handles it appropriately.
Since there is a significant amount of functionality to test, you
should write static helper functions to factor out
redundant code.
Your Makefile should contain intermediate .o files for
each .cc file that will be compiled into the
executable.
Your Makefile should also set dependencies correctly such that it
only recompiles individual files and rebuilds the program when
needed, and should reuse any existing .o files or
other files that are already up to date.
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.
Functions, parameters, and variables should be labeled with
const appropriately throughout your program.
Refer to the lecture material for best practices.
Submit the following file(s) by creating an ex6-submit tag in your exercise repo before the assignment deadline. The file(s) should be located in the exact directory listed below (there should be a folder titled ex6 with the Vector.h, Vector.cc, ex6.cc, and Makefile files within that folder), including capitalization:
ex6/Vector.hex6/Vector.ccex6/ex6.ccex6/MakefileFor full credit, your code must:
attu, or CSE home VM).g++ and valgrind).g++ options
-Wall -g -std=c++17..cc and
.h files with your name(s) and CSE or UW email
address(es).cpplint.py).