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
#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;
}
Comments
Leave a comment