I have a hierarchy of Money , Dollar and Franc classes that simulate money. It is necessary in the style of OOP to implement the following:
- Multiply money by number
- Money comparison
- Test code
I wrote this:
#ifndef MONEY_H #define MONEY_H #include <memory> using std::shared_ptr; using std::make_shared; class Dollar; class Money { public: static shared_ptr<Money> dollar(int amount); virtual shared_ptr<Money> times(int value) const = 0; virtual bool equals(shared_ptr<Money> money) const = 0; protected: int amount; }; class Dollar : public Money { public: Dollar(int amount); shared_ptr<Money> times(int value) const override; bool operator==(const Dollar& dollar) const; bool equals(shared_ptr<Money> dollar) const override; }; class Franc : public Money { public: Franc(int amount); shared_ptr<Money> times(int value) const override; bool operator==(const Franc& franc) const; bool equals(shared_ptr<Money> dollar) const override; }; #endif #include "money.h" #include <gmock\gmock.h> shared_ptr<Money> Money::dollar(int amount) { return make_shared<Dollar>(amount); } Dollar::Dollar(int amount) { this->amount = amount; } shared_ptr<Money> Dollar::times(int value) const { return make_shared<Dollar>(amount * value); } bool Dollar::operator==(const Dollar& dollar) const { return amount == dollar.amount; } bool Dollar::equals(shared_ptr<Money> dollar) const { if (dynamic_cast<shared_ptr<Dollar>>(dollar)) { return true; // TODO } return false; } Franc::Franc(int amount) { this->amount = amount; } shared_ptr<Money> Franc::times(int value) const { return make_shared<Franc>(amount * value); } bool Franc::operator==(const Franc& franc) const { return amount == franc.amount; } using ::testing::Eq; TEST(Money, TestDollarMultiplication) { shared_ptr<Money> five = Money::dollar(5); ASSERT_TRUE(five->times(2) == Dollar(10)); ASSERT_TRUE(five->times(3) == Dollar(15)); } TEST(Money, TestFrancMultiplication) { const Franc five(5); ASSERT_TRUE(five.times(2) == Franc(10)); ASSERT_TRUE(five.times(3) == Franc(15)); } There were problems due to tests. It is necessary to compare money with each other. Since I work with pointers due to polymorphism, this brings additional inconvenience.
It turned out that after the transition from objects to pointers, the test stopped compiling:
TEST(Money, TestDollarMultiplication) { shared_ptr<Money> five = Money::dollar(5); ASSERT_TRUE(five->times(2) == Dollar(10)); ASSERT_TRUE(five->times(3) == Dollar(15)); } At first I used the comparison operator, but now I needed the equals function for comparison by shared points.
bool Dollar::equals(shared_ptr<Money> dollar) const { if (dynamic_cast<shared_ptr<Dollar>>(dollar)) { return true; // TODO } return false; } You need to make sure that pointers to objects of the same type are compared. I use RTTI for this, but using this dynamic_cast results in an error.
How to do right?
shared_ptr, nordynamic_castare particularly needed for solving this task ... - VTT