详细解析C++编写的ATM自动取款机模拟程序

王朝c/c++·作者佚名  2008-06-01
宽屏版  字体: |||超大  

学习c++有一段时间了,前两天有个朋友要我帮她做个模拟ATM自动取款机的程序,于是花了一个晚上写了出来,其实这个程序也很简单,但是我觉得它对于刚学c++的人来说比较有用处,因为它可以帮助你更加深刻的理解面向对象程序设计的真谛-------以现实世界为模型编写程序。学习c++的真正目的也就在于此,真正的理解面向对象程序设计!

// ************************************

// * *

// * function.h *

// * *

// ************************************

#include

class consumer;

class ATM

// ATM取款机

{

public:

ATM(consumer& cn):cnsm(cn)

{

}

void welcome();

// 登陆界面

bool check_passwd(char n[],char pwd[]);

// 核对密码

void change_passwd();

// 修改密码

void fetchmoney();

// 取款

void information();

// 查询信息

void exitATM();

// 退出系统

void functionshow();

// 功能界面

void lock();

// 锁机

private:

int times;

// 记录密码输入次数

consumer& cnsm;

};

class consumer

// 用户

{

public:

friend class ATM;

consumer(char Name[],char Num[],

float Money,char PassWord[]);

protected:

char* get_name();

// 取得姓名

char* get_num();

// 取得卡号

char* get_passwd();

// 取得密码

float get_money();

// 取得余额

void set_passwd(char pwd[]);

// 设置密码

void set_money(float m);

// 取钱

private:

char passwd[8];

// 用户密码

char name[20];

// 用户姓名

char num[20];

float money;

};

// ************************************

// * *

// * consumer类的成员函数 *

// * *

// ************************************

#include"function.h"

#include

consumer::consumer(char Name[],

char Num[],float Money,char Password[])

{

strcpy(name,Name);

strcpy(num,Num);

money=Money;

strcpy(passwd,Password);

}

float consumer::get_money()

{

return money;

}

char* consumer::get_name()

{

return name;

}

char* consumer::get_num()

{

return num;

}

char* consumer::get_passwd()

{

return passwd;

}

void consumer::set_money(float m)

{

money-=m;

}

void consumer::set_passwd(char pwd[])

{

strcpy(passwd,pwd);

}

// ************************************

// * *

// * ATM类的成员函数 *

// * *

// ************************************

#include "function.h"

#include

#include

void ATM::welcome()

{

times=0;

cout\\";

cinn;

while(n4)

{

cout\\";

cinn;

}

switch(n)

{

case 1: change_passwd();

break;

case 2: fetchmoney();

break;

case 3: information();

break;

case 4: exitATM();

break;

}

}while(true);

}

void ATM::change_passwd()

{

char pwd[8],repwd[8];

times=0;

do

{

coutpwd;

if(!check_passwd(cnsm.get_num(),pwd))

times++;

else

break;

}while(timespwd;

coutrepwd;

if((t=strcmp(pwd,repwd))!=0)

cout\\"m;

while(m\\ ";

cinm;

}

if(cnsm.get_money()-m\\ ";

cinch;

while(ch!='n'&&ch!='N'&&ch!='Y'&&ch!='y')

{

cout\\";

cinch;

}

}while(ch=='y'ch=='Y');

}

void ATM::information()

{

cout

cout

// ************************************

// * *

// * ATM.cpp *

// * *

// ************************************

#include

#include"function.h"

void main()

{

consumer c1("jim","12345",5200.3f,"123");

// 先构造一个用户

ATM atm(c1);

atm.welcome();

}

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
© 2005- 王朝网络 版权所有