[問題] C++問題
題目是這樣:
假設銀行帳號之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
→
04/26 23:29, , 1F
04/26 23:29, 1F
→
04/26 23:29, , 2F
04/26 23:29, 2F
→
04/26 23:30, , 3F
04/26 23:30, 3F
→
04/26 23:37, , 4F
04/26 23:37, 4F
推
04/26 23:46, , 5F
04/26 23:46, 5F
推
04/27 15:12, , 6F
04/27 15:12, 6F
推
04/27 16:09, , 7F
04/27 16:09, 7F
討論串 (同標題文章)
C_and_CPP 近期熱門文章
PTT數位生活區 即時熱門文章