Due: Monday, July 15th, 2024 by 10:00 am; No late exercises accepted.
Goals: Use C++ dynamic memory management operations new
and delete
, and use friend functions.
Description: In this exercise, you will modify the Vector type from the previous exercise (consider using our sample solution!). You will remove some of its simpler methods, switch out the underlying representation, and change how its overloaded operators are defined.
Vector.h
declare a class Vector with the
following properties:
double
s giving the magnitudes in the
x, y, and z directions. The array should be dynamically allocated
on the heap when a Vector is created, and deallocated when the Vector
no longer exists.get_x()
,
get_y()
, and get_z()
are no longer necessary.
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.
ex10.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 ex10
, recompiling
individual files only when needed. The command make clean
should remove the ex10 executable file, all .o
files, and
any editor or other backup files whose names end in ~
.
Hints and tips:
get_x()
are included in the class this time.
Instead, declare them as friend
functions.
For example, inside class Vector
:
// dot-product: if a is (a,b,c) and b is (x,y,z), // return ax+by+cz friend double operator*(const Vector &a, const Vector &b);Then, declare and implement the actual overloaded
operator*
function in the same namespace
as the class; this will allow it to access
private details of its Vector
parameters.
As usual, your code must:
g++ -Wall -g -std=c++17 -o ex10 ex10.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.