Money.h
#pragma once
#include <string>
class Money {
private:
long pounds;
int pence;
public:
Money();
// Overloaded constructors
explicit Money(long pounds);
Money(long pounds, int pence);
/* Overload operators to allow easier arithmetic of money objects, we will
not overload * or / as it does not make logical sense for money to be multiplied
or divided.
*/
Money operator+(const Money& moneyRhs) const;
Money operator-(const Money& moneyRhs) const;
friend std::ostream& operator<<(std::ostream& os, const Money& money);
// toString method to print out money object
std::string toString() const;
// Getters
long getPounds() const;
int getPence() const;
};
Money.cpp
#include "Money.h"
#include <iomanip>
Money::Money(): pounds(0), pence(0) {}
Money::Money(const long pounds): pounds(pounds), pence(0) {}
Money::Money(const long pounds, const int pence): pounds(pounds), pence(pence) {}
Money Money::operator+(const Money& moneyRhs) const {
// Convert all money to pence then do addition
const long poundsInPence = (pounds + moneyRhs.pounds) * 100;
const int totalPence = pence + moneyRhs.pence;
const long allPence = poundsInPence + totalPence;
const Money m3 = Money(allPence / 100, allPence % 100);
return m3;
}
Money Money::operator-(const Money& moneyRhs) const {
// Convert all money to pence then do subtraction
const long poundsInPence = (pounds - moneyRhs.pounds) * 100;
const int totalPence = pence - moneyRhs.pence;
const long allPence = poundsInPence + totalPence;
const Money m3 = Money(allPence / 100, allPence % 100);
return m3;
}
std::string Money::toString() const {
std::string strMoneyFormat;
// Check so see if the pence value is 1 digit, if so we need to add a trailing 0 for output
// e.g £150.5 becomes £150.05
if((getPence() > 0 ? static_cast<int>(log10(static_cast<double>(getPence()))) + 1 : 1) < 2) {
strMoneyFormat = std::to_string(getPounds()) + "." + "0" + std::to_string(getPence());
}
else {
strMoneyFormat = std::to_string(getPounds()) + "." + std::to_string(getPence());
}
return strMoneyFormat;
}
std::ostream& operator<<(std::ostream& os, const Money& money) {
os << money.toString();
return os;
}
long Money::getPounds() const {
return pounds;
}
int Money::getPence() const {
return pence;
}
我有一个基本的英国银行应用程序的上述货币类实现,但是,我知道在编码中,一般来说,如果你重载了一种类型的运算符,这是最好的做法,例如算术,你也应该重载它的其他运算符,所以这里我重载了+和-,所以我需要重载/和 *。然而,它没有太大意义的乘法或除法的钱,有没有一种方法,我可以去重载这些操作符,任何人都知道,这将是有意义的?
更新:
template <class T>
Money operator*(T number) const {
const int penceMult = pence * number;
const int newPence = penceMult % 100;
const long newPounds = pounds * number + (penceMult / 100);
Money tmp(newPounds, newPence);
return tmp;
}
template <class T>
Money operator/(T number) const {
if (number == 0) {
throw std::invalid_argument("Division by zero");
}
long total = (100 * pounds) + pence;
const long result = total / number;
const int newPence = result % 100;
const long newPounds = result / 100;
Money tmp(newPounds, newPence);
return tmp;
}
1条答案
按热度按时间pw9qyyiw1#
让我们在这里通过类比来推理。假设我有一个表示4D空间中向量的类型。我可以对这些向量进行加减运算,但是没有一种有意义的方法可以将这些向量相乘(我的意思是,有一种点积,但这并不产生向量)。因此,即使我可能会重载类型的+和-运算符,我也不会在两个向量上实现 * 或/。(我可能会重载 * 和/,因为这在数学上是有意义的。)我也不会重载%。
类似地,假设我有一个表示字符串的类型。在这里重载+和+=是有意义的,但我不知道-或-=对字符串意味着什么,也不知道 * 或/应该做什么。
最后,假设我有一个表示不可变量的类型。然后我可能会在它上面实现+和-和 * 和/,但我不会在那个类型上实现+=,-=,*=或/=。
所有这些都是说,“如果你重载了一个操作符,你也应该重载相关的操作符”的建议对我来说并不正确。如果重载一个运算符以询问是否还有其他相关运算符也值得重载(例如,如果重载了+,则+=),这是值得的,但这并不意味着您应该为本质上没有意义的运算符添加重载,只是为了“舍入”一组运算符。