指向类实例的指针数组
Array of pointers to class instance c++
这是我到目前为止的代码,它编译和运行良好,但我需要帮助适应它。这是一个银行应用程序,目前只支持一个账户。
它需要适应两个新文件:bank.h和bank.cpp,并且main应该包含一个指向bank的指针,而bank应该包含一个指向account实例的指针数组。
所以新的界面会像这样工作:
account> 1 12
1为账户号,12为存款额。
我真的需要帮助调整我的代码来做到这一点,我失去了如何在银行创建指向帐户实例的指针数组。如有任何帮助,不胜感激。
//main.cpp file
using namespace std;
#include <iostream>
#include <stdlib.h>
#include "account.h"
//allocate new space for class pointer
account* c = new account;
//function for handling I/O
int accounting(){
string command;
cout << "account> ";
cin >> command;
//exits prompt
if (command == "quit"){
exit(0);
}
//overwrites account balance
else if (command == "init"){
cin >> c->value;
c->init();
accounting();
}
//prints balance
else if (command == "balance"){
cout << "" << c->account_balance() << endl;
accounting();
}
//deposits value
else if (command == "deposit"){
cin >> c->value;
c->deposit();
accounting();
}
//withdraws value
else if (command == "withdraw"){
cin >> c->value;
c->withdraw();
accounting();
}
//error handling
else{
cout << "Error! Invalid operator." << endl;
accounting();
}
//frees memory
delete c;
}
int main() {
accounting();
return 0;
}
//account.h头文件包含类的共享变量和函数
class account{
private:
int balance;
public:
account();
~account();
int value;
int account_balance();
int deposit();
int withdraw();
int init();
};
//account.cpp实现文件
using namespace std;
#include <iostream>
#include "account.h"
account::account(){
}
account::~account(){
}
//balance overwrite function
int account::init(){
balance = value;
}
//balance function
int account::account_balance() {
return balance;
}
//deposit function
int account::deposit(){
balance += value;
}
//withdraw function
int account::withdraw(){
//error handling
if(value>balance){
cout << "Error! insufficient funds." << endl;
return 0;
}
balance -= value;
}
首先,
//error handling
else{
cout << "Error! Invalid operator." << endl;
accounting();
}
这看起来很丑,你在每次错误的输入后都递归地调用会计函数。想象一下这样一种情况:用户输入了1 000 000次错误的输入……然后,您将尝试释放内存1 000 000次-在一次成功输入之后!
//frees memory
delete c;
整个会计功能设计错误。我想你不会想在某笔交易后销毁你的账户吧?我认为一个人从他的1000万美元的账户中取出10美元,这个账户将会被销毁,他会立即换银行:)
所以带有continue的while循环可以是解
//function for handling I/O
int accounting(){
string command;
while(true) {
cout << "account> ";
cin >> command;
//exits prompt
if (command == "quit"){
break;
}
//overwrites account balance
else if (command == "init"){
cin >> c->value;
c->init();
continue;
}
//prints balance
else if (command == "balance"){
cout << "" << c->account_balance() << endl;
continue;
}
//deposits value
else if (command == "deposit"){
cin >> c->value;
c->deposit();
accounting();
}
//withdraws value
else if (command == "withdraw"){
cin >> c->value;
c->withdraw();
continue;
}
//error handling
else{
cout << "Error! Invalid operator." << endl;
continue;
}
}
,
int value;
不是类成员,它应该是方法withdraw和deposit的参数,如下所示
//deposit function
void account::deposit(int value){ //int changed to void, you are not returning anything!
balance += value;
}
//withdraw function
bool account::withdraw(int value){
//error handling
if(value>balance){
cout << "Error! insufficient funds." << endl;
return false;
}
if(value<0) {
cout << "Haha, nice try!" << endl;
return false;
}
balance -= value;
return true;
}
对于数组,可以使用std::vector类。
std::vector<account *>MyAccounts;
MyAccounts.push_back(new account());
那么你可以像使用数组一样使用它来访问它。
MyAccounts[i]->accountFunction();
我不太了解你的代码,所以我在这里只给出一些一般的例子。
在您的银行类中,您有一个成员(如上所示)MyAccounts
。现在,当您向银行添加新帐户时,您可以使用推回功能。
例如,添加一个新帐户,并设置100个货币项目的初始金额。
MyAccounts.push_back(new account());
size_t i = MyAccounts.size();
MyAccounts[i]->setAmount(100);
你可以在
class Bank
{
public:
int AddAccount(Account act){ m_vecAccts.push_back(act);}
....
private:
...
std:vector<account> m_vecAccts;
}
更新:这只是一个Bank类,帐户向量作为私有成员变量。AddAccount是一个公共函数,可以将account添加到vector
- 指向指向字符数组的指针数组的指针
- 通过指向指针数组的指针访问子类的属性
- C++,指针数组,指向双链表中的条目
- 在C++中,如何初始化指向wchar_t*的指针数组(生成wchar_t**)
- C++从函数指针数组调用函数
- 关于指向指针数组的指针
- 将返回值存储在函数指针数组的指针中是如何工作的?
- 将链表转换为指针数组时出错
- C++ 对象指针数组的复制构造函数
- C++ - 循环访问指针数组会导致错误
- 删除指针数组 (C++) 中的元素
- 如何循环访问 cpp 中的函数返回的字符指针数组
- 将函数指针数组中的函数指针作为模板参数传递
- Google Or-Tools Glop:如何创建指向 const 对象的指针数组?
- 具有推导参数的模板函数指针数组变量
- 如何模板化堆栈分配的多态指针数组到接口,包括派生类型的相应点?
- C++指针数组到字符数组中的特定位置
- 如何在C++中复制指针数组的数据
- 初始化类中的指针数组,并在另一个类中检索它
- 尽管直接设置了指针数组,但仍为空