EBK ABSOLUTE C++
EBK ABSOLUTE C++
6th Edition
ISBN: 9780133970944
Author: SAVITCH
Publisher: PEARSON CUSTOM PUB.(CONSIGNMENT)
bartleby

Videos

Textbook Question
Book Icon
Chapter 8, Problem 1PP

Modify the definition of the class Money shown in Display 8.5 so that the following are added:

  1. The operators < , < = , > , and > = have each been overloaded to apply to the type Money. (Hint: See Self-Test Exercise 8.)
  2. The following member function has been added to the class definition. (We show the function declaration as it should appear in the class definition. The definition of the function itself will include the qualifier Money::.)

const Money percent (int percent Figure) const ;

//Returns a percentage of the money amount in the calling

//object . For example, if percentFigure is 10, then the value

//returned is of the amount of money represented by the

//calling object.

For example, if purse is an object of type Money whose value represents the amount $100.10, then the call purse.percent (10) ;

returns 10% of $ 100.10; that is, it returns a value of type Money that represents the amount $10.01.

Expert Solution & Answer
Check Mark
Program Plan Intro

1. The following variables are used in the program.

  • dollarsvariable of integer data type is used to store the user input for dollars.
  • centsvariable of integer data type is used to store the user input for cents.

2. The following methods are used in the program:

  • Default constructor.
  • Constructor taking one argument of double data type.
  • Constructor taking two arguments of integer data type.
  • Constructor taking one argument of integer data type.
  • getAmount() function to take the amount from the user.
  • getDollars() function to take the month first three letter from the user.
  • getCents() function to return the month number.
  • output() function to display the month first three letter.

Program Description:

In this program overload the operators <, <=, >>= and apply to the type money Add the member function in class definition.

Explanation of Solution

#include<iostream>
#include<cstdlib>
#include<cmath>

using namespace std;

class Money
{
    private:
    int dollars, cents;

    public:

    Money();

    Money(double amount);
    Money(int theDollars,int theCents);
    Money(int theDollars);

    double getAmount() const;
    int getDollars() const;
    int getCents() const;

    friend const Money operator+(const Money& amount1,const Money& amount2);

    void output();// const Money operator + (constMoney&yourAmount)const;
    void input();
    const Money operator++();

    int dollarsPart(double amount) const;
    int centsPart(double amount) const;
    int round(double number) const;

    const Money operator+(const Money& amount1);
    bool operator ==(const Money& amount1);
    const Money operator-(const Money& amount);

    //int purse.percent(int n);

};

Money::Money():dollars(0),cents(0)
{
    Money::Money(double amount): dollars(dollarsPart(amount)),cents(centsPart(amount))
    {
        Money::Money(int theDollars): dollars(theDollars),cents(0)
        {
            //Usescstdlib:
            Money::Money(int theDollars,int theCents)
            {
                if((theDollars< 0 && theCents> 0) ||(theDollars>0 && theCents<0))
                {
                    cout<<"Inconsistent money data.\n";
                    exit(1);
                }
                dollars=theDollars;
                cents=theCents;
            }
        }
    }
}

double Money::getAmount() const
{
    return(dollars+cents*0.01);
}

int Money::getDollars() const
{
    return dollars;
}

int Money::getCents() const
{
    return cents;
}

void Money::output()
{
    int absDollars=abs(dollars);
    int absCents=abs(cents);
    if(dollars<0 || cents<0)//accounts for dollars == 0 or cents==0
        cout << "$-";
    else
    {
        cout << '$';
        cout<<absDollars;//output absolute dollar
    //if statement is executed
        if(absCents>=10)
        {
           cout<<'.'<<absCents;
        }
        else
        {
            cout<<'.'<<'0'<<absCents;
        }//Usesiostreamandcstdlib:
    }
}

void Money::input()
{
    char dollarSign;
    cin>>dollarSign;//dollar sign is taken 
    if(dollarSign!='$')
    {
        cout<<"No dollar sign in Money input.\n";
        exit(1);
    }
    double amountAsDouble;
    cin>>amountAsDouble;
    dollars=dollarsPart(amountAsDouble);
    cents=centsPart(amountAsDouble);
}

int Money::dollarsPart(double amount) const
{
    return static_cast<int>(amount);
}

int Money::centsPart(double amount)const
{
    double doubleCents=amount*100;
    int Cents = (round(fabs(doubleCents)))%100;
    if(amount<0)
        Cents=-Cents;
    return Cents;
}

int Money::round(double number) const
{
    return static_cast<int>(floor(number+0.5));
}

const Money Money::operator+(const Money &yourAmount) const
{
    return Money(dollars+yourAmount.dollars, cents+yourAmount.cents);
}

const Money Money::operator++()
{

    ++cents;
    //calling object
    return Money(dollars,cents);
}

const Money operator +(const Money& amount1, const Money& amount2)
{

    int allCents1=amount1.cents+amount1.dollars*100;
    int allCents2 = amount2.getCents( ) + amount2.getDollars( )*100;
    int sumAllCents=allCents1+allCents2;
    int absAllCents = abs(sumAllCents); //Money can be negative.
    int finalDollars=absAllCents/100;
    int finalCents=absAllCents%100;
    if(sumAllCents<0)
    {
        finalDollars=-finalDollars;
        finalCents=-finalCents;
    }


    allCents1 = amount1.getCents( ) + amount1.getDollars( )*100;
    allCents2 = amount2.getCents( ) + amount2.getDollars( )*100;
    int diffAllCents=allCents1-allCents2;

    if(diffAllCents<0)
    {
        finalDollars=-finalDollars;
        finalCents=-finalCents;
    }
    return Money(finalDollars,finalCents);//if statement is true then returns final dollars
}

bool operator ==(const Money& amount1, const Money& amount2)
{
    return((amount1.getDollars()==amount2.getDollars())&&(amount1.getCents()==amount2.getCents()));
}

const Money operator-(const Money& amount)
{
    return Money(-amount.getDollars( ), -amount.getCents());
}


int main()
{
    Money yourAmount,myAmount(10,9);
    cout<<"Enter an amount of money: ";
    yourAmount.input();
    cout<<"Your amount is "<<yourAmount.output()<<endl;
    cout<<endl;
    cout<<"My amount is "<<myAmount.output() << endl;
    cout<< " " << endl;
    ++myAmount;
    cout<<"++my Amount is";
    myAmount.output();
    cout<< " " << endl;
    if(yourAmount==myAmount)
        cout<<"We have the same amounts.\n";
    else
        cout<<"One of us is richer.\n";

    Money ourAmount = yourAmount+myAmount;
    yourAmount.output( );
    cout<<"+";
    myAmount.output();

    cout<<"equals";
    ourAmount.output();
    cout<<endl;
    Money diffAmount;
    diffAmount =yourAmount-myAmount;
    yourAmount.output();
    cout<<"-";
    myAmount.output();
    cout<<"equals";
    diffAmount.output();
    cout<< " " << endl;
    cout<<"a percentage of the money%10 ";

    myAmount.output();
    purse.percent(10);
    return 0;
}

Sample output:

Enter an amount of money:$100.10

Your amount is $100.10

My amount is $10.01

Explanation:

In the above program, two objects are created of class Money. The first object invokes the default constructor. The second object takes two integer parameters and invokes the parameterized constructor. Using the first object, theinput() method is used to take user input for amount in dollars. Next, using the first object, theoutput() method is used to display the user-entered amount.

Create a third object of the class Money. Add the first and second objects of class Money and assign the sum to the third object.Using the second object, call the output() method. Next, using the first object, call the output() method. Lastly, using the third object, call the output() method.

Create a fourth object of the class Money. Subtract the first and second objects of class Money and assign the difference to the third object.Using the second object, call the output() method. Next, using the first object, call the output() method. Lastly, using the fourth object, call the output() method.

Want to see more full solutions like this?

Subscribe now to access step-by-step solutions to millions of textbook problems written by subject matter experts!
Students have asked these similar questions
Can you please help me with these questions? 1) When the left operand of a function that overloads an operator is NOT an object of the class, or a reference to such an object, the function must be declared as a ___ because it is a ___ function. friend, non-member member, void member, overloaded friend, member   2) To overload the greater than operator, you define an operator method whose name is ___.   > operator> >operator greaterThan   3) A(n) ___ ADT is used to store a homogeneous, one-dimensional, sequential, set of data items with a specific ordering. The ordering can be changed, as needed.     list     stack     queue      tree
Consider a class named Calculator with typical four specific functionalities i.e. addition, subtraction, multiplication, and division. Implement these functionalities as four functions with two parameters. It is also required to overload all these functions for int and double data types. In the main function, create an object of class Calculator and invoke its member functions while passing parameters of int and double type.
Write a simple function template for predicate function isEqualTo that compares its two arguments of the same type with the equality operator (==) and returns true if they are equal and false otherwise. Use this function template in a program that calls isEqualTo only with a variety of fundamental types. Now write a separate version of the program that calls isEqualTo with a user-defined class type, but does not overload the equality operator. What happens when you attempt to run this program? Now overload the equality operator (with the operator function) operator==. Now what happens when you attempt to run this program?

Additional Engineering Textbook Solutions

Find more solutions based on key concepts
Write code fragments to display a polygon connecting the following points: (20, 40), (30, 50), (40, 90), (90, 1...

Introduction to Java Programming and Data Structures, Comprehensive Version (11th Edition)

If a method in a subclass has the same signature as a method in the superclass, does the subclass method overlo...

Starting Out with Java: From Control Structures through Data Structures (4th Edition) (What's New in Computer Science)

What is pseudocode?

Starting Out With Visual Basic (7th Edition)

Knowledge Booster
Background pattern image
Computer Science
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
Text book image
C++ Programming: From Problem Analysis to Program...
Computer Science
ISBN:9781337102087
Author:D. S. Malik
Publisher:Cengage Learning
Text book image
C++ for Engineers and Scientists
Computer Science
ISBN:9781133187844
Author:Bronson, Gary J.
Publisher:Course Technology Ptr
Call By Value & Call By Reference in C; Author: Neso Academy;https://www.youtube.com/watch?v=HEiPxjVR8CU;License: Standard YouTube License, CC-BY