I need to write a member function in class Money that adds two Money objects together and returns the result. My instructor told me this function does not have two parameters.
Any idea on how to do write this function?
Any advice would be greatly appreciated
#include <iostream>
usingnamespace std;
/*class definition*/
class Money {
private:
int dollars;
int cents;
public:
Money ();
Money (int d, int c);
void print ();
void set (int d, int c);
int add ();
};
int main() {
Money price;
price.set (9, 99); // sets the value to $9.99
price.print(); // displays "$9.99" on console
Money tax;
tax.set (0, 87); // sets the value to $0.87
tax.print(); // displays "$0.87" on console
}
Money::Money () { //Default Constructor
dollars=0;
cents=0;
}
Money::Money (int d, int c) { //Parameterized constructor
dollars = d;
cents = c;
}
void Money::print () {
/*Display the money on the screen */
cout<<"$"<<dollars<<"."<<cents<<endl;
}
void Money::set (int d, int c) {
/*sets the value for the object to "d" dollars and "c" cents.*/
dollars=d;
cents=c;
}
Thanks @dhayden and @motobus for the reply.
Now, I've made a progress on my program and I think it's getting really close to obtain the intended result, but somehow my addition produces the wrong result.
9.99 + 0.87 is equal to 10.86, but in this program I got 10.86. Anyone knows why?