【 點我 】來看實際運行狀態(GIF)/*看來上傳gif失敗了,請點超連結至雲端*/
要求:
運用兩個類別:提款卡、ATM
提款卡:data member要有「密碼、金額」;member function要有「get密碼、set金額」
ATM:data member要有「空提款卡」;member function要有「插卡、登入、提款、存款、金額查詢」
運用技巧:Composition、Referance
以下Code:
#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;
class ATMCard
{
public:
ATMCard(string &Inp_Password,int Inp_Cash)
{
Password = Inp_Password;
Cash = Inp_Cash;
}
ATMCard(){}
void ininCard()
{
Password = "000000";
Cash = 0;
}
string& Get_Password()
{
return Password;
}
int Get_Cash()
{
return Cash;
}
void Set(int value)
{
Cash += value;
}
void SetP(string &Inp_password)
{
Password = Inp_password;
}
private:
string Password;
int Cash;
};
class ATM
{
public:
ATM()
{
Card.ininCard();
}
void InsertCard(string &Inp_password,int Inp_cash)
{
Card.SetP(Inp_password);
Card.Set(Inp_cash);
cout<<"Welcome to Taiwan Bank"<<endl;
Login();
}
int Check_Login(string tmp_password)/**Check the password**/
{
if(tmp_password==Card.Get_Password())
return 1;
else
return 0;
}
int Login()
{
bool reEnter=true;
for(;reEnter;)
{
cout<<"Please enter Password with 6 number:"<<endl;
string tmp_password;
cin>>tmp_password;
if(Check_Login(tmp_password))
{
cout<<"The password is correct. What would you want to do?"<<endl;
bool reEnterFunction = true;
for(;reEnterFunction;)
{
cout<<"(Enter 1 to Deposit, Enter 2 to Withdraw, Enter 3 to SearchCash)"<<endl;
int key;
cin>>key;
if(key==1)
{
Deposit();
reEnterFunction = false;
}
else if(key==2)
{
Withdraw();
reEnterFunction = false;
}
else if(key==3)
{
SearchCash();
reEnterFunction = false;
}
else
{
cout<<"Wrong Input, please Enter again(Only allow 1, 2 or 3)."<<endl;
reEnterFunction = true;
}
}
cout<<"What else thing you want to do?(Enter Y or N)"<<endl;
char Answer;
cin>>Answer;
if(Answer=='Y')
reEnter = true;
else if(Answer=='N')
reEnter = false;
else{
cout<<"Error!!"<<endl;
cout<<".\n.\n.\n.\n.\n**Your ATMcard has been thrown out from the machine**"<<endl;
cout<<"**You feel not good**.\n.\n.\n"<<endl;
reEnter = false;}
}
else
{
cout<<"Wrong Password!!"<<endl;
reEnter = true;
}
}
}
void Deposit()
{
cout<<"How much would you deposit?"<<endl;
int money;
cin>>money;
Card.Set(money);
cout<<"Now you has "<<Card.Get_Cash()<<"NTD."<<endl;
}
void Withdraw()
{
bool reEnter=true;
for(;reEnter;)
{
cout<<"How much would you withdraw?"<<endl;
int money;
cin>>money;
if(money>Card.Get_Cash())
{
cout<<"Sorry your deposit is not enough."<<endl;
reEnter=true;
}
else
{
money*=-1;
Card.Set(money);
cout<<"Now you has "<<Card.Get_Cash()<<"NTD."<<endl;
reEnter=false;
}
}
}
void SearchCash()
{
cout<<"you has "<<Card.Get_Cash()<<"NTD."<<endl;
}
private:
ATMCard Card;
};
int main()
{
string MyPassword("123456");
int MyCash = 1000;
cout<<"** Developer should know: Password is 123456, and the card has 1000 NTD **\n"<<endl;
ATMCard mycard(MyPassword,MyCash);
ATM ATM_TWBank;
cout<<"**Put card into the ATM**"<<endl;
ATM_TWBank.InsertCard(mycard.Get_Password(),mycard.Get_Cash());
cout<<"**Pick up the card**\n**Leave**"<<endl;
system("pause");
return 0;
}
心得:
- 設成private的data member無法被實作的object給存取
- 建構子(constructor)可以有很多個,如果第一個建構子的括弧有放參數,當要實作另一個括弧不給參數的物件時,請記得再製作一個括弧沒設參數的建構子,否則compiler會無法呼叫。