在这段代码中,我在类库中创建了一个帐户向量,并且(根据我的理解)每当我添加帐户时,银行类都会创建一个不同的帐户对象并将其推送到向量中,并在帐户构造函数中将初始化,每次通过银行类帐户向量创建新帐户时,它将制作不同的客户副本 - 请告诉我,我对此事的理解是否清楚?我在这里连续分配帐号。
`
class customer
{
string customerName;
string customerAddress;
int phoneNumber;
public:
customer(string customerName, string customerAddress, int phoneNumber)
{
this->customerName=customerName;
this->customerAddress=customerAddress;
this->phoneNumber=phoneNumber;
}
};
class account
{
customer customerObject;
int accountNumber;
static int accountBalance;
public:
account(int accountNumber, int accountBalance)
{
customer customerObject(customerName, customerAddress, phoneNumber);
this->accountNumber=accountNumber;
this->accountNumber=accountNumber;
}
int getBalance()
{
return accountBalance;
}
};
int account::accountBalance=0;
class bank
{
vector<account> accountContainer;
static int accountNumber;
int accountBalance=0;
public:
void addAccount(int deposit, string customerName, string customerAddress, int phoneNumber)
{
account accountObject(++this->accountNumber, this->accountBalance+deposit, customerName, customerAddress, phoneNumber);
accountContainer.push_back(accountObject);
}
void getBalance(int accountNumber)
{
if(accountNumber<=this->accountNumber)
{
return accountContainer[accountNumber-1].getBalance;
}
}
}
};
int bank::accountNumber=0;
int bank::accountBalance=0;
`
答案 0 :(得分:0)
accountContainer.push_back(accountObject);
会将accountObject
复制到accountContainer
向量中。所以你有两个帐户类实例。当addAccount返回时,accountContainer
超出范围并被销毁,但向量中的实例仍然存在。
请参阅 http://www.cplusplus.com/reference/vector/vector/push_back/
它说的是push_back
在末尾添加元素
在向量的末尾添加一个新元素,在其当前的最后一个元素之后。 val的内容被复制(或移动)到新元素。
然而,只要最终结果正确,就允许编译器进行各种优化。因此,很难确切地说在执行函数期间将调用哪些函数(也就是构造函数/析构函数)。它可能取决于优化级别。但最终结果总是正确的。在这种情况下,最终结果是向量包含account
类的实例。
由于您的account
类包含customer
成员,因此account
的每个实例都有自己的客户实例。
account
的构造函数似乎错误,因为您在构造函数中创建了一个本地customer
变量。相反,您只需初始化成员变量。它可能看起来像:
account(int account_number, int account_balance) :
customerObject(customerName, customerAddress, phoneNumber)
{
this->accountNumber=account_number;
this->accountBalance=account_balance;
}
甚至:
account(int account_number, int account_balance) :
customerObject(customerName, customerAddress, phoneNumber),
accountNumber(account_number),
accounBalance(account_balance)
{}
答案 1 :(得分:0)
你大多是正确的。每次创建新帐户时,只会创建一个新的客户对象。
#include <string>
#include <vector>
#include <iostream>
#include <memory>
using namespace std;
class customer
{
string m_customerName;
string m_customerAddress;
int m_phoneNumber;
public:
customer(const std::string& customerName, const string& customerAddress, int phoneNumber)
{
m_customerName=customerName;
m_customerAddress=customerAddress;
m_phoneNumber=phoneNumber;
}
};
class account
{
customer m_customerObject;
int m_accountNumber;
int m_accountBalance;
public:
account(int accountNumber, int accountBalance, const std::string& customerName, const string& customerAddress, int phoneNumber):
m_customerObject(customerName, customerAddress, phoneNumber)
{
m_accountNumber=accountNumber;
m_accountBalance=accountBalance;
}
int getBalance()
{
return m_accountBalance;
}
};
class bank
{
vector<account> accountContainer;
public:
bank()
{
}
void addAccount(int accountNumber, int accountBalance, int deposit, const string& customerName, const string& customerAddress, int phoneNumber)
{
account accountObject(accountNumber, accountBalance+=deposit, customerName, customerAddress, phoneNumber);
accountContainer.push_back(accountObject);
}
int getBalance(int accountNumber)
{
try{
int balance = accountContainer.at(accountNumber).getBalance();
}
catch(std::exception& ex)
{
std::cout<<"Exception: "<<ex.what()<<std::endl;
}
}
};
int main()
{
bank b;
b.addAccount(1, 100, 2000,"John","222 Foo Street", 2222);
b.addAccount(2, 3000, 2000,"Roger","101 Apple Lane", 1111);
std::cout<<b.getBalance(0)<< std::endl;
std::cout<<b.getBalance(1);
return 0;
}