2015-06-26 40 views
-1

只要调用getRate()方法,它就会使用错误的值更新对象: 如果费率在savngsAccount对象中被估价为0.05,则一旦应用该方法,它通常会在-2147483648之间更新余额值,当它应该是正数并且更小时。这可能是我很想念的东西。提前致谢。getter返回大的负双倍值?

testbank.cpp:

// testbank.cpp : Defines the entry point for the console application. 
// 

#include "stdafx.h" 


int _tmain(int argc, _TCHAR* argv[]) 
{ 
    return 0; 
} 

#include "money.h" 
#include "bankAccount.h" 
#include "savingsAccount.h" 
#include <iostream> 
#include <stdexcept> 
#include <string> 

using namespace std; 

int main() { 

    Money testBal1(123, 33); 
    BankAccount testAccount1("#000001", "John Smith", testBal1); 

    Money testBal2(12, 99); 
    BankAccount testAccount2("#000002", "Arthur Brown", testBal2); 

    Money testAmount(96, 67); 
    Money testAmount2(20, 13); 
    Money testAmount3(44, 56); 

    cout << "INITIAL ACCOUNTS ..." << endl; 
    cout << "ACCOUNT 1: " << testAccount1.get_balance() << endl; 
    cout << "ACCOUNT 2: " << testAccount2.get_balance() << endl; 

    testAccount1.withdraw(testAmount); // £26.66 = £123.33 - £96.67 
    testAccount2.deposit(testAmount); // £109.66 = £12.99 + £96.67 

    cout << "\nUPDATED ACCOUNTS ..." << endl; 
    cout << "ACCOUNT 1: " << testAccount1.get_balance() << endl; 
    cout << "ACCOUNT 2: " << testAccount2.get_balance() << endl; 

    transfer(testAmount2, testAccount1, testAccount2); 
    // £6.53 = 26.66 - £20.13 
    // £129.79 = 109.66 + £20.13 

    cout << "\ntransferred accounts ..." << endl; 
    cout << "\naccount 1: " << testAccount1.get_balance() << endl; 
    cout << "account 2: " << testAccount2.get_balance() << endl; 


    //Savings Account Testing 

    cout << "\n\n\n" << "Savings Account TESTING: " << endl; 

    SavingsAccount savings1("#000003", "Bettie Burns", testAmount3, 0); 

    cout << endl << "ID: " << savings1.get_identifier() << endl; 
    cout << "Name: " << savings1.get_name() << endl; 
    cout << "Balance: " << savings1.get_balance() << endl; 
    cout << "Rate: " << savings1.getRate() << endl; 

    savings1.deposit(testAmount); 

    cout << "After Deposit: " << savings1.get_balance() << endl; 

    savings1.withdraw(testAmount2); 

    cout << "After Withdrawal: " << savings1.get_balance() << endl; 



    savings1.addInterest(); 

    cout << "After Interest: " << savings1.get_balance() << endl; 

    return 0; 
} 

savingsAccount.h:

// Savings Account as a subclass - definition 

    #include "stdafx.h" 


    #include "bankAccount.h" 

    #include <string> 

    class SavingsAccount : public BankAccount { 

    public: 
     SavingsAccount(const std::string&, const std::string&, const Money&, double); 
     int getRate() { return rate; } 
     void addInterest(); 

    private: 
     double rate; 
    }; 
***savingsAccount.cpp*** 
#include "stdafx.h" 

// savingsAccount.cpp : Defines the entry point for the console application. 
// 

#include "stdafx.h" 

// Implementation of a simple savings bank account class 
// (NDE, 2015-02-07) 

#include "SavingsAccount.h" 
#include <stdexcept> 
#include <iostream> 

using namespace std; 


// Overloaded constructors 


SavingsAccount::SavingsAccount(const string& id, const string& nm, const Money& bal, double r) : 
BankAccount(id, nm, bal) 
{ 
    if (identifier.size() == 0) { 
     throw invalid_argument("empty account ID"); 
    } 

    if (name.size() == 0) { 
     throw invalid_argument("empty account name"); 
    } 

    r = rate; 
} 

void SavingsAccount::addInterest() { 

    int balCents = balance.as_cents(); 
    double newBalCents = balCents * 0.05; 

    int eurosInterst = newBalCents/100; 
    int centsInterst = (int)newBalCents % 100; 


    Money interestReward(eurosInterst, centsInterst); 

    balance = balance + interestReward; 
} 

回答

2

ratedouble,但getRate()把它当作一个int

最简单的解决方案是将int getRate()更改为double getRate()