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.
//
// 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
Comments
Leave a comment