Help with Class Member Functions
I am attempting to create a class and subsequent functions simulating a purse.
Here are my instructions :
-Declare Purse Class
-Include 4 private data members
+int pennies, int nickels, int dimes, & int quarters
-Include 4 public functions
+function insert(int p, int n, int d, int q) to initialize pennies, nickels, dimes, & quarters
+function dollars() to return the dollar amount
+function remove(int p, int n, int d, int q) to subtract pennies, nickels, dimes & quarters
+function display() returns a new String with remaining pennies, nickels, dimes & quarters
-Class should include a test driver main()
+Should declare the Purse object p with (2, 3, 0, 1) and invokes display() method to print content of the purse
Here is the code I have, but I am receiving arbitrary values when the program outputs values.
Code:
//Purse
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
class Purse
{ private:
int pennies;
int nickels;
int dimes;
int quarters;
public:
void insert( int, int, int, int );
void remove( int, int, int, int );
void dollars( int, int, int, int );
void display();
};
//Purse::Purse(
//{
// insert( int p, n, d, q);
//}
void Purse::insert( int p, int n, int d, int q )
{
pennies += p;
nickels += n;
dimes += d;
quarters += q;
}
void Purse::remove( int p, int n, int d, int q )
{
pennies -= p;
nickels -= n;
dimes -= d;
quarters -= q;
}
//void Purse::dollars( int, int, int, int )
//{ float x = pennies + 5*nickels + 10*dimes + 25*quarters;
// x =(float)x/100;
// cout << "\nThe current amount inside of the purse in dollars, is : $" << x << " Dollars.\n";
//}
void Purse::display()
{
cout << "\nCurrent count of purse: " << pennies << " Pennies, " << nickels << " Nickels, " << dimes << " Dimes, and " << quarters << " Quarters.\n" << endl;
}
int main()
{
Purse objectp; //Declare object
objectp.insert( 0, 0, 0, 0);
//objectp.insert( 2, 3, 0, 1 );
objectp.display();
return 0;
}
I have tried messing with the syntax with a few of the functions with no luck. Even inputting all 0's into the insert function returns me with very high values.
What can I do to fix this? Thanks in advance.