Choose the best answer (as far as the syntax of C++ language is concerned).
甲. When an object X of a derived class is assigned, using the default assignment operator, to a variable Y of its corresponding base type: (a) Y has all the data and function members of the derived class. (b) Y has only the data and function members of the base class. (c) an error occurs because this is not an allowed operation. (d) none of the above
乙. If a class C is derived from class B, which is derived from class A, all through public inheritance, then a class C member function can access (a) protected and public data only in C and B. (b) protected and public data only in C. (c) private data in A and B. (d) protected data in A and B.
丙. Usually a pure virtual function (a) has complete function body. (b) will never be called. (c) will be called only to delete an object. (d) is defined only in derived class.
丁. In which case is it mandatory to provide a destructor in a class? (a) Almost in every class. (b) Class for which two or more than two objects will be created. (c) Class for which copy constructor is defined. (d) Class whose objects will be created dynamically.
甲 derived 物件指派給 base 型別的變數,發生 object slicing:只複製 base 的部分,derived 多出來的成員被切掉 → (b)
乙 public 繼承下,C 的 member function 看得到 A 與 B 的 public 與 protected 成員,private 成員不論隔幾層都碰不到;四個選項只有 (d) 的敘述成立((a)(b) 漏掉了 A 或 B,(c) 不可能) → (d)
丙 pure virtual function(= 0)在 base class 通常只有宣告、沒有實作,實作寫在 derived class 的 override 裡 → (d)
丁 物件是動態配置(內部持有 new 出來的資源)時,釋放資源的責任落在 destructor,這時非寫不可 → (d)
丙的 (b)「will never be called」若指 base 版本確實不會被呼叫,但實際執行的是 derived 的 override;選項裡「定義只出現在 derived class」才是 pure virtual 的常態
甲 (b) 乙 (d) 丙 (d) 丁 (d)