Answer to Question #212211 in C++ for Hemambar

Question #212211

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 +,-,*,and / appropriately


1
Expert's answer
2021-06-30T13:42:40-0400
#include <iostream>
#include <string>
using namespace std;
class bMoney{
    long double amount;
    public:
    bMoney(){}
    void ms_to_ld(string s){
        amount = stold(s);
    }
    string ld_to_ms(){
        return to_string(amount);
    }
    void getmoney(string s){
        ms_to_ld(s);
    }
    void putmoney(){
        cout<<ld_to_ms()<<endl;
    }
    bMoney operator+(const bMoney& other){
        bMoney result;
        result.amount = this->amount + other.amount;
        return result;
    }
    bMoney operator-(const bMoney& other){
        bMoney result;
        result.amount = this->amount - other.amount;
        return result;
    }
    bMoney operator*(const bMoney& other){
        bMoney result;
        result.amount = this->amount * other.amount;
        return result;
    }
    bMoney operator/(const bMoney& other){
        bMoney result;
        result.amount = this->amount / other.amount;
        return result;
    }
};
int main(){
    bMoney money, mon;
    money.getmoney("24343324.234234");
    money.putmoney();
    mon.getmoney("84384344204.3432");
    mon.putmoney();
    (mon + money).putmoney();
    (mon - money).putmoney();
    (mon * money).putmoney();
    (mon / money).putmoney();
    return 0;
}

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