Answer to Question #249258 in C++ for Yasir

Question #249258
Create a class called bMoney. It should store money amounts as long doubles. Use the
function mstold() to convert a money string entered as input into a long double, and
the function ldtoms() to convert the long double to a money string for display. (See
Exercises 6 and 10.) You can call the input and output member functions getmoney()
and putmoney(). Write another member function that adds two bMoney amounts; you can
call it madd(). Adding bMoney objects is easy: Just add the long double member data
amounts in two bMoney objects. Write a main() program that repeatedly asks the user to
enter two money strings, and then displays the sum as a money string. Here’s how the
class specifier might look:
class bMoney
{
private:
long double money;
public:
bMoney();
bMoney(char s[]);
void madd(bMoney m1, bMoney m2);
void getmoney();
void putmoney();
};
1
Expert's answer
2021-10-10T09:09:29-0400
#include<iostream>
#include <string>
using namespace std;


long double mstold(string s){
	
    long double x = stold(s);
    return x;
}
string ldtoms(long double s){
	string str = to_string(s);
	return str;
}
class bMoney
{
private:
    long double money;
public:
    bMoney(){


	}
    bMoney(string s){
    	
        money = mstold(s);
        
        
    }
    void madd(bMoney m1, bMoney m2){
    	
        money = m1.money + m2.money;
    }
    void getmoney(){
        string st;
        cin >> st;
        money = mstold(st);
        
    }
    
    void putmoney(){
        cout<<ldtoms(money)<<endl;
    }
};










int main()
{
    char ch;
    do{
    bMoney m1, m2;
    cout<<"Enter money string :";
    m1.getmoney();
    
    cout<<"Enter money string :";
    m2.getmoney();




    bMoney m3;
    m3.madd(m1, m2);




    m1.putmoney();
    m2.putmoney();
    cout<<"After addition: ";
    m3.putmoney();
  cout<<"Enter y to continue or n to stop\n";
  cin>>ch;	
    	
	} while(ch != 'n');
	cout<<"Terminated successfully\n";
	


    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