Question

In: Computer Science

First make the changes in P82.cpp and call the new program ex82.cpp. Compile and run the...

First make the changes in P82.cpp and call the new program ex82.cpp. Compile and run the program and make sure it produces the correct results. Here is what you need to do for the exercise:

    Overload the % operator such that every time you use it, it takes two objects of type AltMoney as its arguments and returns:
            a) 5% of the difference between the income and expenditure, if income is larger than the expenditure
            b) -2% if the the expenditure is larger than the income.
            c) 0 if the expenditure is the same as income
Note that, by doing this, you are required to overload the greater than sign (>), the smaller than sign (<), and the == sign.

#include<iostream>

#include<cstdlib>

using namespace std;

class AltMoney

{

public:

       AltMoney();

       AltMoney(int d, int c);

       friend AltMoney operator +(AltMoney m1, AltMoney m2);

       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();

       sum = m1 + m2;

       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 on the left for numbers less than 10

       cout << cents << endl;

}

AltMoney operator +(AltMoney m1, AltMoney m2)

{

       AltMoney temp;

       int extra = 0;

       temp.cents = m1.cents + m2.cents;

       if (temp.cents >= 100) {

             temp.cents = temp.cents - 100;

             extra = 1;

       }

       temp.dollars = m1.dollars + m2.dollars + extra;

       return temp;

}

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);

       }

}

Solutions

Expert Solution

//Modified program

#include<iostream>

#include<cstdlib>

using namespace std;

class AltMoney

{

public:

AltMoney();

AltMoney(int d, int c);

friend AltMoney operator +(AltMoney m1, AltMoney m2);

friend double operator %(AltMoney income , AltMoney expenditure);

bool operator >(AltMoney m);

bool operator <(AltMoney m);

bool operator ==(AltMoney m);

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();

sum = m1 + m2;

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 on the left for numbers less than 10

cout << cents << endl;

}

AltMoney operator +(AltMoney m1, AltMoney m2)

{

AltMoney temp;

int extra = 0;

temp.cents = m1.cents + m2.cents;

if (temp.cents >= 100) {

temp.cents = temp.cents - 100;

extra = 1;

}

temp.dollars = m1.dollars + m2.dollars + extra;

return temp;

}

bool AltMoney::operator >(AltMoney m){
   if(this->dollars>m.dollars)return true;
   if(this->dollars==m.dollars)
       if(this->cents>m.cents)return true;
   return false;
}
bool AltMoney::operator <(AltMoney m){
   if(this->dollars<m.dollars)return true;
   if(this->dollars==m.dollars)
       if(this->cents<m.cents)return true;
   return false;
}
bool AltMoney::operator ==(AltMoney m){
   return (this->dollars==m.dollars && this->cents==m.cents);
}

double operator %(AltMoney income , AltMoney expenditure){
   if(income>expenditure){
       return 0.05*((income.dollars*100+income.cents)-(expenditure.dollars*100+expenditure.cents));
   }
   else if(income<expenditure){
       return 0.02*((expenditure.dollars*100+expenditure.cents)-(income.dollars*100+income.cents));
   }
   return 0;
}

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);

}

}


Related Solutions

Please make the following changes to the following code: #This program takes in the first and...
Please make the following changes to the following code: #This program takes in the first and last name and creates a userID printing first letter of first name and first 7 characters of last name #by Abi Santo #9/20/20 def main(): print("This Program takes your first and last name and makes a User ID") f_name = str(input("Please enter your first name ")).lower() l_name = str(input("Enter your last name: ")).lower() userID = str(f_name[:1]+str(l_name[:7])) print(userID) main() Call the function createUserName(). This function...
in C++ Compile and run the program for the following values: r = 2 Cm, h...
in C++ Compile and run the program for the following values: r = 2 Cm, h = 10 Cm. The answer should be: The cross section area of the cylinder is 3.8955634 c The side area of the cylinder is 19.474819 inch-sqr Did you get the same answer? Explain the reason for such an error and fix the problem. Modify the previous program to include a new function called total_area, that computes the total suface area of a cylinder. The...
6. Write a program in C programming (compile and run), a pseudocode, and draw a flowchart...
6. Write a program in C programming (compile and run), a pseudocode, and draw a flowchart for each of the following problems: a) Obtain three numbers from the keyboard, compute their product and display the result. b) Obtain two numbers from the keyboard, and determine and display which (if either) is the smaller of the two numbers. c) Obtain a series of positive numbers from the keyboard, and determine and display their average (with 4 decimal points). Assume that the...
Objectives To learn to code, compile, and run a program using file input and an output...
Objectives To learn to code, compile, and run a program using file input and an output file. Assignment Plan and code a program utilizing one file for input and one file for output to solve the following problem: Write a program to determine the highest number, the lowest number, their total, and the average of each line of numbers in a file. A file contains 7 numbers per line. How many lines a file contains is unknown. Note Label all...
UNIX/LINUX LAB (1) Review the sample shell program (tryShell.c), and compile/run the program (tryShell) with the...
UNIX/LINUX LAB (1) Review the sample shell program (tryShell.c), and compile/run the program (tryShell) with the following commands, and at the end, use CTRL+C to terminate the program: ls ls -l tryShell* date whoami hostname uname -a ctrl+C    (2) Run the program (tryShell) with "time -p" with a few commands: time -p ./tryShell (3) Edit the program (tryShell.c) so that it will exit (terminate the program) when the input command string is "exit" try shell.c code at bottom //////////// #include...
We will make some changes to our first program. If you recall we began with, A...
We will make some changes to our first program. If you recall we began with, A car's gas mileage or miles-per-gallon (MPG) can be calculated with the following Formula: MPG = Miles Driven / Gallons of gas used. Write a class called Mileage. The Mileage class should have two private member variables called miles and gallons of type double. The class should have four public methods: setMiles and setGallons should use void return types; getMiles and getGallons should use double...
Task #1 void Methods Copy the file geometry.cpp. This program will compile, but when you run...
Task #1 void Methods Copy the file geometry.cpp. This program will compile, but when you run it , it doesn’t appear to do anything except wait. That is because it is waiting for user input, but the user doesn’t have the menu to choose from yet. We will need to create this. Above the main, write the prototype for a function called PrintMenu that has no parameter list and does not return a value.    Below the main, write the function...
Create Lab project Add the following codes to your main method. Compile and run the program...
Create Lab project Add the following codes to your main method. Compile and run the program with some arguments and observe the output.       printf("You have entered %d arguments.\n", argc);       for (int i = 0; i < argc; ++i)         printf( "argument %d: %s\n",i+1,argv[i]); Create a function called simpleFileWrite() and add the following feature: Open a write only file called “myfile.txt” and write characters into it. The set of characters are read form the keyboard until cntrl-z is entered. (use...
Compile and run the following code then answer the following questions: a.) Run this command from...
Compile and run the following code then answer the following questions: a.) Run this command from the shell prompt: ./a.out ls -F Explain, in your own words, why you see the screen output you do and how that output relates to both the content of the program AND the nature of the shell command used to invoke it. Be sure to comment on the pointer arithmetic done inside the line: execvp(*(argv+1), argv+1); b.) Run this command from the shell prompt:...
You are going to make an advanced ATM program When you run the program, it will...
You are going to make an advanced ATM program When you run the program, it will ask you if you want to: Either log in or make a new user Exit Once I have logged in, I will have 3 balances, credit, checking, and savings. From here it will ask which account to use. Once the account is picked they can deposit, withdraw, check balance, or log out. Each time the user performs one of these actions, it will loop...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT