Due: Friday, July 12th, 2024 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 the class as described above.
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.
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 and tips:
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.
Makefile
on the one presented
as an example in sections (i.e., you might want to download a copy
of that Makefile
and make suitable modifications
to create one for this exercise).
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.
operator<<
) early so you
so you can use it to print Vector
values using
cout << v
as you add operations to your
Vector
class.
As usual, your code must:
g++ -Wall -g -std=c++17 -o ex9 ex9.cc Vector.cc
-- but
don't forget to submit a Makefile
.Makefile
, as described above, that compiles the code
with the g++
options -Wall -g -std=c++17
cpplint
may be helpful in flagging potential style
problems. (You can ignore cpplint
complaints about
using namespace
directives)
You should submit your exercise to the course Gradescope.