C++ track: lab 3: Operators Are Standing By...


Goals

This week, we'll massage Matrix into a production-worthy class, and then we'll compile it against a more rigorous test program and put it through its paces.

Language concepts covered this week

Program to write:

Here's what you need to do to your Matrix class of last week:

  1. Overload the assignment operator.
  2. i.e., given matrices a and b, you should be able to write

    a = b;

    Be sure to handle self-assignment properly. Also support operator chaining i.e.

    a = b = c;
    

    Remember that chains of assignment operators are evaluated from right-to-left. Hint: code from a well-written assignment operator can often be used in a copy constructor.

  3. Overload the destructive arithmetic operators += and -=.
  4. Each of these should work just like your intuition would tell you they would. (Otherwise, overloading operators is often a crummy idea!) Refer to any linear algebra textbook if you've forgotten how to add or subtract matrices. The code you wrote for add and subtract last week will probably be useful here, though you should add support for operator chaining here too.

  5. Overload the + and - operators, too.
  6. These methods will return a new Matrix, of course. (Hint: If you're clever, you can use the code you wrote in part 2 to do most of the work for you.)

  7. Overload the * and *= operators.
  8. Again, you can just define one and define the other in terms of the first one. And again, if you've forgotten how to multiply matrices, consult a linear algebra textbook.

  9. Change your equals method to be an operator overload, too.
  10. In addition, add an operator!= method for symmetry.

  11. Evaluate all of your methods for const correctness.
  12. Make changes as necessary so as to extend to Matrix users the maximum possible type safety from side effects. There are three places you can put a const keyword:

    1. before a return type, e.g.,
      const MyClass &someMethod(...)
    2. before an argument type, e.g.,
      MyClass & someMethod(const SomeClass &other)
    3. after a method name, e.g.,
      MyClass & someMethod(...) const

    Make sure you understand the implications of each of those, and then apply them meaningfully to your code.

  13. Compile your Matrix class against a new, rigorous test suite checkmatrix.cc like so:
    g++ -Wall Matrix.cc checkmatrix.cc -o checkmatrix
  14. Run checkmatrix and use it to help you debug any errors in your matrix implementation. You should not consider this week's lab complete until your class passes the test suite with flying colors. Of course, if you get totally stuck, please ask for help!

To hand in

Once again, the Matrix.hh and Matrix.cc files.