class中函数重载的long与int的问题
定义了一个class Money,如下所示,
class Money
{
public:
Money(long dollars, int cents);
// Initializes the object to its value represents an amount with the
// dollars and cents given by the arguments. If the amount is negative,
// then both dollars and cents must be negative.
Money(long dollars);
// Initializes the object so its value represents $dollars.00.
Money(double dollars);
// Initializes the object so its value represents 100*dollars cents.
Money();
// Initializes the object so its value represents $0.00.
double get_value() const;
// Precondition: The calling object has been given a value.
// Returns the amount of money recorded in the data of the calling object.
void input(istream& ins);
// Precondition: If ins is a file input stream, then ins has already been
// connected to a file. An amount of money, including a dollar sign, has been
// entered in the input stream ins. Notation for negative amounts is -$100.00.
// Postcondition: The value of the calling object has been set to
// the amount of money read from the input stream ins.
void output(ostream& outs) const;
// Precondition: If ins is a file input stream, then ins has already been
// connected to a file.
// Postcondition: A dollar sign and the amount of money recorded
// in the calling object have been sent to the output stream outs.
private:
long all_cents;
};
Money::Money(long dollars):all_cents(dollars * 100)
{
// Body intentionally blank.
}
Money::Money(double dollars)
// Initializes the object so its value represents 100*dollars cents.
{
all_cents=static_cast<long>(100*dollars);
}
Money my_amount(10L);
//or
Money my_amount(10.0);