In: Computer Science
12. 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();
};
Program:
#include<iostream>
using namespace std;
//class bMoney
class bMoney
{
private:
long double money;
public:
bMoney(){money=0;}
bMoney(char s[]){
money = mstold(s);
}
void madd(bMoney m1, bMoney m2){
money = m1.money + m2.money;
}
void getmoney(){
char ms[10];
cin >> ms;
money = mstold(ms);
}
void putmoney(){
cout<<ldtoms(money)<<endl;
}
};
//main function
int main()
{
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();
return 0;
}
Output:
Enter money string :31.35
Enter money string :41.75
31.35
41.75
After addition: 73.10
N.B. Don't forget to include the definition of mstold() and ldtoms() functions to this program, otherwise you will get error message.
Solving your question and
helping you to well understand it is my focus. So if you face any
difficulties regarding this please let me know through the
comments. I will try my best to assist you. However if you are
satisfied with the answer please don't forget to give your
feedback. Your feedback is very precious to us, so don't give
negative feedback without showing proper reason.
Thank you.