C++ need help, provide comments pls Define a class named Money that stores a mon
ID: 3692210 • Letter: C
Question
C++ need help, provide comments pls
Define a class named Money that stores a monetary amount. The class should have two private integer variables, one to store the number of dollars and another to store the number of cents. Include a default constructor that initializes the amount to $0.00 and an overloaded parameterized constructor to initialize any non-zero amount.
Add accessor member functions: getDollars( ) getCents( )
Add mutator member functions: setDollars( )setCents( )
Add a condition to your setCents() mutator that will update the dollars member if the cents input argument is 100 or larger.
Add another member function named getAmount() that returns the monetary amount as a single double number.
Write a main() program that tests all of your member functions with at least two different Money objects
your code should be able to print the following output
Testing Money Class:
Money object1 has the amount $13.35 <- printed with both getDollars() and getCents()
Money object2 has the amount $120.03 <- printed with just getAmount()
Explanation / Answer
main.cpp
#include <iostream>
#include "money.h"
using namespace std;
int main()
{
Money moneyOne, moneyTwo;
unsigned int dollars, cents;
/*Prompting the user to enter a monetary value.*/
cout << "This serves as a test program for the Money class." << endl;
cout << "Monetary Value One" << endl
<< "Please enter the number of dollars: ";
cin >> dollars;
cout << "Please enter the number of cents: ";
cin >> cents;
/*Entering collected data into a Money instance.*/
moneyOne.setDollars(dollars);
moneyOne.setCents(cents);
/*Spitting out dollars and cents to confirm that
our setters and getters are functioning appropriately.*/
cout << "You entered " << moneyOne.getDollars() << " dollars and "
<< moneyOne.getCents() << " cents." << endl;
cout << "Monetary Value Two" << endl
<< "Please enter the number of dollars: ";
cin >> dollars;
cout << "Please enter the number of cents: ";
cin >> cents;
/*Entering collected data into a Money instance.*/
moneyTwo.setDollars(dollars);
moneyTwo.setCents(cents);
/*Spitting out dollars and cents to confirm that
our setters and getters are functioning appropriately.*/
cout << "You entered " << moneyTwo.getDollars() << " dollars and "
<< moneyTwo.getCents() << " cents." << endl;
/*Testing double getMoney() const by displaying full monetary values.*/
cout << "Monetary Value One: $" << moneyOne.getMoney() << endl;
cout << "Monetary Value Two: $" << moneyTwo.getMoney() << endl;
return 0;
}
Money.h
#ifndef MONEY_H
#define MONEY_H
class Money
{
private:
unsigned int dollars;
unsigned int cents;
public:
unsigned int getDollars() const;
void setDollars(unsigned int dollars);
unsigned int getCents() const;
void setCents(unsigned int cents);
double getMoney() const;
};
#endif
Money.cpp
#include "money.h"
/*Gets the number of dollars in this money amount.*/
unsigned int Money::getDollars() const
{
return dollars;
}
/*Sets the number of dollars for this money amount.*/
void Money::setDollars(unsigned int dollars)
{
this->dollars = dollars;
}
/*Gets the number of cents in this money amount.*/
unsigned int Money::getCents() const
{
return cents;
}
/*Sets the number of cents for this money amount.*/
void Money::setCents(unsigned int cents)
{
this->cents = cents;
}
/*Retrieves a total amount in dollars and cents.*/
double Money::getMoney() const
{
double monetaryTotal = 0.0;
monetaryTotal = dollars + ((double) cents/100);
return monetaryTotal;
}
sample output
This serves as a test program for the Money class.
Monetary Value One
Please enter the number of dollars: 13
Please enter the number of cents: 14
You entered 13 dollars and 14 cents.
Monetary Value Two
Please enter the number of dollars: 3
Please enter the number of cents: 2
You entered 3 dollars and 2 cents.
Monetary Value One: $13.14
Monetary Value Two: $3.02
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.