Lec6. Operator Overload
Lec6. Operator Overload
Operator
Overloading, Friends,
and References
Example Declaration:
const Money operator +(const Money& amount1, const Money& amount2);
– Overloads + for operands of type Money
– Uses constant reference parameters for efficiency
– Returned value is type Money that is the addition of "Money" objects
8-2
#include <iostream>
#include <cstdlib>
#include <cmath>
using namespace std;
//Class for amounts of money in U.S. currency
class Money {
public:
Money( );
Money( double amount);
Money( int theDollars, int theCents);
Money( int theDollars);
double getAmount( ) const {return (dollars + cents*0.01);}
int getDollars( ) const {return dollars;}
int getCents( ) const {return cents;}
void input( ); //Reads the dollar sign as well as the amount number.
void output( ) const;
private:
int dollars; //A negative amount is represented as negative dollars and
int cents; //negative cents. Negative $4.50 is represented as -4 and -50.
int dollarsPart( double amount) const {return static_cast<int>(amount); }
int centsPart( double amount) const;
int round( double number) const{return static_cast<int>(floor(number + 0.5));}
};
const Money operator +( const Money& amount1, const Money& amount2);
const Money operator -( const Money& amount1, const Money& amount2);
bool operator ==( const Money& amount1, const Money& amount2);
8-3
const Money operator -( const Money& amount);
int main( ){
Money yourAmount, myAmount(10, 9);
cout << "Enter an amount of money: ";
yourAmount.input( );
cout << "Your amount is "; Enter an amount of money: $123.45
yourAmount.output( ); Your amount is $123.45
cout << endl;
My amount is $10.09.
cout << "My amount is ";
One of us is richer.
myAmount.output( );
cout << endl;
$123.45 + $10.09 equals $133.54
if (yourAmount == myAmount) $123.45 - $10.09 equals $113.36
cout << "We have the same amounts.\n";
else
cout << "One of us is richer.\n";
Money ourAmount = yourAmount + myAmount;
yourAmount.output( ); cout << " + "; myAmount.output( );
cout << " equals "; ourAmount.output(); cout << endl;
return 0;
}
Money::Money( ): dollars(0), cents(0)
{ /*Body intentionally empty.*/}
Money::Money(double amount):dollars(dollarsPart(amount)), cents(centsPart(amount))
{ /*Body intentionally empty*/ }
Money::Money( int theDollars): dollars(theDollars), cents(0)
{ /*Body intentionally empty*/}
8-4
Money::Money( int theDollars, int theCents){
if ((theDollars < 0 && theCents > 0) ||(theDollars > 0 && theCents < 0)){
cout << "Inconsistent money data.\n";
exit(1);
}
dollars = theDollars;
cents = theCents;
}
int Money::centsPart( double amount) const{
double doubleCents = amount * 100;
int intCents = (round(fabs(doubleCents))) % 100;
if (amount < 0) //% can misbehave on negatives
intCents = -intCents;
return intCents;
}
const Money operator +( const Money& amount1, const Money& amount2){
int allCents1 = amount1.getCents( ) + amount1.getDollars( )*100;
int allCents2 = amount2.getCents( ) + amount2.getDollars( )*100;
int sumAllCents = allCents1 + allCents2;
int absAllCents = abs(sumAllCents); //Money can be negative.
int finalDollars = absAllCents / 100;
int finalCents = absAllCents % 100;
if (sumAllCents < 0){
finalDollars = -finalDollars;
finalCents = -finalCents;
}
return Money(finalDollars, finalCents);
} 8-5
const Money operator -( const Money& amount1, const Money& amount2){
int allCents1 = amount1.getCents( ) + amount1.getDollars( )*100;
int allCents2 = amount2.getCents( ) + amount2.getDollars( )*100;
int diffAllCents = allCents1 - allCents2;
int absAllCents = abs(diffAllCents);
int finalDollars = absAllCents / 100;
int finalCents = absAllCents % 100;
if (diffAllCents < 0) {
finalDollars = -finalDollars;
finalCents = -finalCents;
}
return Money(finalDollars, finalCents);
}
bool operator ==( const Money& amount1, const Money& amount2) {
return ((amount1.getDollars( ) == amount2.getDollars( ))
&& (amount1.getCents( ) == amount2.getCents( )));
}
const Money operator -( const Money& amount) {
return Money(-amount.getDollars( ), -amount.getCents( ));
}
8-6
void Money::output( ) const {
int absDollars = abs(dollars);
int absCents = abs(cents);
if (dollars < 0 || cents < 0) //accounts for dollars == 0 or cents == 0
cout << "$-";
else
cout << '$';
cout << absDollars;
8-9
Constructors Returning Objects
• Constructor a "void" function?
– We "think" that way, but no
– A "special" function
• With special properties
• CAN return a value!
8-10
Returning by const Value
• Consider "+" operator overload again:
const Money operator +(const Money& amount1, const Money& amount2);
– Returns a "constant object"?
– Why?
• Consider impact of returning "non-const“
Money operator +(const Money& amount1,const Money& amount2);
8-13
Overloading as Member Functions
• Previous example: operators were overloaded as standalone
functions, defined outside a class
• Can overload operator as "member operator“ considered "member
function" like others
• When operator is member function:
const Money operator +(const Money& amount);
– Only ONE parameter, not two!
– Calling object serves as 1st parameter
• Example:
Money cost(1, 50), tax(0, 15), total;
total = cost + tax;
– If "+" overloaded as member operator:
• Variable/object cost is calling object
• Object tax is single argument
– Think of as: total = cost.+(tax);
8-14
class Money {
public: //same as previous example but operator overload class member functions
const Money operator +(const Money& amount2) const;
const Money operator -(const Money& amount2) const;
bool operator ==(const Money& amount2) const;
const Money operator -() const;
private: //same as previous example
};
int main( ){//same as previous example }
const Money Money:: operator +( const Money& secondOperand) const {
int allCents1 = cents + dollars * 100;
int allCents2 = secondOperand.cents + secondOperand.dollars * 100;
int sumAllCents = allCents1 + allCents2;
int absAllCents = abs(sumAllCents); //Money can be negative.
int finalDollars = absAllCents / 100;
int finalCents = absAllCents % 100;
if (sumAllCents < 0){
finalDollars = -finalDollars;
finalCents = -finalCents;
}
return Money(finalDollars, finalCents);
}
const Money Money:: operator -( const Money& secondOperand) const { }
bool Money:: operator ==( const Money& secondOperand) const { }
const Money Money:: operator -( ) const{}
// <The rest of these definition is Self-Test Exercise .>
8-15
Constructors for Automatic Type Conversion
Money baseAmount(100, 60), fullAmount;
fullAmount = baseAmount + 25; The output would be $125.60
fullAmount.output( );
8-19
const Money operator +(const Money& amount1, const Money& amount2) {
int allCents1 = amount1.cents + amount1.dollars*100;
int allCents2 = amount2.cents + amount2.dollars* 100;
int sumAllCents = allCents1 + allCents2;
int absAllCents = abs(sumAllCents); //Money can be negative.
int finalDollars = absAllCents / 100;
int finalCents = absAllCents % 100;
if (sumAllCents < 0){
finalDollars = -finalDollars;
finalCents = -finalCents;
}
return Money(finalDollars, finalCents);
}
const Money operator -( const Money& amount1, const Money& amount2)
//<The complete definition is Self-Test>
8-24
Overloading
<< and >>
8-25
Overloading
<< and >>
void Money::input( ){
char dollarSign;
cin >> dollarSign; //hopefully
if (dollarSign != '$'){
cout << "No dollar sign in Money input.\n";
exit(1);
}
double amountAsDouble;
cin >> amountAsDouble;
dollars = dollarsPart(amountAsDouble);
cents = centsPart(amountAsDouble);
} 8-27
Assignment Operator, =
8-28