作者hateexam ()
看板C_and_CPP
标题[问题] 解构子
时间Wed May 27 00:15:06 2009
每次调用构造函数後,count的数值都是0??
感觉好像每一次while回圈就呼叫解构子一次,所以才会一直为0
所以试着将解构子的--count;注解掉 果然就就没事了,开始累加了
为何会这样呢?
#include <stdio.h>
#include <string.h>
#include <windows.h>
#include <stdlib.h>
#include <iostream>
using namespace std;
const int OK = 1;
const int Error = 0;
class ACountList;
class ACount{
private:
static float rate;
char name[20];
int password;
int balance;
public:
static int count;
ACount() {}
ACount(char [],int);
~ACount()
{--count;}
void openAcount(ACount user[]);
void deleteAcount();
int save(ACount &x, int money);
int take(ACount &x, int money);
void display(ACount &x);
void Print(int n);
static int getCount(){return count;}
friend class ACountList;
};
int ACount::count=0;
ACount::ACount(char nm[20],int pw) //构造函数,开户後count加1
{
strcpy(name,nm);
password=pw;
++count;
balance=0;
}
void ACount::openAcount(ACount user[])//开户
{
char Name[20];
int Password;
cout<<"please input your name:"<<endl;
cin>>Name;
cout<<"please input your password:"<<endl;
cin>>Password;
user[ACount::count]=ACount(Name,Password);
cout<<"success!"<<endl;
cout<<count;
system("pause");
}
void ACount::deleteAcount()//删除
{
int n;
cout<<"Input the number of the acount you want to delete:";
cin>>n;
if(count==n)
--count;
else if(0<count&&count<n)
{--count;}
else
cout<<"Input error\n";
}
int ACount::save(ACount &x, int money)//存钱
{
if(money>0)
{
x.balance+=money;
return OK;
}
return Error;
}
int ACount::take(ACount &x, int money)
{//取钱
if(money>0&&x.balance-money>=0)
{
x.balance-=money;
return OK;
}
return Error;
}
void ACount::display(ACount &x)
{//列印
cout<<endl<<x.name<<"\t";
cout<<x.balance<<endl;
}
void ACount::Print(int n)
{//查询
cout<<"No."<<n<<"\t"<<name<<"\t\t"<<balance<<endl;
}
inline void topmenu()//一级菜单
{
cout<<"\n****************************************************";
cout<<"\n*Option of the Program *";
cout<<"\n* *";
cout<<"\n* 1. Register *";
cout<<"\n* 2. Delete *";
cout<<"\n* 3. Oprate *";
cout<<"\n* 0. Exit *";
cout<<"\n* *";
cout<<"\n****************************************************";
}
void main()
{
ACount *user;
user=new ACount[10];
int op;
while (1)
{
system("cls");
topmenu();
cout<<"\n please enter selection:";
cin>>op;
switch(op)
{
case 1:
user[ACount::count].openAcount(user);
break;
case 2:
user[ACount::getCount()].deleteAcount();
break;
case 3:
break;
case 0:
exit(0);
default:
cout<<"\n Selection error!";
break;
}
}
}
--
你知道每年全球有多少人死於饥饿吗?美国的「The Hunger site」
http://www.thehungersite.com/clickToGive/home.faces?siteId=1 网站上只要网友
每天上网按一次,他们就会联合世界企业家,捐给世界各地饥民一碗食物,
光是去年一整年,「The Hunger site」就送出了4,800万碗食物给世界各地需要的
饥民.只要连上网路,动动你的滑鼠,加入首页,每天击点一次就可以帮助一个人,
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 218.172.108.115
※ 编辑: hateexam 来自: 218.172.108.115 (05/27 00:16)
1F:→ legnaleurc:缩排会比标色还有帮助 05/27 01:19
※ 编辑: hateexam 来自: 218.172.108.115 (05/27 01:29)
2F:→ akasan:ACount() {} <--问题点 阵列会呼叫这个建构子 05/27 01:25
3F:→ akasan:而这个家伙没更新count 05/27 01:25
4F:→ akasan:另外user[ACount::count].openAcount(user); 会有问题 05/27 01:26
5F:→ akasan:阵列能用的indxe 只有0~9而已 05/27 01:27
6F:→ akasan:还有预设建构子的地方记得初始化name 至少在name[0]塞'\0' 05/27 01:28
7F:→ hateexam:不好意思 可以在请问一下吗 就是ACount() {}这里没变 05/27 01:33
8F:→ hateexam:但将~ACount() {}里的count--注解掉就能累加了耶 05/27 01:34
9F:→ hateexam:为啥会这样 不是主程式结束掉才会呼叫解构子吗 05/27 01:34
10F:→ hateexam:另外其他部份的错误 感谢提醒^^ 05/27 01:37
11F:→ hateexam:还真没注意到.... 05/27 01:37
12F:推 VictorTom:不是主程式结束才呼叫解构子, 是这个obj大限到了该挂了 05/27 09:00
13F:→ VictorTom:就会呼叫; ex1: delete obj; ex2: { OBJ obj; ... } 05/27 09:01