60.6k views
16 votes
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. Add accessor and mutator functions to read and set both member variables. Add another function that returns the monetary amount as a double. Write a program that tests all of your functions with at least two different Money objects.

User Ray Foss
by
3.8k points

1 Answer

11 votes

Answer:

#include<iostream>

using namespace std;

class Money{

private:

int dollars;

int cents;

public:

Money(int d=0, int c=0){

setDollar(d);

setCent(c);

}

void setDollar(int d){

dollars = d;

}

void setCent(int c){

cents = c

}

int getDollar() {

return dollars;

}

int getCent() {

return cents;

}

double getMoney(){

return (dollars + (cents/100));

}

};

// testing the program.

int main(){

Money Acc1(3,50);

cout << M.getMoney() << endl;

Money Acc2;

Acc2.setDollars(20);

Acc2.setCents(30);

cout <<"$" << Acc2.getDollars() << "." << Acc2.getCents() << endl;

return 0;

}

Step-by-step explanation:

The C++ source code defines the Money class and its methods, the class is used in the main function as a test to create the instances of the money class Acc1 and Acc2.

The object Acc2 is mutated and displayed with the "setDollar and setCent" and "getDollar and getCent" methods respectively.

User Michael Blanza
by
4.0k points