LAB-13 研究以下程式並寫心得, (要自己改一改再測試,) 程式可在這抓到: http://www.csie.nctu.edu.tw/~tsaiwn/introcs/LAB-13 Inheritance 繼承就是指示 compiler 把父類別內全部 copy 過來! 注意 constructor 是 在物件(就是變數啦)找好記億位置後立刻去執行的 function. Destructor 是記憶體要還掉之前的瞬間去執行的 function. 注意 m=258; 會先生出臨時的 _tmp = new B(258); 做 m=_tmp; 再把 _tmp 殺掉! 1 //ab.cpp -- @CopyLeft by tsaiwn@csie.nctu.edu.tw 2 // g++ -Wno-deprecated -ab.cpp 3 //test constructor/destructor and Inheritance 4 //Note that void test( ) is friend of class B 5 #include // so that can be run under TC++3.0 6 //using namespace std; 7 struct A { // 此例寫 class 也完全相同, why? 8 protected: 9 int data; 10 public: 11 A( ) { cout << "A "; data = 38;} // constructor 12 A(int k) { cout << "Aint "<g++ -Wno-deprecated ab.cpp C:\testc>a.exe A B Aint 8880 Bint 888 Aint 123 Aint 13570 Bint 1357 === === A BDBL 3300 in test ... x.kkk = 3300 x.data = 38 B_3300 dying ..A_38 dying.. Now do m=258; Aint 2580 Bint 258 B_258 dying ..A_2580 dying.. <=====注意這是 m=258; 引起的 m.getK()=258 m.getData()=2580 === bye ... B_1357 dying ..A_13570 dying.. A_123 dying.. B_258 dying ..A_2580 dying.. B_49 dying ..A_38 dying.. C:\testc>