[問題] trace C++ 寫出result的考題

看板C_and_CPP (C/C++)作者 (鬱)時間15年前 (2011/03/05 15:16), 編輯推噓0(001)
留言1則, 1人參與, 最新討論串1/2 (看更多)
因為本身只學過C語言也沒學得很深 突然發現考題裡面有C++有點嚇到 不過就硬頭皮把程式碼打下來 邊想邊對照Output 後來做了幾份大概有些概念 雖然也不確定到底思考的流程是不是對的= = 但有個地方實在想不通 不知道有沒有人可以幫我說明一下.. # include <string> # include <iostream> class Base { public: Base(int b1 = 0, int b2 = 0) {value1 = b1; value2 = b2;} void compute(int x) {value1 += x; value2 *= x;} void compute1(int x) {calculate(x);} virtual void calculate(int x) {value1 = x; value2 = x * x;} void print() {cout << value1 + value2 << " ";} int value1, value2; }; class Derived: public Base { public: Derived(int x1, int x2, int x3):Base(x1, x2) {value2 = x1 + x2; value3 = x3;} virtual void calculate(int x) {value1 = x; value2 = 2 * x; value3 = x + 1;} void print() {cout << value2 + value3 << " ";} int value2, value3; }; void main() { Base b1(1, 2); b1.print(); b1.compute(3); b1.print(); Derived d1(2, 3, 4); d1.print(); d1.compute(5); d1.Base::print(); Derived d2(3, 4, 5); Base b2; b2 = d2; b2.print(); Derived d3(4, 5, 6); d3.calculate(3); d3.print(); d3.Base::print(); //這行 Derived d4(5, 6, 7); Base *b4; b4 = &d4; b4->compute(2); b4->print(); b4->calculate(4); d4.print(); Base *b5 = new Derived(6, 7, 8); b5->compute1(5); b5->print(); //這行 system("pause"); } 有標記那兩行的所輸出結果想不通 有沒有人可以幫我說明一下~ 謝謝! -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 114.24.182.89 ※ 編輯: hyujr 來自: 114.24.182.89 (03/05 15:19)

03/05 15:27, , 1F
北科的考題? XD
03/05 15:27, 1F
文章代碼(AID): #1DSUD07I (C_and_CPP)
文章代碼(AID): #1DSUD07I (C_and_CPP)