看板 C_and_CPP 關於我們 聯絡資訊
class PiggyBank { private: int password; public: int total; void deposite(int saving) { if(saving>=0) { total=total+saving; cout<<"$ You saved "<<saving<<" dolars."<<endl; } else { cout<<"Sorry, money saved was incorrect."<<endl; } } ....................... 因為我跑出來的程式顯示出total有殘值2 所以在第六行的地方,原本我要設定total=0 但是compile一直有錯誤 那我想請問該怎麼寫? -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.113.189.8
gozha:constructor 03/17 00:16
miecowbai:PiggyBank(){total=0;}; 用建構者設定.. 03/17 00:17
eu7908:噢 我們還沒教到建構元= = 03/17 00:19
eu7908:請問有別的辦法嗎? 03/17 00:24
chrisdar:void total(int t){total=t;} 03/17 00:25
chrisdar:void set_total(int t){total=t;} 03/17 00:25
plover:自己寫 init(), allocate(), ... 03/17 01:04
softwind:還沒有教 constructor阿... 阿碼勒真天才... 03/17 01:14
legnaleurc:PiggyBank() : total( 0 ) {} 03/17 01:15
stonehomelaa:還沒教到 自己看書不就好了 又不難 03/17 11:18
Ebergies:PiggyBank piggybank; piggybank.total= 0; ... 03/17 11:30
kvykn:用member initialization list 謝謝 03/17 13:53
eu7908:我知道了 謝謝 03/18 18:55