Question

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

  1. 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 the money class and practice operators

****************************************************************/

int main()

{

   Money account1;

   Money account2;

   // Get the input from the user

   account1.prompt();

   account2.prompt();

   cout << endl;

   cout << "The original values are: ";

   account1.display();

   cout << " and ";

   account2.display();

   cout << endl;

   // TODO: Add code here to add account2 onto account1 using the += operator

   cout << "After doing account1 += account2, the value of account1 is: ";

   account1.display();

   cout << endl;

   Money account3;

   // TODO: Add code here to add account1 and account2 together

   // and put the result in account3

   cout << "From account1 + account2, the value of account3 is: ";

   account3.display();

   cout << endl;

   // TODO: Add code here to apply the ++ pre-increment operator to account1;

   cout << "After ++account1, the value of account1 is: ";

   account1.display();

   cout << endl;

   return 0;

}

###############################################################

# Program:

#     Checkpoint 12b, Member Operators

# Summary:

#     Summaries are not necessary for checkpoint assignments.

###############################################################

a.out : money.o check12b.o

            g++ money.o check12b.o

money.o : money.h money.cpp

            g++ -c money.cpp

check12b.o : money.h check12b.cpp

            g++ -c check12b.cpp

clean :

            rm *.o *.out

/***********************

* File: money.cpp

***********************/

#include

#include

using namespace std;

#include "money.h"

/*****************************************************************

* Function: prompt

* Purpose: Asks the user for values for dollars and cents

*   and stores them.

****************************************************************/

void Money :: prompt()

{

   int dollars;

   int cents;

   cout << "Dollars: ";

   cin >> dollars;

   cout << "Cents: ";

   cin >> cents;

   setDollars(dollars);

   setCents(cents);

}

/*****************************************************************

* Function: display

* Purpose: Displays the value of the money object.

****************************************************************/

void Money :: display() const

{

   cout << "$" << getDollars() << ".";

   cout << setfill('0') << setw(2) << getCents();

}

/*****************************************************************

* Function: handleOverflow

* Purpose: Checks if cents is greater than 100, and if so, rolls

*   that amount over to dollars.

****************************************************************/

void Money :: handleOverflow()

{

   if (cents >= 100)

{

      dollars += cents / 100;

      cents = cents % 100;

   }

}

// Put the bodies of your member functions here!

/******************

* File: money.h

******************/

#ifndef MONEY_H

#define MONEY_H

#include

/******************************************************

* Class: Money

* Description: Holds a value of dollars and cents.

******************************************************/

class Money

{

private:

   int dollars;

   int cents;

public:

   /************************

    * Constructors

    ************************/

   Money()

   {

      setDollars(0);

      setCents(0);

   }

   Money(int dollars, int cents)

   {

      setDollars(dollars);

      setCents(cents);

   }

   /************************

    * Getters and Setters

    ************************/

   int getDollars() const { return dollars; }

   int getCents() const { return cents; }

  

   // These could be done in a smarter way to add cents to dollars if more than 100 etc.

   // but we're trying to keep it simple for this assignment...

   void setDollars(int dollars) { this->dollars = dollars; }

   void setCents(int cents) { this->cents = cents; }

   /************************

    * Other public methods

    ************************/

   void prompt();

   void display() const;

   /************************

    * Member operators

    ************************/

   // TODO: Put your protoypes here!

   /*************************

    * Private helper methods

    *************************/

   void handleOverflow();

};

#endif

  1. This class has the following class diagram:

Money

-dollars : int

-cents : int

+Money()

+Money(dollars, cents)

+getDollars() : int

+getCents() : int

+setDollars(int) : void

+setCents(int) : void

+prompt() : void

+display() : void

-handleOverflow() : void

  1. You need to overload the following operators as member functions (in other words methods contained inside the class). This means that you should put the prototypes in your class in the Money.h file, and the bodies of these functions should be in the Money.cpp file.
  2. Assuming two Money objects, m1 and m2, implement the following:
    • m1 + m2 (Addition) - Add the dollars and cents of two money objects and return a new one.
    • m1 += m2 (Add onto) - Add the dollars and cents of the item on the right (the parameter) to the item of the left (the current object) and return a reference to it.
    • ++m1 (Pre-increment) - Add one cent to the provided money object (and return a reference to the current object).
  3. Note that if the value of cents ever exceeds 100, that amount should roll over into the dollars. There is a function handleOverflow() provided for you that contains this logic. You should call it where appropriate.
  4. Fill in the main function (in check12b.cpp) to use your operators as noted.

Sample Output

The following is an example of output for this program:

Dollars: 10

Cents: 67

Dollars: 11

Cents: 67

The original values are: $10.67 and $11.67

After doing account1 += account2, the value of account1 is: $22.34 From account1 + account2, the value of account3 is: $34.01 After ++account1, the value of account1 is: $22.35

Another example would be:

Dollars: 10

Cents: 85

Dollars: 1

Cents: 14

The original values are: $10.85 and $1.14

After doing account1 += account2, the value of account1 is: $11.99 From account1 + account2, the value of account3 is: $13.13 After ++account1, the value of account1 is: $12.00

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

Note: Could you plz go through this code and let me know if u need any changes in this.Thank You
=================================

// Money.h

#ifndef MONEY_H

#define MONEY_H

/******************************************************

* Class: Money

* Description: Holds a value of dollars and cents.

******************************************************/

class Money

{

private:

int dollars;

int cents;

public:

/************************

* Constructors

************************/

Money()

{

setDollars(0);

setCents(0);

}

Money(int dollars, int cents)

{

setDollars(dollars);

setCents(cents);

}

/************************

* Getters and Setters

************************/

int getDollars() const { return dollars; }

int getCents() const { return cents; }

  

// These could be done in a smarter way to add cents to dollars if more than 100 etc.

// but we're trying to keep it simple for this assignment...

void setDollars(int dollars) { this->dollars = dollars; }

void setCents(int cents) { this->cents = cents; }

/************************

* Other public methods

************************/

void prompt();

void display() const;

/************************

* Member operators

************************/

// TODO: Put your protoypes here!
Money operator + (const Money &);
Money operator += (const Money &right);
void operator++();

/*************************

* Private helper methods

*************************/

void handleOverflow();

};

#endif

=========================================

// Money.cpp

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

#include "money.h"

/*****************************************************************

* Function: prompt

* Purpose: Asks the user for values for dollars and cents

* and stores them.

****************************************************************/

void Money :: prompt()

{

int dollars;

int cents;

cout << "Dollars: ";

cin >> dollars;

cout << "Cents: ";

cin >> cents;

setDollars(dollars);

setCents(cents);

}

/*****************************************************************

* Function: display

* Purpose: Displays the value of the money object.

****************************************************************/

void Money :: display() const

{

cout << "$" << getDollars() << ".";

cout << setfill('0') << setw(2) << getCents();

}

/*****************************************************************

* Function: handleOverflow

* Purpose: Checks if cents is greater than 100, and if so, rolls

* that amount over to dollars.

****************************************************************/

void Money :: handleOverflow()

{

if (cents >= 100)

{

dollars += cents / 100;

cents = cents % 100;

}

}

// Put the bodies of your member functions here!
Money Money::operator + (const Money &right)
{
Money temp;
temp.dollars = dollars + right.dollars;
temp.cents = cents + right.cents;
temp.handleOverflow();
return temp;
}

Money Money::operator += (const Money &right)
{

this->dollars = dollars + right.dollars;

this->cents = cents + right.cents;
this->handleOverflow();
return *this;
}
// Preincrememnt operator overloading
void Money::operator++()
{
  
++(this->cents);
handleOverflow();
}


=======================================

// main.cpp

#include <iostream>
using namespace std;

#include "money.h"

/****************************************************************

* Function: main

* Purpose: Test the money class and practice operators

****************************************************************/

int main()

{

Money account1;

Money account2;

// Get the input from the user

account1.prompt();

account2.prompt();

cout << endl;

cout << "The original values are: ";

account1.display();

cout << " and ";

account2.display();

cout << endl;

// TODO: Add code here to add account2 onto account1 using the += operator
account1+=account2;
cout << "After doing account1 += account2, the value of account1 is: ";

account1.display();

cout << endl;

Money account3;

// TODO: Add code here to add account1 and account2 together
account3=account1+account2;
// and put the result in account3

cout << "From account1 + account2, the value of account3 is: ";

account3.display();

cout << endl;

// TODO: Add code here to apply the ++ pre-increment operator to account1;
++account1;
cout << "After ++account1, the value of account1 is: ";

account1.display();

cout << endl;

return 0;

}

=======================================

Output#1:

1./Money Dollars: 10 Cents: 67 Dollars: 11 Cents: 67 The original values are: $10.67 and $11.67 After doing account1 += accou

=========================================

Output#2:

./Money Dollars: 10 Cents: 85 Dollars: 1 Cents: 14 The original values are: $10.85 and $1.14 After doing account1 += account2

=====================Could you plz rate me well.Thank You

Add a comment
Know the answer?
Add Answer to:
Overview This checkpoint is intended to help you practice the syntax of operator overloading with member...
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;   ...

  • C++ Redo Practice program 1 from chapter 11, but this time define the Money ADT class...

    C++ Redo Practice program 1 from chapter 11, but this time define the Money ADT class in separate files for the interface and implementation so that the implementation can be compiled separately from any application program. Submit three files: YourLastNameFirstNameInitialMoney.cpp - This file contains only the Money class implementation YourLastNameFirstNameInitialProj4.cpp - This file contains only the main function to use/test your money class YourLastNameFirstNameInitialMoney.h - This file contains the header file information.   Optional bonus (10%): Do some research on make...

  • MAIN OBJECTIVE       Develop a polymorphic banking program using the Account hierarchy created (below). C++ Capture...

    MAIN OBJECTIVE       Develop a polymorphic banking program using the Account hierarchy created (below). C++ Capture an output. polymorphism. Write an application that creates a vector of Account pointers to two SavingsAccount and two CheckingAccountobjects. For each Account in the vector, allow the user to specify an amount of money to withdraw from the Account using member function debit and an amount of money to deposit into the Account using member function credit. As you process each Account, determine its...

  • // 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...

  • Project 1 - Wallet Following the class diagram shown below, create the class Wallet. A Wallet...

    Project 1 - Wallet Following the class diagram shown below, create the class Wallet. A Wallet represents a carrier of bills and coins. You use your wallet to pay for things, which reduces the balance held inside. When you visit an ATM, you can fill it back up with cash again. A sample driver for this class is shown below. You should probably make a more thorough driver to test your class better. I will have my own driver which...

  • In an effort to develop in-depth knowledge of base class, derived class, and operator overloading, we...

    In an effort to develop in-depth knowledge of base class, derived class, and operator overloading, we will expand on our in-class exercise of developing the rectangleType class and overload the following operators: +, -, *, ++, --, ==, !=, >>, and <<. Your program should contain the following features: a. Create a base class named rectangleType with following private data members: • length with double type • width with double type b. rectangleType class should contain the following functions: •...

  • Project 1 - Operator Overloading (FlashDrive 2.0) In C++, many of the keyboard symbols that are...

    Project 1 - Operator Overloading (FlashDrive 2.0) In C++, many of the keyboard symbols that are used between two variables can be given a new meaning. This feature is called operator overloading and it is one of the most popular C++ features. Any class can choose to provide a new meaning to a keyboard symbol. Not every keyboard letter is re-definable in this way, but many of the ones we have encountered so far are like +, -, *, /,...

  • For the LinkedList class, create a getter and setter for the private member 'name', constructing your...

    For the LinkedList class, create a getter and setter for the private member 'name', constructing your definitions based upon the following declarations respectively: std::string get_name() const; and void set_name(std::string); In the Main.cpp file, let's test your getter and setter for the LinkedLIst private member 'name'. In the main function, add the following lines of code: cout << ll.get_name() << endl; ll.make_test_list(); ll.set_name("My List"); cout << ll.get_name() << endl; Output should be: Test List My List Compile and run your code;...

  • 13.21 Lab: Rational class This question has been asked here before, but every answer I have...

    13.21 Lab: Rational class This question has been asked here before, but every answer I have tested did not work, and I don't understand why, so I'm not able to understand how to do it correctly. I need to build the Rational.cpp file that will work with the main.cpp and Rational.h files as they are written. Rational Numbers It may come as a bit of a surprise when the C++ floating-point types (float, double), fail to capture a particular value...

  • Write a C++ Program. You have a following class as a header file (dayType.h) and main()....

    Write a C++ Program. You have a following class as a header file (dayType.h) and main(). #ifndef H_dayType #define H_dayType #include <string> using namespace std; class dayType { public:     static string weekDays[7];     void print() const;     string nextDay() const;     string prevDay() const;     void addDay(int nDays);     void setDay(string d);     string getDay() const;     dayType();     dayType(string d); private:     string weekDay; }; #endif /* // Name: Your Name // ID: Your ID */ #include <iostream>...

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