# OOP Final solutions
Contributed by < `25077667`, `song856854132`, `ernestchu` >
###### tags: `class notes`
:::info
尚未解答題目:麻煩回報哪邊漏寫
::: danger
2008 (5)
2009 (2)
討論進行中... Go go!
:::
:::
## 2008

> §15: The default constructor (15.1), copy constructor and copy assignment operator (15.8), move constructor and move assignment operator (15.8), and destructor (15.4) are special member functions.[^first]
|Member function| implicitly defined:| default definition:|
|---------------|----------------------|-----------------------|
|Default constructor| if no other constructors |does nothing
|Destructor| if no destructor| does nothing|
|Copy constructor| if no move constructor and no move assignment |copies all members|
|Copy assignment| if no move constructor and no move assignment |copies all members|
|Move constructor| if no destructor, no copy constructor and no copy nor move assignment| moves all members|
|Move assignment| if no destructor, no copy constructor and no copy nor move assignment| moves all members|
[^first]: [C++17 standard](http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2017/n4713.pdf)

> The interpretation of the call of a virtual function depends on the type of the object for which it is called, whereas the interpretation of a call of a non-virtual member function depends only on the type of the pointer or reference denoting that object.[^third]
> Also in C++17 standard[^first]§13.3.10
> 
[^third]:[Standard_web](https://eel.is/c++draft/class.virtual#9)
<!-- > 看不懂"How the call"要問什麼不會回答。
> Virtual functions are in §13.3[name=楊志璿]
> virtual function -> Late Binding
> If a class is created then a virtual pointer(VPTR) is inserted as a data member of the class to point to VTABLE of that class.
> Irrespective of object is created or not, a static array of function pointer called VTABLE where each cell contains the address of each virtual function contained in that class.[name=song856854132]
>> 可是這個是在建構階段做 Binding 的動作,跟 How the call 不知道關係是什麼 [name=楊志璿]
>> In my opinoin, this problem might be "how virtual function be interpreted" [name=song856854132]
 -->

> The C++ Programming Language[^second]§21.4.6
Let's see the source code:
`operator<<` that takes a pointer to function argument and invokes it
```cpp=
template <class Ch, class Tr=char_traits<Ch>>
class basic_ostream : virtual public basic_ios<Ch, Tr>{
// ...
basic_ostream &operator<<(basic_ostream &(*f)(basic_ostream &)){
return f(*this);
}
// ...
};
```
For this to work, a function must be a nonmember or static-member function with the right type. In particular, `flush()` is defined like this:
```cpp=
template <class Ch, class Tr=char_traits<Ch>>
basic_ostream<Ch, Tr> &flush(basic_ostream<Ch, Tr> &s){
return s.flush();
}
```
Therefore, these declarations ensure that:
```
cout << flush;
```
is resolved as
```
cout.operator<<(flush);
```
which calls
```
flush(cout);
```
which then invokes
```
cout.flush();
```
[^second]: [The C++ Programming Language](https://www.amieindia.in/downloads/ebooks/cplusplus.pdf)

> Local variables are on the "Stack" segment
> Dynamic variables are on the "Heap" segment
> Golbal variables are on the "Data" segment
> The process graph:
> 
:::warning
But in 64-bits machine, this is not actually correct.
See: [Why do x86-64 systems have only a 48 bit virtual address space?](https://stackoverflow.com/questions/6716946/why-do-x86-64-systems-have-only-a-48-bit-virtual-address-space)
:::

> Static member functions are not associated with any object. When called, they have no `this` pointer.
> However, these operators must use `this` pointer to operate their data members.
>> I agree with this more.
>> Because when we call these operator, an extra register was used to pass `this` pointer to object. [name=song856854132]
> [name=璿]
>
> They require 1st argument to be l-values
> For operator=, it must have an address to be store to.
> For operator[], it must have an base address to be added to with the offset.
> For operator(), it must have an address to jump to.
> For operator->, same as operator[]

>X1 has error output: X() and ~X()
><del>X2, however, has no output because no error </del>
> [name=楊志璿] In X2, `a` is a function, which returns an `X` object.

> Specialization and termination.
>> Specialization: To serve the need of special implementations, e.g. vector of bool [name=ernestchu]
>> Termination: To create the terminal case of recurrsion generating codes form template. e.g. [Variadic template](https://en.wikipedia.org/wiki/Variadic_template#C++). [name=楊志璿]

>B=16, because p is a pointer to B, p->f calling its B::f(although it has D's address)

> [name=楊志璿] 我覺得應該不是這樣解釋。 @song856854132
> 詳細如下面 2009 (8)

>59
>SQUARE_M(3+1*2)*SQUARE_F(3+1*2) will turn into 3+6+2*25 =59
>SQUARE_M(3+1*2) = 3+1*2*3+1*2
>SQUARE_M(3+1*2) = 5*5
## 2009
1. What are the names of the special member functions that would be implicitly defined by a C++ implementation if not given?
> The default constructor (15.1), copy constructor and copy assignment operator (15.8), move constructor and move assignment operator (15.8), and destructor (15.4) are special member functions.
2. When overloading an operator, at least one of the operands to the operator must be of a class type. Why?
> 不這樣做有存在運算子重載的必要?
> We cannot redefine operators for built-in types, otherwise we redefine C++.
>> In my opinion, the standard types are already defined the operators with that signature. We cannot 'overload' the fucnction with same signature, therefore, we shloud use at least one self-defined type to overload it. [name=楊志璿] @ernestchu @nsysuB073040018
3. Use typedef to declare “foo” as an array of 3 pointers to functions taking as input an integer and returning as output an integer in a single declaration in C++.
```cpp=
typedef int (*foo[3])(int);
```
4. Define an integer “a” in C++ that can only be seen in the file at which it is defined by not using static.
```cpp=
namespace{int a;}
```
5. Define in a single declaration in C++ a pointer to integer “p” so that p[1] is an alias of a[0], p[2] is an alias of a[1], and so on, all the way up to p[128] is an alias of a[127] for the integer array “int a[128];” defined in C++.
```cpp=
int *p = a - 1;
```
6. What would be the output of the following program, assuming that the variable a is located at $\beta$?
```cpp=
int a = 10;
int& b = a;
int& c = b;
cout << "&a = " << &a << endl;
cout << "&b = " << &b << endl;
cout << "&c = " << &c << endl;
```
Answer:
Assume address of `a` is $\beta$.
> $\beta$
> $\beta$
> $\beta$
7. What would be the output of the following C++ program? Why?
```cpp=
class A {
public:
A(int i) : v(i) { cout << "A::A(" << i << ") called" << endl; }
~A() { cout << "A::~A(" << v << ") called" << endl; }
operator bool() { return v != 0; }
private:
int v;
};
int main(){
int i = 1;
while (A a = i) {
i = 0;
cout << "avocado" << endl;
}
cout << "vineyard" << endl;
return 0;
}
```
Output:
> A::A(1) called
> avocado
> A::~A(1) called
> A::A(0) called
> A::~A(0) called
> vineyard
Why?
> In the while condition, that is initializing an object A.
> Which would call `A(int)` , hense it would call `cout`.
8. What would be the output of the following C++ program? Why?
```cpp=
class B
{
public:
virtual void f(int n)
{
cout << "B=" << n << endl;
}
};
class D : public B
{
public:
void f(short n)
{
cout << "D=" << n << endl;
}
};
int main()
{
B *p = new D;
p->f(16);
delete p;
return 0;
}
```
Output:
> B=16
Why?
> 兩個 function 的特徵不同(使用參數不同`int`、`short`),所以屬於不同的函式
> 同時`p`是一個`B`指標
> 因為不同函式,所以不會去`D`物件尋找,而是直接使用 `B` 物件之函式
9. What would be the output of the following C++ program? Why?
```cpp=
class B
{
public:
virtual B *f(int n)
{
cout << "B=" << n << endl;
}
};
class D : public B
{
public:
D *f(int n)
{
cout << "D=" << n << endl;
}
};
int main()
{
B *p = new D;
p->f(16);
delete p;
return 0;
}
```
Output:
> D=16
Why?
> 因為兩個 function 的特徵一樣(函數名稱為`f`,使用一個`int`的參數,回傳一個指標)
> 同時 `p` 是一個`B` 指標指向 `D` 物件
> 相同函式,所以會優先去找 `D` 物件的函式
10. What would be the output of the following C++ program? Why?
```cpp=
class B
{
public:
B *f(int n)
{
cout << "B=" << n << endl;
}
};
class D : public B
{
public:
D *f(int n)
{
cout << "D=" << n << endl;
}
};
int main()
{
B *p = new D;
p->f(16);
delete p;
return 0;
}
```
Output:
> B=16
Why?
> 無虛擬函式繼承關係,因此,使用`B`物件自身之函式。
---
## Midterm solutions
https://hackmd.io/@25077667/oop-mid