Home
>>    




Homework 3

CS 300, Parallel and Distributed Computing (PDC)

Homework 3   Due 10/22/2018
  1. Overloading operators in C++

    1. [C]

      Create a directory ~/PDC/hw3 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.

      Create a commit recording this work as follows:

      link%  git add Mvector.h Mvector.cpp MvectorTest.cpp Makefile
      link%  git commit -m "hw3: Implement Mvector class, with * and + operators"
      

    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
      

      Implement the class Poly in files Poly.h and Poly.cpp, and update your Makefile to include rules for building the code.

      Record your work for this part in a commit:

      link%  git add Poly.{h,cpp} Makefile 
      link%  git commit -m "hw3: Implement Poly class, with operator()"
      

  2. Submitting this homework

      All of your code for this assignment should already be contained in commits. Modify the most recent commit message to indicate that you are submitting the completed assignment.

      link%  git commit --amend
      
      Add the following to the latest commit message.
          hw3: complete
      

      If your assignment is not complete, indicate your progress instead, e.g.,
          hw3: items 1-5 complete, item 6 partial
      
      You can make later commits to submit updates.

      Finally, pull/push your commits in the usual way.

      link%  git pull origin master
      link%  git push origin master
      

      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.


      Files: Mvector.h Mvector.cpp Makefile Poly.h Poly.cpp