Home
>>    




Homework 2

CS 300, Parallel and Distributed Computing (PDC)

Homework 2   Due
  1. Overloading operators in C++

    1. [C]

      Create a directory ~/PDC/hw2 on a Link machine for your work on this homework assignment.

    2. [C]

      Implement and test a class Mvector that satisfies the spec provided.

      Implement your class in files Mvector.h and Mvector.cpp, and test it with a program MvectorTest.cpp. Include a Makefile for building your program.

      Notes:

      • Reminder: A Makefile describes 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.o is the target file to be produced by that rule.

        • Mvector.cpp and Mvector.h are the prerequisites in that rule.
          If any of the prerequisites change, then the target Mvector.o must be regenerated.

        • g++ -c Mvector.cpp is the action in that rule, a shell command for producing the target Mvector.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 compiling MvectorTest.o; and one for linking those two .o files to create an executable MvectorTest.

      • When implementing Mvector, use dynamic allocation for the state variable arr[], and correct memory management throughout your class definition.

      • Define default_len (needed for default constructor) as an int class variable in Mvector, i.e., a state variable with class scope (static). Be sure to define that class variable outside of the class definition for Mvector -- not in a header file (perhaps named Mvector.h) but in an implementation module (perhaps Mvector.cpp).

      • Use C++'s cassert feature to cause the program to crash when a method is called incorrectly. For example, you can implement the method operator[ ] for this class as follows:

            #include <cassert>
            ...
            float &Mvector::operator[](int index) {
              assert(0 <= index && index < len);
              return arr[index];
            }
        

        Then, if vec is an Mvector object, a call such as vec[-1] will cause the program to abort execution and print a useful error message that involves the expression 0 <= index && index < len .

      • Here are sample calls of the * and + operators for Mvector. Assume that vec, vec1, etc., are Mvector objects with appropriate lengths.

            vec = vec1 * 3;
            vec = vec1 * vec2;
            vec = vec1 + vec2;
        
      • In your test code, use operator[] to assign values to Mvector 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 a Mvector object in a readable format.

    3. [C]

      Implement and test a class Poly that satisfies the spec provided. Poly is a subclass of Mvector that adds a single operator, operator(), which enables a Poly object p to be called as if it were a function (while still providing all the Mvector 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
      

  2. 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.