In C++: Create a multifile project following the examples from the textbook. The
ID: 3566985 • Letter: I
Question
In C++:
Create a multifile project following the examples from the textbook. There should be a file with the main function, a file with a class definition and a file with the functions of the class defined as well.
The class will define the data and functions of a coin box. For simplicity sake, we will simply keep up with the total of the coins in cents and not bother with any other aspect of the box. The variety of the coins, how many, the denominations, etc. will be ignored. Hopefully, it is obvious that this coin box cannot go into debt, so the values of the coins can be zero or any positive value, nothing negative.
The CoinBox class must have functions to add value to the box (like adding coins), subtract value from the box (like taking coins out), and examining the contents to determine what is there. We won't bother to deal with how we can take away 3 cents when the only coin inside is a quarter, for example and just assume that there is some exchange required in order to just remove 3 cents. We won't (as mentioned) bother to determine the actual coins necessary to have the reported value anyway, so we won't know what kinds of coins are there.
Be sure to include a default constructor to initialize the coin box value to zero.
The class will have one data member of type int, but keep in mind that this value can never be negative. How this translates to code is that the subtract function must verify that the requested quantity is allowed to be removed before that modification takes place. This function can fail (if we want to take away 20 cents when there is only 15 cents in the box, for example). Borrowing from last week's lab example, this subtract function should be boolean return for the same reason as the choose function was boolean return.
The add function will be a void function because there is no way for it to fail (let's assume the box has no size limitation). Of course, the function to determine the value must have an int return.
Make sure to test all the class functionality in the main function (create an object of CoinBox, exercise all the functions). Make sure to test both the success and fail operations of the subtract function.
Make sure none of the class functions do any kind of console input or output (cin and cout). The main function will be responsible for all output and if there is any, input from the user.
Explanation / Answer
/////////////////// main.cpp ////////////////////////
#include <iostream>
#include "CoinBox.h"
using namespace std;
int main()
{
CoinBox myBox = CoinBox();
cout << "Initial Value of Box: ";
cout << myBox.getValue() << endl;
// Adding 102 cents
myBox.add(102);
cout << "Value of Box after adding 102 cents: ";
cout << myBox.getValue() << endl;
// Trying to remove 105 cents from box and printing result of the action
cout << "Removed 105 cents? (1-Yes/0-No): ";
cout << myBox.subtract(105) << endl;
cout << "Value of Box after trying to remove 105 cents: ";
cout << myBox.getValue() << endl;
// Trying to remove 99 cents from box and printing result of the action
cout << "Removed 99 cents? (1-Yes/0-No): ";
cout << myBox.subtract(99) << endl;
cout << "Value of Box after trying to remove 99 cents: ";
cout << myBox.getValue() << endl;
return 0;
}
/////////////////// CoinBox.h ////////////////////////
class CoinBox
{
public:
CoinBox ();
void add(int);
bool subtract(int);
int getValue();
protected:
private:
int Value;
};
/////////////////// CoinBox.cpp ////////////////////////
#include "CoinBox.h"
CoinBox::CoinBox()
{
Value = 0;
}
int CoinBox::getValue(){
return Value;
}
void CoinBox::add(int donation){
Value = Value + donation;
}
bool CoinBox::subtract(int withdraw){
if(withdraw> getValue()){
return false;
}else{
Value = Value - withdraw;
return true;
}
}