# Programming Language Design Assignment \#3
Please hand-in your answers on LMS before 11/06 23:55 and
make sure you're following these rules or you will get penalty:
* Submit your answers in a single pdf file. (only pdf files will be accepted)
* Name your pdf file by your student ID\#. (ex. `107525008.pdf`)
* Use `monospace`-style font to format all the program codes you wrote and **DO NOT** use code screenshots as your answer.
## Multiple Inheritance
Assume we are going to simulate C++'s `iostream` using classes and inheritance.
~~~graphviz
digraph {
rankdir=BT;
ios;
istream -> ios;
ostream -> ios;
iostream -> istream;
iostream -> ostream;
}
~~~
Given the following C++ code, implement `iostream` class which provides all methods provided in `istream` and `ostream`. (You're free to modify class declarations)
~~~cpp
class ios {};
class istream : public ios {
public:
char read_char() {};
}
class ostream : public ios {
public:
void print_ch(char x) {};
};
// create iostream class
~~~
:::info
For the sake of simplicity, we're not going to implement a real c++ "stream" here.
:::
## Prototype-based OOP
One special part of Prototype-based OOP is we can create new kind of objects from another object. In contrast, class inheritance is required in order to do the same thing using a classical OOP language.
In JavaScript, you can also "inherits" from an existing object using `Object.create(obj)`.
~~~js
var Animal = {name: "Animal"};
Animal.hello = function() {
console.log(this.name + ": hello");
};
var Bird = Object.create(Animal);
Bird.name = "Bird";
Bird.fly = function(where) {
console.log(this.name + " fly " + where);
};
Bird.hello() // prints "Bird: hello"
~~~
Follow the way showed by above code,
1. Create an object `Penguin` which inherits from `Bird` and its `fly(where)` method prints "`NAME` can't fly" where `NAME` is the `name` attribute of the object.
2. Create an object `Duck` which inherits from `Bird`. It have a `quack` method which prints "`NAME`: quack!" where `NAME` is the `name` attribute of the object.
:::info
* In prototype style OOP, there is only "instances" but not "classes". To create objects without involving prototype chain, one can just clone an object.
:::
<!--
~~~js
function Animal(name) {
this.name = name;
this.hello = function() {
console.log(name + ": hello");
};
};
bird = new Animal("bird");
bird.fly = function(where) {
console.log(this.name + " fly " + where);
};
~~~
1. The above code use a different way to create a bird object.
What is the difference between the above code and the code on the slide?
:::info
hint: try both code in your browser's debugging console, you can open it on any webpages by pressing "Ctrl+Alt+J" if you're using chrome)
:::
~~~js
var duck = {name: "ducky",
quack: function() {console.log("quack!")}};
Object.setPrototype(duck, bird); // magic happens here.
duck.fly(" in the sky");
~~~
2. The above code shows a different way to create a new kind of object. Will it be different if bird
-->
## Polyporphism and Types
### "One-Term" Answer Questions
Please answer what is the name of polymorphism/typing mechanism used in the following situations:
(Pick the mainly one if your answer is more than one)
1. When creating function `add` accepts numbers, strings, lists (both arguments have the same type).
~~~python
add("abc", "def") # "abcdef"
add(123, 456) # 579
add([1,2], [3]) # [1,2,3]
~~~
2. When creating a function takes a list and counts how many elements in the list.
~~~python
length([1,2,3,4]) # 4
length(['a','b','c']) # 3
length([]) # 0
~~~
3. When creating a binary function `mul` accepts combinations of numbers, vectors, matrices.
~~~r
mul(vector(1,2,3), 3) # mul(vector, number)
mul(vector(1,2,3), vector(4,5,6)) # mul(vector, vector)
mul(3, matrix(1,2,3; 4,5,6)) # mul(number, matrix)
~~~
4. When creating a function like the following "static typed" pseudo code, which typing will allow `distance_from_origin` to accepts both type of `rect` and `circle` as its arguments?
~~~python
def distance_from_origin(obj):
return sqrt(obj.x ** 2 + obj.y ** 2)
# rect has the type {x: int, y: int, w: int, h: int}
rect = {x: 10, y: 10, w: 10, h: 5}
# circle has the type {x: int, y: int, r: int}
circle = {x: 5, y: 3, r: 7}
distance_from_origin(rect) # 14.14
distance_from_origin(circle) # 5.83
~~~
### Short Description Questions
Please answer the following questions:
5. Following the previous question, if we want to do the same thing but not using the typing mechanism used in 4.
Which typing can be used? What modifications should be done?
6. Suppose `Pineapple` is the subtype of `Topping`, and `Hawaiian` is the subtype of `Pizza`. Given a function type named `T: Pineapple ⟶ Pizza`. Is the function type `Topping ⟶ Hawaiian` a subtype of `T`? Explain why?