作者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)
→ legnaleurc:縮排會比標色還有幫助 05/27 01:19
※ 編輯: hateexam 來自: 218.172.108.115 (05/27 01:29)
→ akasan:ACount() {} <--問題點 陣列會呼叫這個建構子 05/27 01:25
→ akasan:而這個傢伙沒更新count 05/27 01:25
→ akasan:另外user[ACount::count].openAcount(user); 會有問題 05/27 01:26
→ akasan:陣列能用的indxe 只有0~9而已 05/27 01:27
→ akasan:還有預設建構子的地方記得初始化name 至少在name[0]塞'\0' 05/27 01:28
→ hateexam:不好意思 可以在請問一下嗎 就是ACount() {}這裡沒變 05/27 01:33
→ hateexam:但將~ACount() {}裡的count--註解掉就能累加了耶 05/27 01:34
→ hateexam:為啥會這樣 不是主程式結束掉才會呼叫解構子嗎 05/27 01:34
→ hateexam:另外其他部份的錯誤 感謝提醒^^ 05/27 01:37
→ hateexam:還真沒注意到.... 05/27 01:37
推 VictorTom:不是主程式結束才呼叫解構子, 是這個obj大限到了該掛了 05/27 09:00
→ VictorTom:就會呼叫; ex1: delete obj; ex2: { OBJ obj; ... } 05/27 09:01