#include <iostream>
using namespace std;
class Money
{
public:
Money();
Money(int, int);
void setDollars(int d);
void setCents(int c);
int getDollars() const;
int getCents() const;
double getAmount(int, int);
private:
int dollars;
int cents;
};
Money::Money()
{
dollars = 0;
cents = 0;
}
Money::Money(int d, int c)
{
dollars = d;
cents = c;
}
void Money::setDollars(int d)
{
dollars = d;
}
void Money::setCents(int c)
{
if (c > 100)
{
c = c % 100;
dollars = dollars + (c / 100);
}
//update the dollars member if the cents input argument is 100 or larger.
cents = c;
}
int Money::getDollars() const
{
return dollars;
}
int Money::getCents() const
{
return cents;
}
double Money::getAmount(int d, int c)
{
return dollars + (cents / 100);
}
int main()
{
int dlr;
int cts;
//double amt;
cout << "Please input amount of dollars: ";
cin >> dlr;
cout << "Please input amount of cents: ";
cin >> cts;
Money money0(dlr, cts);
cout << "The money object1 has amount: " << money0.getDollars() << "." << money0.getCents() << endl;
cout << "The money object2 has amount: " << money0.getAmount() << endl;
//I try to call the functions to tell user how much is it
//use both ways to tell user how much is it(1.getDollars+getCents, 2. getAmount)
return 0;
system("pause");
}
当我尝试在main中调用get函数时,错误会显示出来。如何正确调用这些功能? 这是我的任务: 定义一个名为Money的类,用于存储货币金额。该类应该有两个私有整数变量,一个用于存储美元数,另一个用于存储美分数。包括一个默认构造函数,用于将金额初始化为$ 0.00,以及一个重载参数化构造函数,用于初始化任何非零金额(如下面的示例程序中所示)。