0

So I've read a few posts on how to store money and have heard it a bad idea to just store it as a double or float, and instead, a money class should be used for all money handling e.g balance, deposit, withdrawal. However I am now debating if I should just have 1 member value an int and that holds the whole value in pence, or have 2 members 1 int for pounds and another for pence? What is the better approach and if so why?

WHAT I HAVE SO FAR

#pragma once
#include <string>

class Money{
private:
    long pounds;
    int pence;

public:
    Money() : pounds(0), pence(0) {};
    Money(const long& pounds, const int& pence) : pounds(pounds), pence(pence){};  
    Money (const long& pounds) : pounds(pounds), pence(0){};

    // Overload operators to allow easier arithmetic of money objects
    Money operator+(const Money& moneyRhs) const; //constructer for addition  
    Money operator-(const Money& moneyRhs) const; //constructor for subtraction 
    Money operator*(const Money& moneyRhs) const; //constructor for multiply
    Money operator/(const Money& moneyRhs) const; //constructor for division 

    // toString method to print out money object
    std::string toString() const; 
};

Am I missing anything else? my basic project is a simple banking system that can make current and savings accounts and users can transfer, deposit, and withdraw money, and I plan to use the money class for my 'balance' data type in the Account (base abstract class) class that current and savings will inherit, the balance will also be a pointer. Thank you.

0 Answers0