Homework 2
CS 300, Parallel and Distributed Computing (PDC)
- Homework 2 Due
-
-
Overloading operators in C++
-
[C]
Create a directory
~/PDC/hw2on a Link machine for your work on this homework assignment. -
[C]
Implement and test a class
Mvectorthat satisfies the spec provided.Implement your class in files
Mvector.handMvector.cpp, and test it with a programMvectorTest.cpp. Include aMakefilefor building your program.Notes:
-
Reminder: A
Makefiledescribes how to compile and link software.Makefiles include rules for carrying out one step in building your program. For example,

is a Makefile rule.-
Mvector.ois the target file to be produced by that rule. -
Mvector.cppandMvector.hare the prerequisites in that rule.
If any of the prerequisites change, then the targetMvector.omust be regenerated. -
g++ -c Mvector.cppis 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.ofiles 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 anintclass 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
cassertfeature 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
vecis anMvectorobject, 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., areMvectorobjects with appropriate lengths.vec = vec1 * 3; vec = vec1 * vec2; vec = vec1 + vec2; -
In your test code, use
operator[]to assign values toMvectorobjects. 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 aMvectorobject in a readable format.
-
-
[C]
Implement and test a class
Polythat satisfies the spec provided.Polyis a subclass ofMvectorthat adds a single operator,operator(), which enables aPolyobjectpto be called as if it were a function (while still providing all theMvectoroperations 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
giton a Link computer.# cd ~/PDC # git pull origin master # git add -A # git commit -m "HW2 submit (link)" # git push origin masterAlso, fill out this form to report on your work.
-