Answer to Question #216060 in C++ for Hemambar

Question #216060

Create a class called bMoney. It should store money amounts as long doubles. Use the function ms_to_ld() to convert a money string entered as input into a long double, and the function ld_to_ms() to convert the long double to a money string for display. You can call the input and output member functions getmoney() and putmoney().

Overload the operators +,-,*,/ appropriately.


1
Expert's answer
2021-07-18T04:23:36-0400
//
// Created by azimjon on 7/18/21.
//
#include <iostream>
#include <string>

using namespace std;

class bMoney
{
private:
    long double dAmount;
    string sAmount;
public:
    bMoney(long double d = 0, string s = "") {
        dAmount = d;
        sAmount = s;
    }

    bMoney operator + (bMoney const &obj) {
        bMoney res;
        res.dAmount = dAmount + obj.dAmount;
        res.sAmount = sAmount + obj.sAmount;
        return res;
    }
    bMoney operator - (bMoney const &obj) {
        bMoney res;
        res.dAmount = dAmount - obj.dAmount;
        return res;
    }
    bMoney operator * (bMoney const &obj) {
        bMoney res;
        res.dAmount = dAmount * obj.dAmount;
        return res;
    }
    bMoney operator / (bMoney const &obj) {
        bMoney res;
        res.dAmount = dAmount / obj.dAmount;
        return res;
    }
    long double ms_to_ld() {
        double d = stod(sAmount);
        return d;
    }
    string ld_to_ms() {
        string s = to_string(dAmount);
        return s;
    }

    long double getDAmount() const {
        return dAmount;
    }

    void setDAmount(long double dAmount) {
        bMoney::dAmount = dAmount;
    }

    const string &getSAmount() const {
        return sAmount;
    }

    void setSAmount(const string &sAmount) {
        bMoney::sAmount = sAmount;
    }


};

int main()
{
    bMoney test = bMoney(123.4, "345.4");
    cout << test.ld_to_ms() << '\n';
    cout << test.ms_to_ld() << '\n';

    return 0;
}

Output:
123.400000
345.4

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS