[問題] 解構子
每次調用構造函數後,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)
→
05/27 01:19, , 1F
05/27 01:19, 1F
※ 編輯: hateexam 來自: 218.172.108.115 (05/27 01:29)
→
05/27 01:25, , 2F
05/27 01:25, 2F
→
05/27 01:25, , 3F
05/27 01:25, 3F
→
05/27 01:26, , 4F
05/27 01:26, 4F
→
05/27 01:27, , 5F
05/27 01:27, 5F
→
05/27 01:28, , 6F
05/27 01:28, 6F
→
05/27 01:33, , 7F
05/27 01:33, 7F
→
05/27 01:34, , 8F
05/27 01:34, 8F
→
05/27 01:34, , 9F
05/27 01:34, 9F
→
05/27 01:37, , 10F
05/27 01:37, 10F
→
05/27 01:37, , 11F
05/27 01:37, 11F
推
05/27 09:00, , 12F
05/27 09:00, 12F
→
05/27 09:01, , 13F
05/27 09:01, 13F
討論串 (同標題文章)
C_and_CPP 近期熱門文章
PTT數位生活區 即時熱門文章