getter返回大的负双数值
getter returning large negative double value?
每当调用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;
}
rate
是double
,但getRate()
将其视为int
。
最简单的解决方案是将int getRate()
更改为double getRate()
。
相关文章:
- 返回常量对象引用 (getter) 和仅返回字符串有什么区别?
- 派生类继承的 getter,找不到返回正确值的方法
- C Getter不返回值(null)
- 在C 性能问题中使用Getter返回地图
- C++:getter 如何返回指向动态分配数组的指针,而不直接访问它
- 按Qt类的getter中的值返回
- 正确的方法返回getter中的字符串参考
- 此getter是否返回由构造函数或对班内对象的引用给出的引用?C
- 如何使编译器不优化 getter 的返回值并使其恒定
- 返回指向常量getter的指针时,返回值类型与函数类型不匹配
- C++getter函数返回0
- 如何编写getter方法,使其返回右值
- C++getter应该返回什么
- 从getter方法返回常量和非常量值
- c++getter没有在类外返回更改后的值
- Getter工作了,但当我将其转换为常量时返回一个错误
- C++重载getter两次,一次返回指针,另一次返回常量引用,都失败了
- 返回矢量大小的 getter 应该返回什么类型的值
- Getter函数返回错误的值
- 返回其他类对象的c++类getter中的错误