out: Friday, April 25, 2025
due: Monday, April 28, 2025 by 10:00 am,
No late exercises accepted.
Goals: Create and use a complete C++ class for a new 3-D Vector abstraction, including constructors, destructors, operators including assignment and stream output, and a simple user-defined namespace.
Description: Create a C++ class Vector that implements 3-D vectors.
Vector.h
declare a class Vector with the
following properties:
double
s giving the magnitudes in the x, y, and z
directions.get_x()
,
get_y()
, and get_z()
that return the values
of the x, y, and z coordinates of the Vector, respectively.u=v
).u+=v
and u-=v
)
that perform element-by-element addition or subtraction of the Vector
components.+
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.*
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 as a double
.*
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)).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 only, and a
right parentheses. There should be no added spaces in the output.vector333
.
Vector.cc
implement this class.ex9.cc
write a main program that
demonstrates that your Vector class works properly. The output format
is up to you, but it should be labeled neatly and should be concise so
it can be read with pleasure, not pain.
It should show the operands and results of Vector
operations,
such as (1,-2,3)+(1,1,1)=(2,-1,4)
so the reader can verify
that the Vector
operations work properly.
Feel free to write using namespace vector333;
in your test program so you don't need to type vector333::
repeatedly.Makefile
. The command make
should build an
executable program ex9
, recompiling individual files
only when needed. The command make clean
should remove the ex9
executable file, all .o
files, and any editor or other backup files whose
names end in ~
.
Hints: You will find the complex_example
code from
lecture very useful as a model for this exercise.
That code demonstrates several ways to implement operators; do not
feel obligated to use all of them -- use the ones that make the most sense.
However, not all of the operators can be implemented as member functions
of the class, so you should expect to have some non-member operator functions
in your solution. Also, it is generally considered good practice to minimize
the number of member functions that have access to private details
inside the class if it is possible to do the job
equally well with a non-member function.
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.
Feel free to base your Makefile
on the one presented
earlier as an example in class (i.e., you might want to download a copy
of that Makefile
and make suitable modifications
to create one for this exercise).
Advice: get the Makefile
working early, as soon as
you've got a minimal set of source code to compile, and before adding
all the different operations. That should save a lot of time because
after each change you can just type make
to re-build everything and
not need to type lots of lengthy g++
commands.
Advice: Implement stream output (operator<<
) early
so you can use it to print Vector
values using
cout << v
as you add operations to your Vector
class.
Your code must:
Makefile
as described above that compiles the code with the
g++
options -Wall -std=c++17 -g
. cpplint
may be helpful
in flagging potential style problems. (You can ignore
cpplint
complaints, if any, about
using namespace
directives.)
const
and so on must make us
smile rather than cry.
You should submit your exercise using the Gradescope dropbox linked on the course resources web page.