作者eu7908 (小事)
看板C_and_CPP
标题[问题] 请问C++的变数要怎麽设定初值
时间Tue Mar 17 00:14:34 2009
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
1F:推 gozha:constructor 03/17 00:16
2F:推 miecowbai:PiggyBank(){total=0;}; 用建构者设定.. 03/17 00:17
3F:→ eu7908:噢 我们还没教到建构元= = 03/17 00:19
4F:→ eu7908:请问有别的办法吗? 03/17 00:24
5F:推 chrisdar:void total(int t){total=t;} 03/17 00:25
6F:→ chrisdar:void set_total(int t){total=t;} 03/17 00:25
7F:推 plover:自己写 init(), allocate(), ... 03/17 01:04
8F:→ softwind:还没有教 constructor阿... 阿码勒真天才... 03/17 01:14
9F:→ legnaleurc:PiggyBank() : total( 0 ) {} 03/17 01:15
10F:推 stonehomelaa:还没教到 自己看书不就好了 又不难 03/17 11:18
11F:推 Ebergies:PiggyBank piggybank; piggybank.total= 0; ... 03/17 11:30
12F:推 kvykn:用member initialization list 谢谢 03/17 13:53
13F:→ eu7908:我知道了 谢谢 03/18 18:55