编写一个基本账户类。成员变量包括:账号、储户姓名和存款余额,成员函数包括:存款和取款。编写一个测试程序进行测试。
时间: 2023-06-05 20:47:09 浏览: 174
好的,以下是基本账户类的代码:
class Account {
private:
string accountNumber; // 账号
string accountHolder; // 储户姓名
double balance; // 存款余额
public:
Account(string accountNumber, string accountHolder, double balance) {
this->accountNumber = accountNumber;
this->accountHolder = accountHolder;
this->balance = balance;
}
void deposit(double amount) { // 存款
balance += amount;
}
void withdraw(double amount) { // 取款
if (balance >= amount) {
balance -= amount;
} else {
cout << "余额不足!" << endl;
}
}
};
测试程序如下:
int main() {
Account myAccount("123456789", "张三", 100.);
myAccount.deposit(500.);
myAccount.withdraw(200.);
myAccount.withdraw(150.);
return ;
}
输出结果为:
余额不足!
阅读全文