getter返回大的负双值?

时间:2015-06-26 02:03:53

标签: c++

每当调用getRate()方法时,它都会使用错误的值更新对象: 例如如果0.05对象中的费率为savngsAccount,则一旦应用该方法,它通常会更新余额值,例如-2147483648,应该是正数且小得多。这可能是我想念的非常简单的事情。提前谢谢。

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;
}

1 个答案:

答案 0 :(得分:2)

ratedouble,但getRate()将其视为int

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