c++程序设计模拟收支系统
时间: 2023-06-14 21:06:59 浏览: 56
好的,以下是一个简单的C++程序设计模拟收支系统的示例:
```
#include <iostream>
#include <string>
using namespace std;
// 定义收支类
class Account {
private:
string name;
double balance;
public:
// 构造函数
Account(string n, double b) {
name = n;
balance = b;
}
// 存款
void deposit(double amount) {
balance += amount;
}
// 取款
void withdraw(double amount) {
if (balance < amount) {
cout << "余额不足!" << endl;
}
else {
balance -= amount;
}
}
// 显示余额
void display() {
cout << name << "的账户余额为:" << balance << endl;
}
};
int main() {
// 创建账户
Account myAccount("张三", 1000.0);
// 显示余额
myAccount.display();
// 存款
myAccount.deposit(500.0);
myAccount.display();
// 取款
myAccount.withdraw(2000.0);
myAccount.withdraw(500.0);
myAccount.display();
return 0;
}
```
这个程序定义了一个Account类,包含了账户名和余额两个属性,以及存款、取款、显示余额三个方法。在main函数中,创建了一个初始余额为1000元的账户,然后演示了存款、取款和显示余额等操作。你可以根据需要进行修改和完善。