C++ 带类的循环
在我的“computeInterest()”函数中,我需要一个循环来显示先前从“enterAccountData()”填充数组时使用的帐号。我会使用一个嵌套循环来查看帐号吗?还是只有一个循环 谢谢你的帮助,这是我的代码C++ 带类的循环,c++,class,function,loops,C++,Class,Function,Loops,在我的“computeInterest()”函数中,我需要一个循环来显示先前从“enterAccountData()”填充数组时使用的帐号。我会使用一个嵌套循环来查看帐号吗?还是只有一个循环 谢谢你的帮助,这是我的代码 #include <iostream> #include <iomanip> using namespace std; class BankAccount { private: int accountNum; double accountBal; st
#include <iostream>
#include <iomanip>
using namespace std;
class BankAccount
{
private:
int accountNum;
double accountBal;
static const double annualIntRate;
public:
double enterAccountData(int, double);
void computeInterest();
void displayAccount();
};
//implementation section:
const double BankAccount::annualIntRate = 0.03;
double BankAccount::enterAccountData(int number, double balance)
{
cout << setprecision(2) << fixed;
cout << "Enter the account number " << endl;
cin >> number;
accountNum = number;
while(number < 0 || number < 1000)
{
cout << "Account numbers cannot be negative or less than 1000 " <<
"Enter a new account number: " << endl;
cin >> number;
}
cout << "Enter the account balance " << endl;
cin >> balance;
accountBal = balance;
while(balance < 0)
{
cout << "Account balances cannot be negative. " <<
"Enter a new account balance: " << endl;
cin >> balance;
}
return balance,number;
}
void BankAccount::computeInterest()
{
const int MAX_ACCOUNTS = 10;
const int MONTHS_IN_YEAR = 12;
int months;
double rate = 0;
int counter = 0;
cout << "How many months will the account be held for? ";
cin >> months;
counter = 0;
do
{
accountBal = accountBal * annualIntRate + accountBal;
counter++;
}while(months > counter);
/*for(counter = 0; counter < MAX_ACCOUNTS; counter++)
{
cout << "Account # " << counter << "Balance is:$" << accountBal << endl;
}*/
}
int main()
{
const int QUIT = 0;
const int MAX_ACCOUNTS = 10;
int counter;
int input;
int number = 0;
double balance = 0;
BankAccount accounts[MAX_ACCOUNTS];
//BankAccount display;
counter = 0;
do
{
accounts[counter].enterAccountData(number, balance);
cout << " Enter " << QUIT << " to stop, or press 1 to proceed.";
cin >> input;
counter++;
}while(input != QUIT && counter != 10);
for(counter = 0; counter < MAX_ACCOUNTS; counter++)
{
accounts[counter].computeInterest();
}
system("pause");
return 0;
}
#包括
#包括
使用名称空间std;
类别银行帐户
{
私人:
int accountNum;
双重账户;
静态常数双年表;
公众:
双输入AccountData(int,double);
void computeInterest();
作废显示帐户();
};
//执行科:
常数双银行账户::年利率=0.03;
双倍银行账户::enterAccountData(整数,双倍余额)
{
cout您不需要循环。即使使用循环,也会打印相同的值
为什么?
该程序有10个BankAccount
实例,这意味着每个实例都有自己的类成员副本
const int MAX_ACCOUNTS = 10;
const int MONTHS_IN_YEAR = 12; // Both of these declared global
在main()
中,如果需要显示所有帐户信息-
for(int i = 0; i < MAX_ACCOUNTS; ++i)
{
accounts[i].displayAccount(); // Where in displayAccount definition is to display accountNum and accountBal
}
void BankAccount::ComputeInterest()
{
// ......
std::cout << "\n Acc. Num:\t " << accountNum << "\t Acc. Bal:\t" << accountBal << "\n";
}
BankAccount::ComputeInterest()
不接受任何引用其他实例的参数。因此,它只有在对象信息上才被调用。而且程序可以更好地组织
变量存在不必要的重复(如最大值等)
和double BankAccount::entercountdata(整数,双倍余额)
返回类型为double
double BankAccount::enterAccountData(int number, double balance)
{
// ....
return balance,number;
}
return
不能返回多个值。返回最后一个变量的值。因此,只返回number
。不需要循环。即使使用循环,也会打印相同的值
为什么?
该程序有10个BankAccount
实例,这意味着每个实例都有自己的类成员副本
const int MAX_ACCOUNTS = 10;
const int MONTHS_IN_YEAR = 12; // Both of these declared global
在main()
中,如果需要显示所有帐户信息-
for(int i = 0; i < MAX_ACCOUNTS; ++i)
{
accounts[i].displayAccount(); // Where in displayAccount definition is to display accountNum and accountBal
}
void BankAccount::ComputeInterest()
{
// ......
std::cout << "\n Acc. Num:\t " << accountNum << "\t Acc. Bal:\t" << accountBal << "\n";
}
BankAccount::ComputeInterest()
不接受任何引用其他实例的参数。因此,它只有在对象信息上才被调用。而且程序可以更好地组织
变量存在不必要的重复(如最大值等)
和double BankAccount::entercountdata(整数,双倍余额)
返回类型为double
double BankAccount::enterAccountData(int number, double balance)
{
// ....
return balance,number;
}
return
不能返回多个值。返回最后一个变量的值。因此,number
只返回。@Alex:如果你不介意接受建议,请阅读本书。@Alex:如果你不介意接受建议,请阅读本书。