CS 334: HW 8

Instructions

This homework has two types of problems:

  • Self Check: You are strongly encouraged to think about and work through these questions, but you will not submit answers to them.

  • Problems: You will turn in answers to these questions.

Reading

  • (Required) Mitchell, Chapters 12

  • (As Needed) Scala Resources

Self Check

1. Assignment and Derived Classes

Mitchell, Problem 12.1

I put a working version of the code on in the git repositories if you would like to experiment with it. Use g++ stack.cc to compile the program, and then run the executable a.out with the command "./a.out".

Problems

1. Function Subtyping (16 points)

Assume that {\tt Square}\mathrel{<:}{\tt Rectangle} and {\tt Rectangle}\mathrel{<:}{\tt Polygon}. Which of the following subtype relationships hold in principle? (Yes/No for each part is fine.)

  1. ({\tt Square}\rightarrow{\tt Square}) \mathrel{<:}({\tt Rectangle}\rightarrow{\tt Rectangle})

  2. ({\tt Square}\rightarrow{\tt Rectangle}) \mathrel{<:}({\tt Rectangle}\rightarrow{\tt Rectangle})

  3. ({\tt Square}\rightarrow{\tt Polygon}) \mathrel{<:}({\tt Rectangle}\rightarrow{\tt Rectangle})

  4. ({\tt Rectangle}\rightarrow{\tt Square}) \mathrel{<:}({\tt Rectangle}\rightarrow{\tt Rectangle})

  5. ({\tt Rectangle}\rightarrow{\tt Rectangle}) \mathrel{<:}({\tt Rectangle}\rightarrow{\tt Rectangle})

  6. ({\tt Rectangle}\rightarrow{\tt Polygon}) \mathrel{<:}({\tt Rectangle}\rightarrow{\tt Rectangle})

  7. ({\tt Polygon}\rightarrow{\tt Square}) \mathrel{<:}({\tt Rectangle}\rightarrow{\tt Rectangle})

  8. ({\tt Polygon}\rightarrow{\tt Rectangle}) \mathrel{<:}({\tt Rectangle}\rightarrow{\tt Rectangle})

  9. ({\tt Polygon}\rightarrow{\tt Polygon}) \mathrel{<:}({\tt Rectangle}\rightarrow{\tt Rectangle})

  10. ({\tt Square}\rightarrow({\tt Rectangle}\rightarrow{\tt Square})) \mathrel{<:}({\tt Polygon}\rightarrow({\tt Rectangle}\rightarrow{\tt Square}))

  11. ({\tt Rectangle}\rightarrow({\tt Rectangle}\rightarrow{\tt Square})) \mathrel{<:}({\tt Polygon}\rightarrow({\tt Rectangle}\rightarrow{\tt Square}))

  12. ({\tt Polygon}\rightarrow({\tt Rectangle}\rightarrow{\tt Square})) \mathrel{<:}({\tt Polygon}\rightarrow({\tt Rectangle}\rightarrow{\tt Square}))

  13. ({\tt Polygon}\rightarrow({\tt Square}\rightarrow{\tt Square})) \mathrel{<:}({\tt Polygon}\rightarrow({\tt Rectangle}\rightarrow{\tt Square}))

  14. ({\tt Polygon}\rightarrow({\tt Polygon}\rightarrow{\tt Rectangle})) \mathrel{<:}({\tt Polygon}\rightarrow({\tt Rectangle}\rightarrow{\tt Square}))

  15. ({\tt Polygon}\rightarrow({\tt Polygon}\rightarrow{\tt Square})) \mathrel{<:}({\tt Polygon}\rightarrow({\tt Rectangle}\rightarrow{\tt Square}))

  16. ({\tt Rectangle}\rightarrow({\tt Rectangle}\rightarrow{\tt Rectangle})) \mathrel{<:}({\tt Polygon}\rightarrow({\tt Rectangle}\rightarrow{\tt Square}))

2. Printing (10 points)

Programmers often want to print user-defined object types in a reader-friendly way to output streams (like System.out in Java). For example, we may wish to print a Point object p as "(3,4)". There are many ways to design a programming system to facilitate this.

  1. One not-so-great way is to have the stream class provide methods to print each kind of object. For example, the OutputStream class for whatever language we are using could be defined to have a println(Point p) method to facilitate writing the user-defined Point class to an output stream, and similar methods for other object types. What is the deficiency with this approach?

  2. In C++, this problem of writing user-defined types in a reasonable way is solved through operator overloading. C++ stream classes use the << operator to write to streams. For example, "cout << 4" writes the number 4 to the terminal. To define how user-defined types are written to streams, one defines a new function like the following:

    class Point { 
        int x, y; 
        ...
    };
    
    ostream& operator<<(ostream& o, Point p) {
        return o << "(" << p.x << "," << p.y << ")" ;
    }
    

    With this definition, executing "cout << p", would print "(3,4)" if p were a Point object with those coordinates.

    Java does not use operator overloading. The method

    public void println(Object o) { ... }
    

    from java.io.PrintStream permits one to call "System.out.println(o)" on any object o and print its representation. How does the language and library ensure that this method can print different types of objects in the appropriate way? What methods must the programmer write, how are classes structured, and what language features are involved?

c. Could the designers have taken the same approach in C++? Would that approach fit the C++ design criteria?

Submitting Your Work

Submit your answers to the GradeScope assignment named, for example, "HW 0". It should:

  • be clearly written or typed,
  • include your name and HW number at the top,
  • list any students with whom you discussed the problems, and
  • be a single PDF file, with one problem per page.

You will be asked to resubmit homework not satisfying these requirements. Please select the pages for each question when you submit.