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
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
|
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 |
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
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:

=========================================
Output#2:

=====================Could you plz rate me
well.Thank You
Overview This checkpoint is intended to help you practice the syntax of operator overloading with member...
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 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 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; 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 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...
1. (40’) In myStack.cpp, implement the member functions of the class myStack, which is the class for integer stacks. 2. (20’) In stackTest.cpp, complete the implementation of function postfixTest(), which use an integer stack to evaluate post-fix expressions. For simplicity, you can assume the post-fix expression is input character by character (i.e., not an entire string), and each operand is a non-negative, single-digit integer (i.e., 0,1,…,9). However, you are supposed to detect invalid/ illegal post-fix expression input, e.g., “4 5...
C++ please! OVERVIEW This homework builds on HW4. We will modify the classes to do a few new things We will add copy constructors to the Antique and Merchant classes. We will overload the == operator for Antique and Merchant classes. We will overload the + operator for the antique class. We will add a new class the "MerchantGuild." Please use the provided templates for the Antique and Merchant classes, as we simplified their functionality for this homework. Also, you...
I need assistance with the C++ code below to remove the "this" pointers while maintaining the code's current output and functionality. Also, there should be a private class in the header file, but I'm not sure what I should include there. Any help would be greatly appreciated! ***main.cpp driver file*** #include <iostream> #include "vector.h" using namespace std; int main() { vectorInfo v1(7, 6); vectorInfo v2(5, 4); v1.set(3, 2); cout << "v1 : "; v1.print(); cout << "v2 :...
C++ Class and Operator Overloading part 1 The source file contains a C++ program that declares and initializes an array of Circles. The program is using a for loop statement to find the largest circle in the array and display it on the output screen. However, the program is currently not working. The problem is the program uses logical operator ' > ' for comparison of circles and it is not working unless we overload such operator for the class...
C++ I am using visual studio for it. Make a copy of the Rational class you created in the previous Lab. Modify the class. Replace all your mathematical, input, and output functions with overloaded operators. Overload the following 12 operators: + - * / < > = = ! = <= >= >> << In order to test your class in your main function, prompt the user for a numerator and a denominator for your first object. Repeat the prompt...