【C++ Primer Plus习题】10.1
问题:
解答:
main.cpp
#include <iostream>
#include "BankAccount.h"
using namespace std;int main()
{BankAccount BA1("韩立","韩跑跑",1);BA1.get_info();BankAccount BA;BA.init_account("姚国林", "amdin", 10000);BA.get_info();BA.deposit(1000);BA.get_info();BA.withdraw(2000);BA.get_info();return 0;
}
BankAccount.h
#pragma once
#include <iostream>
using namespace std;
class BankAccount
{
private:string fullname;string accid;double balance;
public:BankAccount();BankAccount(const string name, const string id, double bal);~BankAccount();void init_account(const string name, const string id, double bal);void get_info()const;void deposit(double cash);void withdraw(double cash);
};
BankAccount.cpp
#include "BankAccount.h"BankAccount::BankAccount()
{this->fullname = "";this->accid = "";this->balance = 0.0;
}
BankAccount::BankAccount(const string name, const string id, double bal)
{this->fullname = name;this->accid = id;this->balance = bal;
}
BankAccount::~BankAccount()
{}
void BankAccount::init_account(const string name, const string id, double bal)
{this->fullname = name;this->accid = id;this->balance = bal;
}
void BankAccount::get_info()const
{cout << "姓名为:" << this->fullname << endl;cout << "账号为:" << this->accid << endl;cout << "存款为:" << this->balance << endl;
}
void BankAccount::deposit(double cash)
{this->balance += cash;
}
void BankAccount::withdraw(double cash)
{this->balance -= cash;
}
运行结果:

考查点:
- 类和对象
- 分文件
2024年9月3日19:52:54


