Homework 2
CS 300, Parallel and Distributed Computing (PDC)
- Homework 2 Due
-
-
Overloading operators in C++
-
[C]
Create a directory
~/PDC/hw2
on a Link machine for your work on this homework assignment. -
[C]
Implement and test a class
Mvector
that satisfies the spec provided.Implement your class in files
Mvector.h
andMvector.cpp
, and test it with a programMvectorTest.cpp
. Include aMakefile
for building your program.Notes:
-
Reminder: A
Makefile
describes how to compile and link software.Makefile
s include rules for carrying out one step in building your program. For example,
is a Makefile rule.-
Mvector.o
is the target file to be produced by that rule. -
Mvector.cpp
andMvector.h
are the prerequisites in that rule.
If any of the prerequisites change, then the targetMvector.o
must be regenerated. -
g++ -c Mvector.cpp
is the action in that rule, a shell command for producing the targetMvector.o
. There maybe multiple action lines for a given target.
Each action line must be preceded by a TAB character.
You will need three rules: one for compiling
Mvector.o
; one for compilingMvectorTest.o
; and one for linking those two.o
files to create an executableMvectorTest
. -
-
When implementing
Mvector
, use dynamic allocation for the state variablearr[]
, and correct memory management throughout your class definition. -
Define
default_len
(needed for default constructor) as anint
class variable inMvector
, i.e., a state variable with class scope (static
). Be sure to define that class variable outside of the class definition forMvector
-- not in a header file (perhaps namedMvector.h
) but in an implementation module (perhapsMvector.cpp
). -
Use C++'s
cassert
feature to cause the program to crash when a method is called incorrectly. For example, you can implement the methodoperator[ ]
for this class as follows:#include <cassert> ... float &Mvector::operator[](int index) { assert(0 <= index && index < len); return arr[index]; }
Then, if
vec
is anMvector
object, a call such asvec[-1]
will cause the program to abort execution and print a useful error message that involves the expression0 <= index && index < len
. -
Here are sample calls of the
*
and+
operators forMvector
. Assume thatvec
,vec1
, etc., areMvector
objects with appropriate lengths.vec = vec1 * 3; vec = vec1 * vec2; vec = vec1 + vec2;
-
In your test code, use
operator[]
to assign values toMvector
objects. For example,Mvector vec(5); vec[2] = 3.14;
assigns the value 3.14 to the third element in
vec.arr[]
.To check whether your vector operations are producing correct answers, it will help to define a function or method
dump()
that prints all of the values in aMvector
object in a readable format.
-
-
[C]
Implement and test a class
Poly
that satisfies the spec provided.Poly
is a subclass ofMvector
that adds a single operator,operator()
, which enables aPoly
objectp
to be called as if it were a function (while still providing all theMvector
operations such as*
).Example call:
Poly p(4); p[0] = 9; p[1] = 4; p[2] = 6; p[3] = 7; // p.arr[] now contains four float values 9.0, 4.0, 6.0, and 7.0 cout << p(10) << endl; // prints 7.0*103 + 6.0*102 + 4.0*101 + 9.0 = 7649.0
-
-
Submitting this homework
Submit your work using
git
on a Link computer.# cd ~/PDC # git pull origin master # git add -A # git commit -m "HW2 submit (link)" # git push origin master
Also, fill out this form to report on your work.
-