/和 * 在C++货币类实现

eqoofvh9  于 2023-06-25  发布在  其他
关注(0)|答案(1)|浏览(152)

Money.h

  1. #pragma once
  2. #include <string>
  3. class Money {
  4. private:
  5. long pounds;
  6. int pence;
  7. public:
  8. Money();
  9. // Overloaded constructors
  10. explicit Money(long pounds);
  11. Money(long pounds, int pence);
  12. /* Overload operators to allow easier arithmetic of money objects, we will
  13. not overload * or / as it does not make logical sense for money to be multiplied
  14. or divided.
  15. */
  16. Money operator+(const Money& moneyRhs) const;
  17. Money operator-(const Money& moneyRhs) const;
  18. friend std::ostream& operator<<(std::ostream& os, const Money& money);
  19. // toString method to print out money object
  20. std::string toString() const;
  21. // Getters
  22. long getPounds() const;
  23. int getPence() const;
  24. };

Money.cpp

  1. #include "Money.h"
  2. #include <iomanip>
  3. Money::Money(): pounds(0), pence(0) {}
  4. Money::Money(const long pounds): pounds(pounds), pence(0) {}
  5. Money::Money(const long pounds, const int pence): pounds(pounds), pence(pence) {}
  6. Money Money::operator+(const Money& moneyRhs) const {
  7. // Convert all money to pence then do addition
  8. const long poundsInPence = (pounds + moneyRhs.pounds) * 100;
  9. const int totalPence = pence + moneyRhs.pence;
  10. const long allPence = poundsInPence + totalPence;
  11. const Money m3 = Money(allPence / 100, allPence % 100);
  12. return m3;
  13. }
  14. Money Money::operator-(const Money& moneyRhs) const {
  15. // Convert all money to pence then do subtraction
  16. const long poundsInPence = (pounds - moneyRhs.pounds) * 100;
  17. const int totalPence = pence - moneyRhs.pence;
  18. const long allPence = poundsInPence + totalPence;
  19. const Money m3 = Money(allPence / 100, allPence % 100);
  20. return m3;
  21. }
  22. std::string Money::toString() const {
  23. std::string strMoneyFormat;
  24. // Check so see if the pence value is 1 digit, if so we need to add a trailing 0 for output
  25. // e.g £150.5 becomes £150.05
  26. if((getPence() > 0 ? static_cast<int>(log10(static_cast<double>(getPence()))) + 1 : 1) < 2) {
  27. strMoneyFormat = std::to_string(getPounds()) + "." + "0" + std::to_string(getPence());
  28. }
  29. else {
  30. strMoneyFormat = std::to_string(getPounds()) + "." + std::to_string(getPence());
  31. }
  32. return strMoneyFormat;
  33. }
  34. std::ostream& operator<<(std::ostream& os, const Money& money) {
  35. os << money.toString();
  36. return os;
  37. }
  38. long Money::getPounds() const {
  39. return pounds;
  40. }
  41. int Money::getPence() const {
  42. return pence;
  43. }

我有一个基本的英国银行应用程序的上述货币类实现,但是,我知道在编码中,一般来说,如果你重载了一种类型的运算符,这是最好的做法,例如算术,你也应该重载它的其他运算符,所以这里我重载了+和-,所以我需要重载/和 *。然而,它没有太大意义的乘法或除法的钱,有没有一种方法,我可以去重载这些操作符,任何人都知道,这将是有意义的?
更新:

  1. template <class T>
  2. Money operator*(T number) const {
  3. const int penceMult = pence * number;
  4. const int newPence = penceMult % 100;
  5. const long newPounds = pounds * number + (penceMult / 100);
  6. Money tmp(newPounds, newPence);
  7. return tmp;
  8. }
  9. template <class T>
  10. Money operator/(T number) const {
  11. if (number == 0) {
  12. throw std::invalid_argument("Division by zero");
  13. }
  14. long total = (100 * pounds) + pence;
  15. const long result = total / number;
  16. const int newPence = result % 100;
  17. const long newPounds = result / 100;
  18. Money tmp(newPounds, newPence);
  19. return tmp;
  20. }
pw9qyyiw

pw9qyyiw1#

让我们在这里通过类比来推理。假设我有一个表示4D空间中向量的类型。我可以对这些向量进行加减运算,但是没有一种有意义的方法可以将这些向量相乘(我的意思是,有一种点积,但这并不产生向量)。因此,即使我可能会重载类型的+和-运算符,我也不会在两个向量上实现 * 或/。(我可能会重载 * 和/,因为这在数学上是有意义的。)我也不会重载%。
类似地,假设我有一个表示字符串的类型。在这里重载+和+=是有意义的,但我不知道-或-=对字符串意味着什么,也不知道 * 或/应该做什么。
最后,假设我有一个表示不可变量的类型。然后我可能会在它上面实现+和-和 * 和/,但我不会在那个类型上实现+=,-=,*=或/=。
所有这些都是说,“如果你重载了一个操作符,你也应该重载相关的操作符”的建议对我来说并不正确。如果重载一个运算符以询问是否还有其他相关运算符也值得重载(例如,如果重载了+,则+=),这是值得的,但这并不意味着您应该为本质上没有意义的运算符添加重载,只是为了“舍入”一组运算符。

相关问题