Question

// P13_2.cpp - This program adds money of two different people #include<iostream> #include<cstdlib> using namespace std;...

// P13_2.cpp - This program adds money of two different people
#include<iostream>
#include<cstdlib>
using namespace std;

class AltMoney
{
    public:
        AltMoney();
        AltMoney(int d, int c);

        friend void add(AltMoney m1, AltMoney m2, AltMoney& sum);
        void display_money( );
    private:
        int dollars;
        int cents;
};

void read_money(int& d, int& c);

int main( )
{
     int d, c;
     AltMoney m1, m2, sum;

     sum = AltMoney(0,0);

     read_money(d, c);
     m1 = AltMoney(d,c);
     cout << "The first money is:";
     m1.display_money();

     read_money(d, c);
     m2 = AltMoney(d,c);
     cout << "The second money is:";
     m2.display_money();

     add(m1,m2, sum);
     cout << "The sum is:";
     sum.display_money();

     return 0;
}

AltMoney::AltMoney()
{
}

AltMoney::AltMoney(int d, int c)
{
       dollars = d;
       cents = c;
}

void AltMoney::display_money()
{
     cout << "$" << dollars << ".";
     if(cents <= 9)
         cout << "0"; //to display a 0 in the left for numbers less than 10
     cout << cents << endl;
}

void add(AltMoney m1, AltMoney m2, AltMoney& sum)
{
     int extra = 0;
     sum.cents = m1.cents + m2.cents;
     if(sum.cents >=100){
         sum.cents = sum.cents - 100;
         extra = 1;
      }
      sum.dollars = m1.dollars + m2.dollars + extra;
}

void read_money(int& d, int& c)
{
     cout << "Enter dollar \n";
     cin >> d;
     cout << "Enter cents \n";
     cin >> c;
     if( d < 0 || c < 0)
     {
            cout << "Invalid dollars and cents, negative values\n";
            exit(1);
      }
}

In this program the function add is no longer a member function, but it is a friend to the class AltMoney and has access to private variable members. In general, we may want to include the function that does the reading of variables as a function member or make it a friend so that it reads the dollars and cents and directly assigns them to the dollars and cents of an object.

Exercise 13.2

Modify the above C++ program to include the following changes. Call your new program ex13_2.cpp.

1. Modify the previous add function so that it becomes now a member function of the class AltMoney. The function takes the sum of dollars and cents of an object AltMoney and returns the result as an AltMoney. Note that in the above version of the program, the object sum has been passed by reference. Do not pass the sum any longer.

2. Add a new friend function, called subtract, that computes the subtraction of one money from another.

3. Modify read_money and make it member function of AltMoney. Note that if you make read_money a member function, then you can use it to directly initialize the dollars and cents of an AltMoney type object directly.

4. Modify the main to reflect that above changes and the program is still functional, and add a call to subtract to test that the function subtract works properly

0 0
Add a comment Improve this question Transcribed image text
Answer #1

****This requires some effort so please drop a like if you are satisfied with the solution****

I have satisfied all the requires changes as per the question and I'm providing the screenshots of code and output for your reference...

Code:

#include<iostream>
#include<cstdlib>
using namespace std;

class AltMoney
{
public:
AltMoney();
AltMoney(int d, int c);

AltMoney add(AltMoney m1, AltMoney m2);
void read_money(AltMoney& obj);
friend void subtract(AltMoney m1, AltMoney m2, AltMoney& sub);
void display_money( );
private:
int dollars;
int cents;
};

void read_money(int& d, int& c);

int main( )
{
AltMoney m1, m2, sum,sub;

sum = AltMoney(0,0);

m1.read_money(m1);
cout << "The first money is:";
m1.display_money();

m2.read_money(m2);
cout << "The second money is:";
m2.display_money();

sum=sum.add(m1,m2);
cout << "The sum is:";
sum.display_money();
subtract(m1,m2,sub);
cout<<"The subtraction is:";
sub.display_money();

return 0;
}

AltMoney::AltMoney()
{
}

AltMoney::AltMoney(int d, int c)
{
dollars = d;
cents = c;
}

void AltMoney::display_money()
{
cout << "$" << dollars << ".";
if(cents <= 9)
cout << "0"; //to display a 0 in the left for numbers less than 10
cout << cents << endl;
}

AltMoney AltMoney::add(AltMoney m1, AltMoney m2)
{
int extra = 0;
AltMoney sum;
sum.cents = m1.cents + m2.cents;
if(sum.cents >=100){
sum.cents = sum.cents - 100;
extra = 1;
}
sum.dollars = m1.dollars + m2.dollars + extra;
return sum;
}
void subtract(AltMoney m1, AltMoney m2, AltMoney& sub)
{
int extra = 0;
sub.cents = m1.cents - m2.cents;
if(sub.cents <0){
sub.cents = 100+sub.cents;
extra = 1;
}
sub.dollars = m1.dollars - m2.dollars - extra;
}

void AltMoney::read_money(AltMoney& obj)
{
   cout << "Enter dollar \n";
cin >> obj.dollars;
cout << "Enter cents \n";
cin >> obj.cents;
if( obj.dollars < 0 || obj.cents < 0)
{
cout << "Invalid dollars and cents, negative values\n";
exit(1);
}
}

Output Screenshot:

Code Screenshots:

Add a comment
Know the answer?
Add Answer to:
// P13_2.cpp - This program adds money of two different people #include<iostream> #include<cstdlib> using namespace std;...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Here are the files I've made so far: /********************* * File: check05b.cpp *********************/ #include &lt...

    Here are the files I've made so far: /********************* * File: check05b.cpp *********************/ #include <iostream> using namespace std; #include "money.h" /**************************************************************** * Function: main * Purpose: Test the money class ****************************************************************/ int main() {    int dollars;    int cents;    cout << "Dollar amount: ";    cin >> dollars;    cout << "Cents amount: ";    cin >> cents;    Money m1;    Money m2(dollars);    Money m3(dollars, cents);    cout << "Default constructor: ";    m1.display();    cout << endl;    cout << "Non-default constructor 1: ";    m2.display();    cout << endl;   ...

  • Overview This checkpoint is intended to help you practice the syntax of operator overloading with member...

    Overview This checkpoint is intended to help you practice the syntax of operator overloading with member functions in C++. You will work with the same money class that you did in Checkpoint A, implementing a few more operators. Instructions Begin with a working (but simplistic) implementation of a Money class that contains integer values for dollars and cents. Here is my code: /********************* * File: check12b.cpp *********************/ #include using namespace std; #include "money.h" /**************************************************************** * Function: main * Purpose: Test...

  • #include <iostream> #include <string> #include <stdio.h> using namespace std; /** The FeetInches class holds distances measured...

    #include <iostream> #include <string> #include <stdio.h> using namespace std; /** The FeetInches class holds distances measured in feet and inches. */ class FeetInches { private: int feet; // The number of feet int inches; // The number of inches /** The simplify method adjusts the values in feet and inches to conform to a standard measurement. */ void simplify() { if (inches > 11) { feet = feet + (inches / 12); inches = inches % 12; } } /**...

  • Need help with the flow chart of this program #include #include using namespace std; string months[]...

    Need help with the flow chart of this program #include #include using namespace std; string months[] = {"January","February ","March","April","May","June","July","August","September","October","November","December"}; int days_in_months[] = {31,28,31,30,31,30,31,31,30,31,30,31}; class Date //Sample Class for the C++ Tutorial { private: int month; //Data member int day; // Data member int year; public: Date(int m, int d, int y) { if(m<1 || m >12) month = 1; else month = m; if(d<1 || d >days_in_months[month-1]) day = 1; else day = d; if(y<2001) year = 2001; else year...

  • CODES: main.cpp #include <iostream> #include <string> #include "ShoppingCart.h" using namespace std; char PrintMenu() { char answer;...

    CODES: main.cpp #include <iostream> #include <string> #include "ShoppingCart.h" using namespace std; char PrintMenu() { char answer; cout << "MENU" << endl; cout << "a - Add item to cart" << endl; cout << "d - Remove item from cart" << endl; cout << "c - Change item quantity" << endl; cout << "i - Output items' descriptions" << endl; cout << "o - Output shopping cart" << endl; cout << "q - Quit" << endl << endl; while (true) {...

  • Arrays C++ #include <iostream> using namespace std; int main() {       int   tests[6]; // array declaration...

    Arrays C++ #include <iostream> using namespace std; int main() {       int   tests[6]; // array declaration       int   sum = 0;       float avg;       //input test scores       cout << " Enter " << 6 << " test scores: " << endl;       for (int i = 0; i < 6; i++)       {             cout << "Enter Test " << i + 1 << ": ";             cin >> tests[i];       }       return 0; }    Type...

  • #include <iostream> #include <string> using namespace std; int main() { int number; int sum = 0;...

    #include <iostream> #include <string> using namespace std; int main() { int number; int sum = 0; while(true) { cout << "Please enter a number between 1 and 11: "; cin >> number; if (number >= 1 && number <= 11) { cout << number << endl; sum = sum + number; //only add the sum when number is in range: 1-11, so add wthin this if case } else { cout << number << endl; cout << "Out of range;...

  • Consider the following C++ program: #include <iostream> #include <cstdlib> using namespace std; int main int n...

    Consider the following C++ program: #include <iostream> #include <cstdlib> using namespace std; int main int n =0; int i = 0; cout << "Please enter a strictly positive number:"; cin >> n if (n <= 0) exit(EXIT_FAILURE) while (n > 1) n-n/2; i << endl; cout"Output:" return 0; Answer the following questions: What is the output of the program for each of the following values of n: -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9? What does...

  • #include<iostream> #include<string> #include<iomanip> using namespace std; /* ********* Class Car ************* ********************************* */ class Car {...

    #include<iostream> #include<string> #include<iomanip> using namespace std; /* ********* Class Car ************* ********************************* */ class Car { private: string reportingMark; int carNumber; string kind; bool loaded; string choice; string destination; public: Car() { reportingMark = ""; carNumber = 0; kind = "Others"; loaded = 0; destination = "NONE"; } ~Car() { } void setUpCar(string &reportingMark, int &carNumber, string &kind, bool &loaded, string &destination); }; void input(string &reportingMark, int &carNumber, string &kind, bool &loaded,string choice, string &destination); void output(string &reportingMark, int &carNumber,...

  • Please complete Part 1. The code is also below: #include <pthread.h> #include <iostream> using namespace std;...

    Please complete Part 1. The code is also below: #include <pthread.h> #include <iostream> using namespace std; void *PrintHello(void *arg) { int actual_arg = *((int*) arg); cout << "Hello World from thread with arg: " << actual_arg << "!\n"; return 0; } int main() { pthread_t id; int rc; cout << "In main: creating thread \n"; int t = 23; rc = pthread_create(&id, NULL, PrintHello, (void*) &t); if (rc){ cout << "ERROR; return code from pthread_create() is " << rc <<...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT