getter返回大的负双数值

getter returning large negative double value?

本文关键字:返回 getter      更新时间:2023-10-16

每当调用getRate()方法时,它都会使用错误的值更新对象:例如,如果在savngsAccount对象中rate的值为0.05,则一旦应用该方法,它通常会在平衡值应该为正且小得多时使用类似-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 << "nnn" << "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;
}

ratedouble,但getRate()将其视为int

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