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.
const-correctness
Matrix class of last week:
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.
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.
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.)
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.
In addition, add an operator!= method for symmetry.
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:
const MyClass &someMethod(...)
MyClass & someMethod(const SomeClass &other)
MyClass & someMethod(...) const
Make sure you understand the implications of each of those, and then apply them meaningfully to your code.
g++ -Wall Matrix.cc checkmatrix.cc -o checkmatrix
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!
Once again, the Matrix.hh and Matrix.cc files.