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.
(Required) Mitchell, Chapters 12
(As Needed) Scala Resources
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".
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.)
({\tt Square}\rightarrow{\tt Square}) \mathrel{<:}({\tt Rectangle}\rightarrow{\tt Rectangle})
({\tt Square}\rightarrow{\tt Rectangle}) \mathrel{<:}({\tt Rectangle}\rightarrow{\tt Rectangle})
({\tt Square}\rightarrow{\tt Polygon}) \mathrel{<:}({\tt Rectangle}\rightarrow{\tt Rectangle})
({\tt Rectangle}\rightarrow{\tt Square}) \mathrel{<:}({\tt Rectangle}\rightarrow{\tt Rectangle})
({\tt Rectangle}\rightarrow{\tt Rectangle}) \mathrel{<:}({\tt Rectangle}\rightarrow{\tt Rectangle})
({\tt Rectangle}\rightarrow{\tt Polygon}) \mathrel{<:}({\tt Rectangle}\rightarrow{\tt Rectangle})
({\tt Polygon}\rightarrow{\tt Square}) \mathrel{<:}({\tt Rectangle}\rightarrow{\tt Rectangle})
({\tt Polygon}\rightarrow{\tt Rectangle}) \mathrel{<:}({\tt Rectangle}\rightarrow{\tt Rectangle})
({\tt Polygon}\rightarrow{\tt Polygon}) \mathrel{<:}({\tt Rectangle}\rightarrow{\tt Rectangle})
({\tt Square}\rightarrow({\tt Rectangle}\rightarrow{\tt Square})) \mathrel{<:}({\tt Polygon}\rightarrow({\tt Rectangle}\rightarrow{\tt Square}))
({\tt Rectangle}\rightarrow({\tt Rectangle}\rightarrow{\tt Square})) \mathrel{<:}({\tt Polygon}\rightarrow({\tt Rectangle}\rightarrow{\tt Square}))
({\tt Polygon}\rightarrow({\tt Rectangle}\rightarrow{\tt Square})) \mathrel{<:}({\tt Polygon}\rightarrow({\tt Rectangle}\rightarrow{\tt Square}))
({\tt Polygon}\rightarrow({\tt Square}\rightarrow{\tt Square})) \mathrel{<:}({\tt Polygon}\rightarrow({\tt Rectangle}\rightarrow{\tt Square}))
({\tt Polygon}\rightarrow({\tt Polygon}\rightarrow{\tt Rectangle})) \mathrel{<:}({\tt Polygon}\rightarrow({\tt Rectangle}\rightarrow{\tt Square}))
({\tt Polygon}\rightarrow({\tt Polygon}\rightarrow{\tt Square})) \mathrel{<:}({\tt Polygon}\rightarrow({\tt Rectangle}\rightarrow{\tt Square}))
({\tt Rectangle}\rightarrow({\tt Rectangle}\rightarrow{\tt Rectangle})) \mathrel{<:}({\tt Polygon}\rightarrow({\tt Rectangle}\rightarrow{\tt Square}))
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.
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?
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?
Submit your answers to the GradeScope assignment named, for example, "HW 0". It should:
You will be asked to resubmit homework not satisfying these requirements. Please select the pages for each question when you submit.