作者nsysu90 (尊)
看板C_and_CPP
标题[问题] C++问题
时间Sun Apr 26 23:17:32 2009
题目是这样:
假设银行帐号之class宣告如下:
class BankAccount
{
public:
BankAccount(char n[],double b, int t); // 设定帐号名称,本金金额及其数
static void setInterestRate(double ir); // 设定利率,各帐号共用
double getNewBalance(); // 结算期末金额
void printAccount(); // 列印期初及满期本金
private:
static int count; //帐号个数统计
static double interestRate; //利率,以年利率计算
char name[20]; //帐号名称
double balance; //本金
int term; //期数,以月计算
};
试撰写程式,利用上述class完成以下计算:
银行利率之预设值为0.08(8%),Alice、Bob及Henry三人分别定存存入900、800及700元,
期数分别为10年、15年及20年,请在萤幕显示帐号资料及满期本金。若利率调整为
0.12(12%)情形又如何?
这是我写的程式:
#include <iostream>
#include <cmath>
using namespace std;
class BankAccount
{
public:
BankAccount(char n[],double b, int t); // 设定帐号名称,本金金额及其数
static void setInterestRate(double ir); // 设定利率,各帐号共用
double getNewBalance(); // 结算期末金额
void printAccount(); // 列印期初及满期本金
private:
static int count; //帐号个数统计
static double interestRate; //利率,以年利率计算
char name[20]; //帐号名称
double balance; //本金
int term; //期数,以月计算
};
int main(int argc, char *argv[])
{
BankAccount Alice,Bob,Henry;
cout<<"Alice存入900元,10年後为:"<<endl;
Alice.BankAccount(Alice , 900 , 120 );
Alice.setInterestRate(0.08);
cout<<Alice.getNewBalance();
cout<<"Bob存入800元,15年後为:"<endl;
Bob.BankAccount(Bob , 800 , 180);
Bob.setInterestRate(0.08);
cout<<Bob.printAccount();
cout<<"Henry存入700元,20年後为:"<endl;
Henry.BankAccount(Henry , 700, 240);
Henry.setInterestRate(0.08);
cout<<Henry.printAccount();
system("PAUSE");
return EXIT_SUCCESS;
}
BankAccount::BankAccount(char n[],double b, int t)
{
strcpy(name,n);
balance=b;
term=t;
}
static void BankAccount::setInterestRate(double ir)
{
interestRate = ir;
}
double BankAccount::getNewBalance()
{
double newBalance=0;
newBalance = balance * pow( (1 + interestRate/12) , term);
return newBalance;
}
void BankAccount::printAccount()
{
cout<<"帐号:"<<name<<"本金:"<<balance<<endl;
}
都一直出错想请教C++高手能否帮我解决一下谢谢!!
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 115.165.206.15
1F:→ walm20: cout<<Henry.printAccount(); 04/26 23:29
2F:→ walm20: what's this ??? 04/26 23:29
3F:→ walm20:直接放code上来找人帮看 说出错 也不说错在哪 04/26 23:30
4F:→ nsysu90:他说错误是no matching function for call to `BankAccoun 04/26 23:37
5F:推 snowlike:你误用了建构子 04/26 23:46
6F:推 lytn:建构子不是这样用的 04/27 15:12
7F:推 dRUMSS:在宣告的时候就要把引数丢进去了 04/27 16:09