紧急求助,ATM机类。关于对象数组和私有成员
我做一个ATM机类,需要类中存有10个有效用户的帐号和密码,但是发现在成员函数中,不能访问对象数组每个元素的私有成员,但是我已经定义了友元类,还发现只能在构造函数中访问元素的私有成员,下面是代码,希望高手帮我看看,我用的是VC++2008#include <iostream>
#include <cstring>
using namespace std;
static int i=0;
class count
{
public:
friend class ATM;
count(){}
count(char Num[],char Password[],float Money); //初始化
count(count &p);
char *get_num(); //返回卡号
char *get_password(); //返回密码
float get_money(); //返回金额
private:
char password[10]; //用户密码
char num[10];
float money;
};
count::count(char Num[],char Password[],float Money) //构造初始化
{ strcpy(num,Num); //strcpy函数,把Num中的字符串复制到num中
money=Money;
strcpy(password,Password);
}
count::count(count&p){
strcpy(num,p.num);
money=p.money;
strcpy(password,p.password);
}
float count::get_money(){
return money;}
char *count::get_num() //构造返回卡号
{ char *p=num;
return p;
}
char *count::get_password() //构造返密码
{ char *p=password;
return p;
}
class ATM
{
public:
ATM();
void welcome();
bool check_password(char num[],char pwd[]);
void set_money();
void save_money();
void move_money();
void information();
void show_function();
private:
count a[10];
};
ATM::ATM(){count a[10]={count("000","000",000),count("111","111",100),count("222","222",200),count("333","333",300),
count("444","444",400),count("555","555",500),count("666","666",600),count("777","777",700),
count("888","888",800),count("999","999",900)};welcome();}
void ATM::welcome(){
char num[10],pwd[10];
cout<<"欢迎使用存取款机"<<endl;
cout<<"--------------"<<endl;
cout<<"请输入账号"<<endl;
cin>>num;
cout<<"请输入密码"<<endl;
cin>>pwd;
if(!check_password(num,pwd))
{cout<<"输入的密码有误!"<<endl;
welcome();}
else show_function();
}
bool ATM::check_password(char num[], char pwd[]){
while(i<10){
char *b=a[i].num;
char *c=a[i].password;
char *d=num;
char *e=pwd;
if(strcmp(d,b)==0&&strcmp(e,c)==0){
return true;break;}
i++;}
if(i==10) return false;
exit(0);
};
void ATM::show_function(){
int n;
cout<<"1)查询账户"<<endl;
cout<<"2)取款"<<endl;
cout<<"3)转账"<<endl;
cout<<"4)存款"<<endl;
cout<<"请输入服务代号:";
cout<<i;
cin>>n;
switch(n){
case 1:information();break;
case 2:set_money();break;
case 3:move_money();break;
case 4:save_money();break;}
}
void ATM::information(){
cout<<a[i].get_num()<<endl;
cout<<a[i].get_money()<<endl;
}
void ATM::save_money()//存钱
{ float m;
cout<<"请输入要存入的金额:";
cin>>m;
if(a[i].money<100000) a[i].money=+m;
if(a[i].money>=100000) cout<<"存款大于等于十万不能存款";}
void ATM::set_money()//取钱
{ float m;
cout<<"请输入所取金额:";
cin>>m;
if(m<=5000) a[i].money=-m;
if(m>5000) cout<<"不能一次取出大于的存款";}
void ATM::move_money(){ //转账
float m;
int k;
cout<<"请输入要转入的账号:";
cin>>k;
cout<<"请输入要转入的金额:";
cin>>m;
if(m>a[i].money) cout<<"存款不够!";
else {a[i].money=-m;a[k].money=+m;}
}
void main(){
ATM atm;
//atm.welcome();
}
谢谢
[ 本帖最后由 wangxfbc 于 2011-1-16 21:27 编辑 ]