在完成与 BankAccounts 相关的练习时,我在编程书籍中遇到了一些问题。问题是:
创建一个 main() 函数,声明一个包含 10 个 BankAccount 对象的数组。后 输入第一个 BankAccount 对象时,询问用户是否要继续。提示用户输入 BankAccount 值,直到用户想要退出或输入 10 个 BankAccount 对象,以先到者为准。对于输入的每个 BankAccount,显示 BankAccount 详细信息。然后提示用户输入一个值介于 1 和 40 之间的术语。继续提示用户,直到输入有效值。 然后将 BankAccount 对象数组、数组中有效对象的计数以及所需期限传递给一个函数,该函数计算并显示每个 BankAccounts 的值标准利率。
我必须使用 3 个公共(public)函数来完成此任务。当我尝试 computeInterest() 时,我才开始遇到问题。如果我尝试将项和计数传递给函数并在 2 个 for 循环中设置它,我将只会收到单个对象的数字和余额。我想我要问的是:如何让程序调用 computeInterest 并运行每个 BankAccount 对象?
目前我的代码已经设置好,可以很好地编译和运行,但是,问题要求将术语和计数发送到函数,而我只发送术语。
我不确定我是否将对象数组发送给函数?这可能是造成麻烦的原因。这已经困扰我好几天了:(。非常感谢任何帮助/指点!
#include<iostream>
#include<string>
using namespace std;
// Declaration Section
class BankAccount
{
private:
int accNum;
double accBal;
const static double intRate;
public:
void enterAccountData(int, double);
void computeInterest(int);
void displayAccount();
};
// Implementation Section
const double BankAccount::intRate = 0.03;
void BankAccount::enterAccountData(int num, double bal)
{
const int MIN_ACC = 1000, MAX_ACC = 9999, MIN_BAL = 0,
MAX_BAL = 100000;
cout << "Enter account number: ";
cin >> num;
while (num < MIN_ACC || num > MAX_ACC)
{
cout << "ERROR: Account number must be between " << MIN_ACC <<
" - " << MAX_ACC << ".\n" << "Enter account number: ";
cin >> num;
}
cout << "Enter account balance: $";
cin >> bal;
while (bal < MIN_BAL || bal > MAX_BAL)
{
cout << "ERROR: Account balance must be positive and less than $" <<
MAX_BAL << ".\n" << "Enter account balance: $";
cin >> bal;
}
accNum = num;
accBal = bal;
}
void BankAccount::computeInterest(int YR)
{
int x;
double interest;
for (x = 0; x < YR; ++x)
{
interest = accBal * intRate;
accBal = accBal + interest;
cout << "Account# " << accNum <<
", Balance: $" << accBal << ", Interest: " <<
intRate << endl;
}
cout << endl;
}
void BankAccount::displayAccount()
{
cout<<"Account: " << accNum <<", Balance: $" << accBal <<
", Interest: " << intRate << endl << endl;
}
int main()
{
const int END = -999, MAX_ACCOUNTS = 10, CONTINUE = 1, MIN_TERM = 1,
MAX_TERM = 40;
int i, x, count = 0, num = 0, term = 0;
double bal = 0;
BankAccount accounts[MAX_ACCOUNTS];
do
{
count++;
accounts[count - 1].enterAccountData(num, bal);
accounts[count - 1].displayAccount();
cout << "Enter " << CONTINUE << " to proceed, or " << END << " to stop: ";
cin >> i;
if (i == END || count == MAX_ACCOUNTS)
{
cout << "Enter the term of the accounts (" << MIN_TERM <<
"-" << MAX_TERM << "): ";
cin >> term;
while (term < MIN_TERM || term > MAX_TERM)
{
cout << "ERROR: Term must be between " << MIN_TERM <<
" - " << MAX_TERM << " inclusive.\n" <<
"Enter term of the accounts (" << MIN_TERM <<
"-" << MAX_TERM << "): ";
cin >> term;
}
for (x = 0; x < count; ++x)
accounts[x].computeInterest(term);
}
} while (i != END && count != MAX_ACCOUNTS);
}
最佳答案
你已经在做
for (x = 0; x < count; ++x)
accounts[x].computeInterest(term);
如果确实有必要,您可以将那段特定的代码移到一个单独的函数中,该函数接受 3 个参数:数组 accounts
, count
和 term
.
设置 BankAccount 类的方式是每个对象计算自己的利息。 由于您有多个这样的对象,您可以要求每个对象计算自己的兴趣。
希望这对您有所帮助!如果还有其他问题,请告诉我 :)
关于c++ - 将对象数组、对象的数量和所需的术语传递给函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5969181/