Absolute C++
Absolute C++
6th Edition
ISBN: 9780133970784
Author: Walter Savitch, Kenrick Mock
Publisher: Addison-Wesley
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
Rewrite the calculator program using a class called calculator. Your program will keep asking the user if they want to perform more calculations or quit and will have a function displayMenu to display the different functions e.g .(1 - addition, 2- subtraction, 3- multiplication, 4- division)   Your program must have appropriate constructors and member functions to initialize, access, and manipulate the data members as well as : A member function to perform the addition and return the result A member function to perform the subtraction and return the result A member function to perform the multiplication and return the result A member function to perform the division and return the result
SUBJECT: OOPPROGRAMMING LANGUAGE: C++ ALSO ADD SCREENSHOTS OF OUTPUT.   Write a class Distance to measure distance in meters and kilometers. The class should have appropriate constructors for initializing members to 0 as well as user provided values. The class should have display function to display the data members on screen.   Write another class Time to measure time in hours and minutes. The class should have appropriate constructors for initializing members to 0 as well as user provided values. The class should have display function to display the data members on screen.   Write another class which has appropriate functions for taking objects of the Distance class and Time class to store time and distance in a file.   Make the data members and functions in your program const where applicable
(After reading the instructions given in the pictures attached, read the following)   Class membersdoctorType Class must contain at least these functionsdoctorType(string first, string last, string spl); //First Name, Last Name, Specialty               void print() const; //Formatted Display First Name, Last Name, Specialtyvoid setSpeciality(string); //Set the doctor’s Specialtystring getSpeciality(); //Return the doctor’s SpecialtypatientType Class must contain at least these functionsvoid setInfo(string id, string fName, string lName,int bDay, int bMth, int bYear,string docFrName, string docLaName, string docSpl,int admDay, int admMth, int admYear,int disChDay, int disChMth, int disChYear);void setID(string);string getID();void setBirthDate(int dy, int mo, int yr);int getBirthDay();int getBirthMonth();int getBirthYear();void setDoctorName(string fName, string lName);void setDoctorSpl(string);string getDoctorFName();string getDoctorLName();string getDoctorSpl();void…

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
Call By Value & Call By Reference in C; Author: Neso Academy;https://www.youtube.com/watch?v=HEiPxjVR8CU;License: Standard YouTube License, CC-BY