Question

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;

   cout << "Non-default constructor 2: ";
   m3.display();
   cout << endl;

   return 0;
}

/***********************
* File: money.cpp
***********************/

#include <iostream>
#include <iomanip>
#include <cstdlib>
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(abs(dollars));
   setCents(abs(cents));
   Money moneySet;
   moneySet.setDollars(dollars);
   moneySet.setCents(cents);
}

/*****************************************************************
* Function: display
* Purpose: Displays the value of the money object.
****************************************************************/
void Money :: display() const
{

   cout << "$" << getDollars() << ".";
   cout << setfill('0') << setw(2) << getCents();

}

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

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

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

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

/******************
* File: money.h
******************/

#ifndef MONEY_H
#define MONEY_H

class Money
{
private:
   int dollars;
   int cents;

public:
   void prompt();
   void display() const;
   int getDollars() const;
   int getCents() const;
   void setDollars(int dollar);
   void setCents(int cents);
};

#endif

  1. Add the following constructors:
    • Default - Set the values to 0.
    • Non-default that accepts 1 integer - Sets the dollar amount to that integer, sets the cents to 0.
    • Non-default that accepts 2 integers - Sets the dollar amount to the first, and the cents to the second.
  2. Don't forget to use your setters so that you will get any error checking they do for free.
  3. You should not change anything in main (or your functions from last time). All you need to do is add the contructors.
  4. Don't forget to add your information to the makefile before tar-ing, and submitting.

Sample Output

The following is an example of output for this program:

Dollar amount: 10

Cents amount: 23

Default constructor: $0.00

Non-default constructor 1: $10.00

Non-default constructor 2: $10.23

Another example is:

Dollar amount: 3

Cents amount: -2

Default constructor: $0.00

Non-default constructor 1: $3.00

Non-default constructor 2: $3.02

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

If you have any doubts, please give me comment...

money.h

#ifndef MONEY_H

#define MONEY_H

class Money {

private:

int dollars;

int cents;

public:

Money();

Money(int);

Money(int, int);

void prompt();

void display() const;

int getDollars() const;

int getCents() const;

void setDollars(int dollar);

void setCents(int cents);

};

#endif

money.cpp

#include <iostream>

#include <iomanip>

#include <cstdlib>

using namespace std;

#include "money.h"

Money::Money(){

dollars = 0;

cents = 0;

}

Money::Money(int d){

dollars = d;

cents = 0;

}

Money::Money(int d, int c){

dollars = d;

cents = c;

}

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

* 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(abs(dollars));

setCents(abs(cents));

Money moneySet;

moneySet.setDollars(dollars);

moneySet.setCents(cents);

}

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

* Function: display

* Purpose: Displays the value of the money object.

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

void Money::display() const {

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

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

}

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

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

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

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

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;

cout << "Non-default constructor 2: ";

m3.display();

cout << endl;

return 0;

}

Add a comment
Know the answer?
Add Answer to:
Here are the files I've made so far: /********************* * File: check05b.cpp *********************/ #include &lt...
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
  • 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...

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

  • Finish the class Matrix.h: class Matrix { public: Matrix(); //default constructor ~Matrix(); //destructor...

    Finish the class Matrix.h: class Matrix { public: Matrix(); //default constructor ~Matrix(); //destructor Matrix(const Matrix &); //copy constror Matrix(int row, int col); Matrix operator+(const Matrix &)const; //overload operator“+” Matrix& operator=(const Matrix &); //overload operator“=” Matrix transpose()const; //matrix transposition void display()const; //display the data private: int row; //row int col; //column int** mat; //to save the matrix }; //main.cpp #include <iostream> #include"Matrix.h" using namespace std; int main() { int row, col; cout << "input the row and the col for Matrix...

  • Finish the class Matrix.h: class Matrix { public: Matrix(); //default constructor ~Matrix(); //destructor Matrix(const Matrix &);...

    Finish the class Matrix.h: class Matrix { public: Matrix(); //default constructor ~Matrix(); //destructor Matrix(const Matrix &); //copy constror Matrix(int row, int col); Matrix operator+(const Matrix &)const; //overload operator“+” Matrix& operator=(const Matrix &); //overload operator“=” Matrix transpose()const; //matrix transposition void display()const; //display the data private: int row; //row int col; //column int** mat; //to save the matrix }; //main.cpp #include <iostream> #include"Matrix.h" using namespace std; int main() { int row, col; cout << "input the row and the col for Matrix...

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

  • CODES: main.cpp #include <iostream> #include <string> #include "ShoppingCart.h" using namespace std; char PrintMenu() { char answer;...

    CODES: main.cpp #include <iostream> #include <string> #include "ShoppingCart.h" using namespace std; char PrintMenu() { char answer; cout << "MENU" << endl; cout << "a - Add item to cart" << endl; cout << "d - Remove item from cart" << endl; cout << "c - Change item quantity" << endl; cout << "i - Output items' descriptions" << endl; cout << "o - Output shopping cart" << endl; cout << "q - Quit" << endl << endl; while (true) {...

  • Who could write the array.cpp file ?   //main.cpp #include "array.hpp" #include <iostream> int main() { int...

    Who could write the array.cpp file ?   //main.cpp #include "array.hpp" #include <iostream> int main() { int n; std::cin >> n; array a(n); for (int i = 0; i < n; i++) { std::cin >> a.data()[i]; } std::cout << "array size:" << a.max_size() << std::endl; std::cout << "array front:" << a.front() << std::endl; std::cout << "array back:" << a.back() << std::endl; int* data = a.data(); std::cout << "array elements using data:" << std::endl; for (int i = 0; i < n;...

  • Requirements: Finish all the functions which have been declared inside the hpp file. Details: st...

    Requirements: Finish all the functions which have been declared inside the hpp file. Details: string toString(void) const Return a visible list using '->' to show the linked relation which is a string like: 1->2->3->4->5->NULL void insert(int position, const int& data) Add an element at the given position: example0: 1->3->4->5->NULL instert(1, 2); 1->2->3->4->5->NULL example1: NULL insert(0, 1) 1->NULL void list::erase(int position) Erase the element at the given position 1->2->3->4->5->NULL erase(0) 2->3->4->5->NULL //main.cpp #include <iostream> #include <string> #include "SimpleList.hpp" using std::cin; using...

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

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

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