本文整理汇总了C++中Money::getDollars方法的典型用法代码示例。如果您正苦于以下问题:C++ Money::getDollars方法的具体用法?C++ Money::getDollars怎么用?C++ Money::getDollars使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Money
的用法示例。
在下文中一共展示了Money::getDollars方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: Money
// "const" can protect the sum Money object from the unwanted change
const Money operator +(const Money& amount1, const Money& amount2){
int allCents1 = amount1.getCents() + amount1.getDollars() * 100;
int allCents2 = amount2.getCents() + amount2.getDollars() * 100;
int summAllCents = allCents1 + allCents2;
int absAllCents = abs(summAllCents); // Money can be negative
int finalDollars = absAllCents / 100;
int finalCents = absAllCents % 100;
if (sumAllCents < 0){
finalDollars = -finalDollars;
finalCents = -finalCents;
}
// constructor is not a "void" function
// A constructor can return an object
// Called an "anonymous object"
return Money(finalDollars, finalCents);
}
示例2:
bool operator ==(const Money& amount1, const Money& amount2){
return ((amount1.getDollars() == amount2.getDollars())
&& (amount1.getCents() == amount2.getCents()));
}