Answer to Question #218598 in C++ for Hemambar

Question #218598

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-19T00:35:36-0400
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>//input output stream
#include <string>//use string type and function 
using namespace std;
//Define and implement class bMoney
class bMoney
{
	//private section
private:
	long double money;//money amounts
//public section
public:
	//default constructor
	bMoney()
	{
		this->money = 0.0;
	}
	//parametrized construictor
	bMoney(const long double dm)
	{
		this->money = dm;
	}
	//get money
	long double getmoney()const
	{
		return this->money;
	}
	void putmoney(const long double dm)
	{
		this->money = dm;
	}
	//money string to long double
	void ms_to_ld(const string sm)//sm -money string
	{
		this->money = atof(sm.c_str());
		//display
		cout << "Long double: "<<getmoney() << endl;
    }
	//money long double to string
	string ld_to_ms(string val="$")//val="Evro" or..... format strig 120$
	{
		//convert money to string
		char buf[18];
		sprintf(buf, "%lf", this->getmoney());
		string ans(buf);
		ans += val;
		return ans;
	}
	//overloading operator = for +-/*
	bMoney &operator=(const bMoney& t)
	{
		this->money = t.getmoney();
		return *this;
	}
	//overload operator
	friend bMoney& operator+(const bMoney& a, const bMoney& b);
	friend bMoney& operator-(const bMoney& a, const bMoney& b);
	friend bMoney& operator*(const bMoney& a, const bMoney& b);
	friend bMoney& operator/(const bMoney& a, const bMoney& b);
};
//Implement overload operator+
bMoney& operator+(const bMoney& a, const bMoney& b)
{
	bMoney ans;
	ans.putmoney(a.getmoney() + b.getmoney());
	return ans;
}
//Implement operator -
bMoney& operator-(const bMoney& a, const bMoney& b)
{
	bMoney ans;
	ans.putmoney(a.getmoney() - b.getmoney());
	return ans;
}
//operator *
bMoney& operator*(const bMoney& a, const bMoney& b)
{
	bMoney ans;
	ans.putmoney(a.getmoney() * b.getmoney());
	return ans;
}
//operator /
bMoney& operator/(const bMoney& a, const bMoney& b)
{
	bMoney ans;
	ans.putmoney(a.getmoney() / b.getmoney());
	return ans;
}
int main()
{
	//Demonstration all
	bool quit = 0;
	bMoney mn;
	while (!quit)
	{
		cout << "Menu\n";
		cout << "\t 1 -Input money\n";
		cout << "\t 2 -Out money\n";
		cout << "\t 3 -String To MoneyI\n";
		cout << "\t 4 -Money to String\n";
		cout << "\t 5 -Operation(+-*/)\n";
		cout << "\t 6 -Exit\n";
		int cmd;
		cin >> cmd;
		switch (cmd)
		{
		case 1:
		{
			long double dm;
			cout << "Please money amounts enter: ";
			cin >> dm;
			mn.putmoney(dm);
			cout << "Money value changed succefully!!\n";
			break;//1
		}
		case 2:
		{


			cout << mn.getmoney() << endl;
			break;//out
		}
		case 3:
		{
			string str;
			cout << "Enter string : ";
			cin >> str;
			mn.ms_to_ld(str);
			//
			break;//
		}
		case 4:
		{
			string ans = mn.ld_to_ms();
			cout << ans << endl;
			break;
		}
		case 5:
		{
			cout << "Sample ofline test:\n";
			bMoney aa(155);
			bMoney bb(120);
			cout << "a: " << aa.getmoney() << endl;
			cout << "b: " << bb.getmoney() << endl;
			bMoney* add = new bMoney;
			*add = aa + bb;
			cout << "a+b: " << add->getmoney() << endl;
			*add = aa - bb;
			cout << "a-b: " << add->getmoney() << endl;
			*add = aa * bb;
			cout << "a*b: " << add->getmoney() << endl;
			*add = aa / bb;
			cout << "a/b: " << add->getmoney() << endl;
			delete add;
			break;
		}


		case 6:
		{
			cout << "BYE\n";
			quit = 1;
			break;
		}
		}
	}
	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