---
# System prepended metadata

title: 程式設計(二) Lab 6 講解
tags: [lion_1082]

---

# 程式設計(二) Lab 6 講解

## 第一題
:::info
State whether each of the following is true or false. If false, explain why.

1. Base class constructors are not inherited by derived classes.
2. An is-a relationship is implemented via composition.
3. A Student class has an is-a relationship with the Faculty and Course classes.
4. Private members of a private base class are inaccessible to the derived class.
5. A base class’s protected members can be accessed in the base class definition, in derived class definitions and in friends of the base class and its derived classes.
:::

### 題解
1. ==**true**==, constructor 不能被 derived class 所繼承。
3. ==**false**==, is-a 的關係是使用 inheritance 來實作，has-a 的關係才是使用 composition 的方式來實作。
4. ==**false**==, Student 對 Course 與 Faculty 不是 is-a 的關係，而是 has-a 的關係。
5. ==**true**==, private 的 member function 本來就不能被 derived class 存取，所以亦不能被以 private 繼承的 derived class 所存取。
6. ==**true**==, protected 的 member 可以被 derived class 與 friend class 存取。

## 第二題
:::info
Draw an inheritance hierarchy for students at a university. Use Student as the base class of the hierarchy, then include classes UndergraduateStudent and GraduateStudent that derive from Student. Continue to extend the hierarchy as deep (i.e., as many levels) as possible. For example, Freshman, Sophomore, Junior and Senior derive from UndergraduateStudent, and DoctoralStudent and MasterStudent derive from GraduateStudent. After drawing the hierarchy, discuss the relationships that exist between the classes. (Note: You don’t need to write any code for this exercise.)
:::

### 題解
```graphviz
digraph UML_diagram {
    node [shape = "record"]
    
    student[label = "{Student}"]
    under[label = "{UndergraduateStudent}"]
    graduate[label = "{GraduateStudent}"]
    
    under->student
    graduate->student
    
    freshman[label = "{Freshman}"]
    sophomore[label = "{Sophomore}"]
    junior[label = "{Junior}"]
    senior[label = "{Senior}"]
    
    senior->under
    freshman->under
    sophomore->under
    junior->under
    
    doctor[label = "{DoctoralStudent}"]
    master[label = "{MasterStudent}"]
    
    doctor->graduate
    master->graduate
    
    home[label = "{LiveAtHome}"]
    dorm[label = "{LiveAtDorm}"]
    
    home->freshman
    dorm->freshman
    
    sl[label = "{Sheng-Li}"]
    kf[label = "{Kuang-Fu}"]
    cy[label = "{Ching-Yeh}"]
    
    sl->dorm
    kf->dorm
    cy->dorm
}
```

###### tags: `lion_1082`