I have been studying c ++ for a while. A friend asked me to help her develop a simulated ATM program two days ago. So I spent one night writing it. In fact, this program is also very simple, however, I think it is useful for people who have just learned c ++, because it can help you better understand the true meaning of object-oriented programming ------- programming based on the real world model. This is the true purpose of learning c ++. It truly understands object-oriented programming!
//************************************// ** // * Function. h *//**//******************************** * *** # include
Class consumer; class ATM // ATM {public: ATM (consumer & cn): cnsm (cn) {} void welcome (); // logon interface bool check_passwd (char n [], char pwd []); // check the password void change_passwd (); // modify the password void fetchmoney (); // void information () for withdrawal; // query information void exitATM (); // exit the system void functionshow (); // function interface void lock (); // lock private: int times; // record Password Input times consumer & cnsm ;}; class consumer // user {public: friend class ATM; consumer (char Name [], char Num [], floatMoney, char Password []); protected: char * get_name (); // get the name char * get_num (); // get the card number char * get_passwd (); // obtain the password float get_money (); // obtain the balance void set_passwd (char pwd []); // set the password void set_money (float m); // private: char passwd [8]; // User Password char name [20]; // user name char num [20]; float money ;};
//************************************// ** // * Member functions of the consumer class *//**//*********************** * ************ # include "function. h "# include <string. h> 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 );}
//************************************// ** // * Member functions of the ATM class *//**//*********************** * ************ # include "function. h "# include
# Include
Void ATM: welcome () {times = 0; cout <"$ welcome to the snow bank ATM !~! "<
\ "; Cin> n; while (n <1 | n> 4) {cout <" enter the correct operation number! "<
\ "; Cin> n;} 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 {cout <
> Pwd; if (! Check_passwd (cnsm. get_num (), pwd) times ++; else break;} while (times <3); if (times = 3) lock (); int t = 0; do {cout <"Enter the new password:"; cin> pwd; cout <"Enter the new password again:"; cin> repwd; if (t = strcmp (pwd, repwd ))! = 0) cout <"the two passwords you entered are different. Please enter them again! "<
\ "<
> M; while (m <= 0) {cout <"enter the correct number! "<
\ "; Cin> m;} if (cnsm. get_money ()-m <0) {cout <" sorry, your balance is insufficient! "<
\ "; Cin> ch; while (ch! = N & ch! = N & ch! = Y & ch! = Y) {cout <"$ >\"; cin> ch ;}}while (ch = y | ch = Y);} void ATM :: information () {cout <"**********************************" <
//************************************// ** // * ATM. cpp *//**//******************************** * *** # include
# Include "function. h "void main () {consumer c1 (" jim "," 12345 ", 5200.3f," 123 "); // first construct a user ATM (c1); atm. welcome ();}