.cpp file guidance

I have this program and I did my .h file but I am having trouble with the .cpp file since they don't involve setters.


Create a class called DollarsCents for performing arithmetic with money. A driver program is given to you to test your class. Use integer variables to represent the private data members of the class:
int cents;
int dollars;

Here cents is the amount (number) of cents and dollars is the amount (number) of dollars. The private data members are set through the two-parameter constructor that has default values for these data members. This class has no setters. However, the values of cents and dollars can be updated via an assignment from one DollarsCents object to another. The cents and dollars data members should be reduced and stored in simplified form (where 0 <= cents < 99) after their values are set within the constructor or after some arithmetic operations. You must code a simplify function.

Provide member functions that perform each of the tasks given in the table below. Each of them should have public access, except for the simplify utility function, which should be a private member function:

return value: none
function: DollarsCents
parameters: int numCents=0
Task: Two parameter constructor with default values for dollasrs and cents provided in the prototype. It should set the dollars and cents data members in simplified form

return value: int
function: getDollars
parameters: none
task: returns the amount of dollars

return value: int
function: getCents
parameters: none
task: returns the amount of cents

return value: DollarsCents
function: Subtract
parameters: DollarCents dc
task: subtract another DollarsCents object from this one and return the result in a third DollarsCents object

return value: DollarsCents
function: add
parameters: DollarCents dc
task: Add another DollarsCents object to this one and return the result in a third DollarsCents object

return value: DollarsCents
function: multiply
parameters: int const num
task: Multiply this DollarsCents object by a whole no. and return the result in another DollarsCents object

return value: DollarsCents
function: divide
parameters: int const num
task: divide this DollarsCents object by a whole no. and return the result in a second DollarsCents object
discard any leftover cents

return value: void
function: simplify
parameters: none
task: utility function that simplifies the data members dollars and cents so that 0 <= cents <100

return value: void
function: print
parameters: none
task: Displays the DollarsCents object as a double

the main function is below:


#include <iostream>
#include <iomanip>
#include <cstdlib>
#include "DollarsCents.h" // include definition of class DollarsCents
using namespace std;

int main()
{
cout << fixed << setprecision(2);

int const divideBy = 2;
int const multiplyBy = 3;

// Create three DollarsCents objects

DollarsCents dc;

int dollars1 = 2;
int cents1 = 2121;
DollarsCents dc1 (dollars1, cents1);

int dollars2 = 3;
int cents2 = 333;
DollarsCents dc2 (dollars2, cents2);

// Addition

cout << “Adding “ << dollars2 << “ dollars and “ << cents2
<< “ cents” << endl;
cout << “To “ << dollars1 << “ dollars and “ << cents1
<< “ cents :“ << endl << endl;

dc1.print(); // prints DollarsCents object dc1
cout << " + ";
dc2.print(); // prints DollarsCents object dc2

dc = dc1.add(dc2); // adds object dc2 to dc21
cout << " = "; // places results in DollarsCents dc
dc.print(); // prints DollarsCents object dc
cout << endl << endl;

// Subtraction

cout << “Subtracting “ << dollars2 << “ dollars and “ << cents2
<< “ cents” << endl;
cout << “From “ << dollars1 << “ dollars and “ << cents1
<< “ cents:“ << endl << endl;

dc1.print(); // prints DollarsCents object dc1
cout << " - ";
dc2.print(); // prints DollarsCents object dc2
dc = dc1.subtract(dc2); // subtracts object dc2 from dc1
cout << " = "; // places results in DollarsCents dc
dc.print(); // prints DollarsCents object dc
cout << endl << endl;

// Multiplication by a constant

cout << “Multiplying “ << dollars1 << “ dollars and “ << cents1
<< “ cents” << “ by “ << multiplyBy << “:“ << endl << endl;

dc1.print(); // prints DollarsCents object dc1
cout << " * " << multiplyBy;

dc = dc1.multiply(multiplyBy); // multiplies object dc1 by multiplyBy
cout << " = "; // places results in DollarsCents dc
dc.print(); // prints DollarsCents object dc
cout << endl << endl;

// Division by a constant

cout << “Dividing “ << dollars1 << “ dollars and “ << cents1
<< “ cents” << “ by “ << divideBy << “:“ << endl << endl;

dc1.print(); // prints DollarsCents object dc1
cout << " / " << divideBy;

dc = dc1.divide(divideBy); // divides object dc1 in divideBy
cout << " = "; // places results in DollarsCents dc
dc.print(); // prints DollarsCents object dc
cout << endl << endl;

system("pause");
return 0;
} // end main



//.H file as well

#include <iostream>
#include <iomanip>
#include <cstdlib>
#ifndef DollarsCents_H
#define DollarsCents_H

using namespace std;


class DollarsCents
{

public:

DollarsCents(int numCents = 0, int numDollars = 0);
int getDollars;
int getCents;
DollarsCents add(DollarCents, dc);
DollarsCents subtract(DollarCents, dc);
DollarsCents multiply(int const num);
DollarsCents divide (int const num);
void print






private:
int dollars;
int cents;
void simplify


};





#endif




//.cpp file so far:

#include "DollarsCents.h"
#include <iostream>
using namespace std;



DollarsCents::DollarsCents(int numCents = 0, int numDollars = 0)
{
dollars = nd;
numcents = nc;
}
Any hints on this?
Sure, a hint:
1
2
3
4
5
addDollars(DollarsCents dc) // no comma here
{
    int sumDollars = this.dollars + dc.dollars;
    // ...
}
Last edited on
thanks jockx this has been helpful
When the teacher gives you such nice guidelines, you should use them in your file:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include "DollarsCents.h"
/*******************************************************************************
* return value: none
* function: DollarsCents
* parameters: int numCents=0
* Task: Two parameter constructor with default values for dollasrs and cents
*       provided in the prototype.
* It should set the dollars and cents data members in simplified form
*******************************************************************************/
DollarsCents::DollarsCents( int numCents )
{
  cents = numCents;
  dollars = 0;
  simplify();
}

/*******************************************************************************
* return value: int
* function: getDollars
* parameters: none
* task: returns the amount of dollars
*******************************************************************************/

/*******************************************************************************
* return value: int
* function: getCents
* parameters: none
* task: returns the amount of cents
*******************************************************************************/

/*******************************************************************************
* return value: DollarsCents
* function: Subtract
* parameters: DollarCents dc
* task: subtract another DollarsCents object from this one
*       and return the result in a third DollarsCents object
*******************************************************************************/

/*******************************************************************************
* return value: DollarsCents
* function: add
* parameters: DollarCents dc
* task: Add another DollarsCents object to this one and
*       return the result in a third DollarsCents object
*******************************************************************************/

/*******************************************************************************
* return value: DollarsCents
* function: multiply
* parameters: int const num
* task: Multiply this DollarsCents object by a whole no.
*       and return the result in another DollarsCents object
*******************************************************************************/

/*******************************************************************************
* return value: DollarsCents
* function: divide
* parameters: int const num
* task: divide this DollarsCents object by a whole no.
*       and return the result in a second DollarsCents object
* Discard any leftover cents
*******************************************************************************/

/*******************************************************************************
* return value: void
* function: simplify
* parameters: none
* task: utility function that simplifies the data members dollars and cents
*       so that 0 <= cents <100
*******************************************************************************/
void simplify( void )
{
  // do logic
}

/*******************************************************************************
* return value: void
* function: print
* parameters: none
* task: Displays the DollarsCents object as a double
*******************************************************************************/


From there you could probably try compiling and check out the errors you will get, undefined functions for starters. Define all of the methods, do what you have to do to make it compile. Then add the logic to the functions. I did the constructor for you
Last edited on
Topic archived. No new replies allowed.