Пример #1
0
// "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
0
bool operator ==(const Money& amount1, const Money& amount2){
	return ((amount1.getDollars() == amount2.getDollars())
		&& (amount1.getCents() == amount2.getCents()));
}