C_and_CPP 板


LINE

每次调用构造函数後,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







like.gif 您可能会有兴趣的文章
icon.png[问题/行为] 猫晚上进房间会不会有憋尿问题
icon.pngRe: [闲聊] 选了错误的女孩成为魔法少女 XDDDDDDDDDD
icon.png[正妹] 瑞典 一张
icon.png[心得] EMS高领长版毛衣.墨小楼MC1002
icon.png[分享] 丹龙隔热纸GE55+33+22
icon.png[问题] 清洗洗衣机
icon.png[寻物] 窗台下的空间
icon.png[闲聊] 双极の女神1 木魔爵
icon.png[售车] 新竹 1997 march 1297cc 白色 四门
icon.png[讨论] 能从照片感受到摄影者心情吗
icon.png[狂贺] 贺贺贺贺 贺!岛村卯月!总选举NO.1
icon.png[难过] 羡慕白皮肤的女生
icon.png阅读文章
icon.png[黑特]
icon.png[问题] SBK S1安装於安全帽位置
icon.png[分享] 旧woo100绝版开箱!!
icon.pngRe: [无言] 关於小包卫生纸
icon.png[开箱] E5-2683V3 RX480Strix 快睿C1 简单测试
icon.png[心得] 苍の海贼龙 地狱 执行者16PT
icon.png[售车] 1999年Virage iO 1.8EXi
icon.png[心得] 挑战33 LV10 狮子座pt solo
icon.png[闲聊] 手把手教你不被桶之新手主购教学
icon.png[分享] Civic Type R 量产版官方照无预警流出
icon.png[售车] Golf 4 2.0 银色 自排
icon.png[出售] Graco提篮汽座(有底座)2000元诚可议
icon.png[问题] 请问补牙材质掉了还能再补吗?(台中半年内
icon.png[问题] 44th 单曲 生写竟然都给重复的啊啊!
icon.png[心得] 华南红卡/icash 核卡
icon.png[问题] 拔牙矫正这样正常吗
icon.png[赠送] 老莫高业 初业 102年版
icon.png[情报] 三大行动支付 本季掀战火
icon.png[宝宝] 博客来Amos水蜡笔5/1特价五折
icon.pngRe: [心得] 新鲜人一些面试分享
icon.png[心得] 苍の海贼龙 地狱 麒麟25PT
icon.pngRe: [闲聊] (君の名は。雷慎入) 君名二创漫画翻译
icon.pngRe: [闲聊] OGN中场影片:失踪人口局 (英文字幕)
icon.png[问题] 台湾大哥大4G讯号差
icon.png[出售] [全国]全新千寻侘草LED灯, 水草

请输入看板名称,例如:Gossiping站内搜寻

TOP