Hey all,
Here I am again, most of my program written but I'm stuck with the last step.
The assignment is asking us novice programmers to add three functions (1. to subtract, 2. to multiply, 3. add a function DollarsAndCentsAre which will print out (using cout) the complete value of a money value) to a program using a class called MoneyType where we initialize two amounts of money and use those two in the functions.
There are three extra functions that were written for us that set dollars = to newDollars and cents = to newCents and then DollarsAre() and CentsAre() which return dollars and cents.
My problem lies with the DollarsAndCentsAre. I'm not quite sure what he's asking us to do here.. do you guys see it as putting the two together and then print out one value after the calcluations?
Here's the code:
// Program Money manipulates instances of class MoneyType.
class MoneyType {
public:
void Initialize(long, long);
long DollarsAre() const;
long CentsAre() const;
MoneyType Add(MoneyType) const;
MoneyType Sub(MoneyType) const;
MoneyType Multi(MoneyType) const;
MoneyType DollarsAndCentsAre(MoneyType)const;
private:
long dollars;
long cents;
};
//**********************************************************
#include <iostream>
using namespace std;
int main () {
char a_character;
MoneyType money1;
MoneyType money2;
MoneyType money3;
MoneyType money4;
MoneyType money5;
money1.Initialize(10, 59);
money2.Initialize(20, 70);
money3 = money1.Add(money2);
money4 = money1.Sub(money2);
money5 = money1.Multi(money2);
cout << "Added values are: " << "$" << money3.DollarsAre() << "." << money3.CentsAre() << endl;
cout << "Subtracted values are: " << "$" << money4.DollarsAre() << "." << money4.CentsAre() << endl;
cout << "Multiplied values are: " << "$" << money5.DollarsAre() << "." << money5.CentsAre() << endl;
cout << endl << "Hit any key and enter to exit: ";
cin >> a_character;
return 0;
}
//********************************************************
void MoneyType::Initialize(long newDollars, long newCents)
// Post: dollars is set to newDollars; cents is set to
// newCents.
{
dollars = newDollars;
cents = newCents;
}
//********************************************************
long MoneyType::DollarsAre() const
// Post: Class member dollars is returned.
{
return dollars;
}
//********************************************************
long MoneyType::CentsAre() const
// Post: Class member cents is returned.
{
return cents;
}
//********************************************************
MoneyType MoneyType::Add(MoneyType value) const
// Pre: Both operands have been initialized.
// Post: value + self is returned.
{
MoneyType result;
result.dollars = dollars + value.dollars + (cents + value.cents) / 100;
result.cents = (cents + value.cents) % 100;
return result;
}
MoneyType MoneyType::Sub(MoneyType value) const
//Pre: Both operatnds have been initialized
//post: value - self is returned
{
MoneyType result;
result.dollars = value.dollars - dollars;
result.cents = value.cents - cents;
if(result.cents < 0)
{
if (result.dollars > 0)
{
result.dollars--;
result.cents += 100;
}
}
return result;
}
MoneyType MoneyType::Multi(MoneyType value) const
//Pre: Both operands have been initialized
//Post: value * self is returned
{
MoneyType result;
result.dollars = value.dollars * dollars;
result.cents = (value.cents * cents) % 100;
return result;
}
MoneyType MoneyType::DollarsAndCentsAre(MoneyType value) const
{
//??????
}
Thanks for your help and time
iTsweetie