简单版
#include<iostream>
using namespace std;
#include<string>
class Account
{
public:
Account(string Id,double Balance)
{
id=Id;
balance=Balance;
}
string Get_id()
{
return id;
}
double Get_balance()
{
return balance;
}
void cun(double c)
{
cout<<"此次存款金额:"<<c<<endl;
cout<<"存款成功!"<<endl;
balance=balance+c;
cout<<"余额为:"<<balance<<endl;
}
void qu(double q)
{ if(q>balance)
{ cout<<"本次取款金额:"<<q<<endl;
cout<<"余额不足,取款失败"<<endl;
}
else
{ cout<<"本次取款金额:"<<q<<endl;
cout<<"取款成功!"<<endl;
balance=balance-q;
cout<<"余额为:"<<balance<<endl;
}
}
private:
string id;
double balance;
};
int main()
{ Account id1("17001",5000),id2("17002",500);
cout<<"账号:"<<id1.Get_id()<<endl;
cout<<"当前余额:"<<id1.Get_balance()<<endl;
id1.cun(1000);
id1.qu(500);
cout<<"账号:"<<id2.Get_id()<<endl;
cout<<"当前余额:"<<id2.Get_balance()<<endl;
id2.qu(1000);
id2.qu(100);
return 0;
}
豪华版
#include<iostream>
using namespace std;
class Account
{
public:
Account(int i, double b)
{
id = i;
balance = b;
}
int Get_id()
{
return id;
}
double Get_balance()
{
return balance;
}
void show()
{
cout << "当前账户:" << id << endl << "当前余额:" << balance << endl;
}
void Cun( )
{
int c;
cout << "请输入此次存款金额:";
cin >> c;
cout << "此次存款金额为:" << c;
cout << endl<<"存款成功!" << endl;
balance=balance+c;
cout << "当前余额是:" << balance<< endl;
cout<<"请继续选择!"<<endl;
}
void Qu( )
{
double q;
cout << "请输入取款金额:" << endl;
cin >> q;
if (balance < q)
{
cout << "本卡余额不足,取款失败,请重新输入!" << endl;
}
if (balance >=q)
{
cout << "本次取款成功!";
balance=balance-q;
cout << "当前余额是:" << balance<< endl;
cout<<"请继续选择!"<<endl;
}
}
private:
int id;
double balance;
};
int main()
{
int i1, i2;
double b1, b2;
cout << "请输入账户ID:";
cin >> i1;
cout << "请输入余额:";
cin >> b1;
Account id1(i1, b1);
id1.show();
while(1)
{
cout << "存款请选1,取款请选2,退卡请选0:" << endl;
int flag;
cin >> flag;
if (flag == 1) id1.Cun();
if (flag == 2) id1.Qu();
if (flag ==0) break;
if(flag!=1&&flag!=2&&flag!=0) cout<<"输入出错,请重新输入!"<<endl;
}
return 0;
}