Course Home | Assignments | Computing Resources | Lab Hours/Tutoring | Schedule

Saint Louis University

Computer Science 146
Object-Oriented Practicum

Michael Goldwasser

Spring 2011

Dept. of Math & Computer Science

Assignment 08

Inheritance Design

Overview

Topic: Inheritance Design
Due: 11:59pm Wednesday, 13 April 2011

Please make sure you adhere to the policies on academic integrity.


Introduction

In class, we outlined a new high-level design for our matrix suite. It makes use of inheritance to avoid the unnecessary repetition of implementation details. Specifically, we rely on the definition of an abstract base class named matrix_expression, which serves as the parent of two concrete classes named matrix and matrix_proxy.

In this assignment, we explore the details of the design and test your intuitions for what behaviors can be easily reused and what behaviors are specific to one class. For example, in the previous assignment we explored various forms of addition. What you hopefully noticed is that doing addition of proxies with each other, or one proxy and one matrix, is really the same logic as when adding two matrices; in all cases we needed to create a new matrix instance, and then to set the elements to be the sum of the respective elements from the two operands. In our new design, we will place this implementation at the level of matrix_expression, so that it can be shared by all. Its formal signature will be described as

   matrix matrix_expression::operator+(const matrix_expression& other) const;
Each part of that declaration is important in the design. In general, it uses the form:
   returnType class::function(params);
In this particular example, we note that by defining the behavior in the matrix_expression class, and taking a parameter that is also a matrix_expression, it can be applied to any expression B+C in our hierarchy (e.g., a matrix plus a matrix, a proxy plus a proxy, a proxy plus a matrix). We also note that we have declared the return type to be a matrix instance. So even when adding two proxies, the result is a matrix rather than a proxy. This is because the values in the sum do not exist in any other location before this operation, so we need to create an underlying matrix to store those values.


Your Task

We want to test your intuitions as you consider many subtle cases that may arise in our new framework. For each of the following sample syntaxes, consider where and how you think such a behavior is most naturally defined. In particular, we want you to give a complete signature using the form shown above. Make clear in which class the behavior belongs, the type and form of the parameter passing and the return value. Make sure to rely on your understanding of the inner-workings of the matrix and matrix_proxy classes.

For each of the following, you are to given the full signature, and you must provide a written explanation for why you have chosen such an answer. We will get you started with our answer to #0.

  1. A + B
       matrix matrix_expression::operator+(const matrix_expression& other) const;
    

    The syntax A+B is supported by the operator+ function in C++.

    We place this behavior in the matrix_expression class, and with a matrix_expression parameter, because we are able to add any two objects that qualify as matrix expressions (e.g., matrix + matrix, matrix + proxy, proxy + matrix, proxy + proxy), so long as the dimensions agree. This is possible because we simply need to compute element-by-element sums and all matrix expressions support basic (r,c) indexing to access individual elements.

    The two occurrences of const are there because the evaluation of expression A+B should have no lasting effect on either of the individual operands. Rather, it should generate a new resulting sum.

    We have chosen matrix as the return type because we need to create new storage for the result, as the element sums are not explicitly stored in either of the individual operands.

  2. A += B
  3. A * B
  4. A == B
  5. A(3,5)
  6. A(range(2,4), range(3,8))
  7. A.reshape(4,3)

Finally, we want you to carefully consider the role of the assignment operator =, and to determine reasonable semantics depending on the underlying types of the operands. In particular, for each of the following, describe what effect you think the assignment should have and whether there are any restrictions (such as having dimensions agree).

  1. matrix = matrix

  2. proxy = proxy

  3. matrix = proxy

  4. proxy = matrix

Submitting your assignment

Please write or type your responses, using the numbering scheme above. You may either email a document or hand in a hardcopy.


Michael Goldwasser
CSCI 146, Spring 2011
Last modified: Wednesday, 06 April 2011
Course Home | Assignments | Computing Resources | Lab Hours/Tutoring | Schedule