out: Monday, April 28, 2025
due: Wednesday, April 30, 2025 by 10:00 am,
No late exercises accepted.
Goals: Create a modified version of the Vector type
from the previous exercise using dynamic memory allocation and
friend functions this time. Learn how to use C++ dynamic memory
management operations new
and delete
.
Description:
Create a C++ class Vector that implements 3-D vectors and has almost the
same specification as the Vector class from the previous exercise,
but this time does not include the get_x
, get_y
,
and get_z
accessor functions from before,
and that uses a different representation for the data as described below.
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 deleted when the Vector no longer
exists.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.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.
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 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: Some of the functions we need to implement, such as vector
multiplication and stream output,
need access to the instance variables of a vector even though
no access functions like get_x()
are included in the class
this time. Instead, declare inside
the class that these external routines are 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);The actual overloaded
operator*
function is then declared
and implemented as a separate ordinary function in the same namespace as the class,
but it can access
private details of its Vector
parameters.
You may find it helpful to start with your code or the sample solution from the previous exercise (ex9) and modify it as needed to use the new array representation for the Vector data and make other necessary changes.
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 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.