22
Operator Overloadin g

Operator Overloading. Objectives At the conclusion of this lesson, students should be able to Explain what operator overloading is Write code that overloads

Embed Size (px)

Citation preview

Page 1: Operator Overloading. Objectives At the conclusion of this lesson, students should be able to Explain what operator overloading is Write code that overloads

Operator Overloading

Page 2: Operator Overloading. Objectives At the conclusion of this lesson, students should be able to Explain what operator overloading is Write code that overloads

Objectives

At the conclusion of this lesson, students should be able to Explain what operator overloading is Write code that overloads the standard binary operators Explain the rules for operator overloading

Page 3: Operator Overloading. Objectives At the conclusion of this lesson, students should be able to Explain what operator overloading is Write code that overloads

The expression

a + b * c

is simpler to understand then the expression

plus(a, times(b, c) )

Page 4: Operator Overloading. Objectives At the conclusion of this lesson, students should be able to Explain what operator overloading is Write code that overloads

Therefore, in C++ you can define new meaningsfor the C++ operators that operate on objectsderived from classes that you have defined.

Page 5: Operator Overloading. Objectives At the conclusion of this lesson, students should be able to Explain what operator overloading is Write code that overloads

Some Rules:

An operator is overloaded by writing a function whose nameis the word operator, followed by the symbol for the operatorbeing overloaded.

With some exceptions, these operator can be written as memberfunctions or as non-member functions.

You cannot change the meaning of any of the C++ operators forbuilt in data types. For example, you cannot change how plus workson integer data.

Precedence and associativity are fixed by the language. You cannotchange these for an operator.

Page 6: Operator Overloading. Objectives At the conclusion of this lesson, students should be able to Explain what operator overloading is Write code that overloads

Examples of Operator Overloading:

The Number class

Page 7: Operator Overloading. Objectives At the conclusion of this lesson, students should be able to Explain what operator overloading is Write code that overloads

Class Number{ public: Number( ); Number(int); int getValue( ); private: int theValue;};

Page 8: Operator Overloading. Objectives At the conclusion of this lesson, students should be able to Explain what operator overloading is Write code that overloads

Overloading a Binary OperatorAs a Member Function

Page 9: Operator Overloading. Objectives At the conclusion of this lesson, students should be able to Explain what operator overloading is Write code that overloads

Note that when you write

a = b + c;

the values of b and c do not change.

Page 10: Operator Overloading. Objectives At the conclusion of this lesson, students should be able to Explain what operator overloading is Write code that overloads

When using a member function we willuse the following terminology (a, b, and care objects of the Number class):

this is the righthand operand (rho)

This is the lefthand operand (lho)

a = b + c;

Page 11: Operator Overloading. Objectives At the conclusion of this lesson, students should be able to Explain what operator overloading is Write code that overloads

Overloading a binary operator as a member function

Number Number::operator+(const Number& rho){ int newValue = theValue + rho.getValue( ); Number newNumber(newValue); return newNumber;}

Page 12: Operator Overloading. Objectives At the conclusion of this lesson, students should be able to Explain what operator overloading is Write code that overloads

Number Number::operator+(const Number& rho){ int newValue = theValue + rho.getValue( ); Number newNumber(newValue); return newNumber;}

Question: Why is this Number objectnot being returned by reference?

Page 13: Operator Overloading. Objectives At the conclusion of this lesson, students should be able to Explain what operator overloading is Write code that overloads

Now, when the compiler sees the statement

a = b + c;

it generates the code

a = b.operator+(c);

the message is sentto the left hand operand the right hand operand

is passed as the parameter.

Page 14: Operator Overloading. Objectives At the conclusion of this lesson, students should be able to Explain what operator overloading is Write code that overloads

Overloading a binary operator as a non-member function

Number operator-(const Number& lho, const Number& rho){ int newValue = lho.getValue( ) - rho.getValue( ); Number newNumber(newValue); return newNumber;}

Page 15: Operator Overloading. Objectives At the conclusion of this lesson, students should be able to Explain what operator overloading is Write code that overloads

Overloading the Stream Functions

Page 16: Operator Overloading. Objectives At the conclusion of this lesson, students should be able to Explain what operator overloading is Write code that overloads

ostream& operator<<(ostream& out, const Number& rho){ out << rho.getValue( ); return out;}

Page 17: Operator Overloading. Objectives At the conclusion of this lesson, students should be able to Explain what operator overloading is Write code that overloads

Overloading the Increment Operators

Page 18: Operator Overloading. Objectives At the conclusion of this lesson, students should be able to Explain what operator overloading is Write code that overloads

Number& Number::operator++( ){

theValue++; return *this;

}Can you explain what happenshere? What gets returned?

This is the pre-increment operator

Page 19: Operator Overloading. Objectives At the conclusion of this lesson, students should be able to Explain what operator overloading is Write code that overloads

To overload the post-increment, we have’to give the compiler a clue that tells it thisis a post increment, not a pre-increment.

Also, the code will be much different. Why?

Page 20: Operator Overloading. Objectives At the conclusion of this lesson, students should be able to Explain what operator overloading is Write code that overloads

Number Number::operator++(int n){

Number newNumber(theValue); theValue++; return newNumber;

}

The int parameter tellsthe compiler that thisis a post-increment.

Page 21: Operator Overloading. Objectives At the conclusion of this lesson, students should be able to Explain what operator overloading is Write code that overloads

Overloading a Comparison Operator

Page 22: Operator Overloading. Objectives At the conclusion of this lesson, students should be able to Explain what operator overloading is Write code that overloads

bool Number::operator==(const Number& rho){ if (theValue == rho.theValue) return true; else return false;}