[問題] 繼承習題問題

看板C_and_CPP作者 (PT鄉民)時間11年前 (2014/06/01 23:18), 編輯推噓0(003)
留言3則, 1人參與, 最新討論串1/2 (看更多)
開發平台(Platform): (Ex: VC++, GCC, Linux, ...) 額外使用到的函數庫(Library Used): (Ex: OpenGL, ...) 問題(Question): 餵入的資料(Input): 預期的正確結果(Expected Output): 錯誤結果(Wrong Output): 程式碼(Code):(請善用置底文網頁, 記得排版) 補充說明(Supplement): #include <iostream.h> class CA{ public: CA(){a=30; b=40; c=50; } int a, b, c; }; class CB: public CA{ public: CB(){ x=10; y=20;} int x, y; int getM(){ return x*y; } int getN(){ return CA::a*CA::b*c/x; } }; main() { CB b; b.CB::x = 5; b.a = 1; b.CA::a = 5; b.c = 2; cout<< "result= "<< b.getM()<< endl; cout<< "result= "<< b.getN()<< endl; } 請問這兩個答案輸出 會100,80 不懂他的trace code 希望有人肯解析一下,桑Q -- ◢ ◣ ▊ ▊ ▊ ▊ ◢◣ ◢◣ ▊ ▊ ▊███ ◣ ◣ ◢█ L I N ◣ ▊ ▊ █◣ ▊◢ ◥◣ ▊ ▊ █◣ ▊ ▊ ▊ ▊ ▊ ◥◤ ▊ ▇▇ ◥◤ ▊ ▊ ▊◥◣▊◥ ▊ ▊▊◥◣▊ ▊ ▊ ▊ ▊ ▊ ▊ ▊ ▊ ▊ ◥▊ ◥◣ ▊ ▊▊ ◥▊ ▊ ▊ ▉ ▉ ▊ ▊ ▊ ▊ ◥◣█▆▆▊▊ ▊ ▊ ▊ ◥█ ψ █▇▇ ▊ ▊ ▊◣▅▇◤▊ ▊▊ ▊ ▊ ▊ ▊ -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 118.171.39.74 ※ 文章網址: http://www.ptt.cc/bbs/C_and_CPP/M.1401635925.A.DF0.html

06/01 23:44, , 1F
你找本書看比較實在
06/01 23:44, 1F

06/01 23:44, , 2F
還是你那邊不懂?
06/01 23:44, 2F

06/01 23:51, , 3F
b.CA::a 這意思是指向繼承的 CA 內的 a
06/01 23:51, 3F
文章代碼(AID): #1JYqHLtm (C_and_CPP)
文章代碼(AID): #1JYqHLtm (C_and_CPP)