# basis class derived class Basis class Parent class

• Slides: 43

EX: 完成以下程式 class Three. Dim: public Two. Dim { int z ; public: setxyz(int a, int b, int c) {…. } showxyz() {…. } }; 先寫出Three. Dim 有哪些成員? 18

EX: 回答以下問題 class Two. Dim { Q 1: 完成set(int a, int b, int c) int x, y ; Q 2: 寫出Three. Dim的成員? public: Q 3: set()算不算override? void set(int a, int b) { x = a; y = b; } void show() { cout << x << “ “ << y ; } }; class Three. Dim : public Two. Dim { int z; public: void set(int a, int b, int c) {……. } void show() { Two. Dim: : show(); cout << “ “ << z ; } }; 21

2 -3 範例一 fruit color: red, yellow, green, orange name, color get. Name() get. Color() set(name, color) Apple cooking set(name, color, cooking) show() Orange juicy set(name, color, juicy) show() 22

main() void main() { Apple a; Orange o ; a. set(“Washington”, red, yes) ; a. show(); o. set(“Sunkys”, orange, false) ; b. show() ; } 23

7 -2 使用保護成員 class One. Dim { int x; Q: 我可否直接在Two. Dim public: 取用x? void setx(int a) { x = a ; } void getx() { return x ; } }; class Two. Dim: public One. Dim { int y ; void distance(Two. Dim& pt) { return sqrt(pow(getx()-pt. getx(), 2), Q: pow(y-pt. y, 缺點? 2)) ; } … } 29

7 -1 基底類別存取控制 class One. Dim {…} class Two. Dim: public One. Dim {…} 33