将对象数组、对象计数和所需项传递给函数
Passing an array of objects, the count of objects and a desired term to a function
我在完成与 BankAccount 有关的练习时在我的编程书中遇到了一些麻烦。问题问:
创建一个 main() 函数,该函数声明一个包含 10 个 BankAccount 对象的数组。后输入第一个 BankAccount 对象,询问用户是否要继续。提示用户输入 BankAccount 值,直到用户想要退出或输入 10 个 BankAccount 对象(以先到者为准)。对于输入的每个银行账户,显示银行账户详细信息。然后提示用户输入值介于 1 和 40 之间的术语(包括 1 和 40)。继续提示用户,直到输入有效值。然后将 BankAccount 对象的数组、数组中有效对象的计数以及所需的术语传递给一个函数,该函数计算并显示给定年数后以标准利率计算和显示每个 BankAccounts 的值。
我必须使用 3 个公共函数来完成此任务。只有当我尝试计算Interest()时,我才开始遇到问题。如果我尝试将项和计数传递给函数并将其设置为 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++映射?
- 为什么我可以改变常量对象中的成员变量,这是返回常量对象函数的结果?
- 如何将对象函数的实例传递给另一个函数
- 调用模板函数的问题"No matching function for call"参数:迭代器、对象函数
- makefile对我的名称空间对象/函数/构造函数的不确定引用
- 将对象函数转换为函数指针
- 非对象函数/类函数C++
- 线程对象函数 C++
- C 将成员对象函数分配给类成员功能
- 使用基本指针调用派生对象函数
- 可以(通过编译器)使用多少个线程来初始化全局对象(函数main之前)
- C++类对象函数
- 对对象::函数的未定义引用
- 无法弄清楚将多个对象函数作为单独的线程调用的语法
- 在提升作用域出口中调用对象函数
- 使用基指针来使用派生对象函数
- 在for_each lambda 中调用对象函数
- C++:: 模板函数 - 从对象函数获取对象的地址
- Qt5 未解析的外部静态元对象函数
- 通过变量使用对象和对象函数