+ All Categories
Home > Documents > Operator Overloading

Operator Overloading

Date post: 22-Feb-2016
Category:
Upload: kaiya
View: 31 times
Download: 0 times
Share this document with a friend
Description:
Operator Overloading. 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. The expression a + b * c - PowerPoint PPT Presentation
Popular Tags:
23
Operator Overloadin g
Transcript
Page 1: Operator Overloading

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 the standard binary operators Explain the rules for operator overloading

Page 3: Operator Overloading

The expression

a + b * c

is simpler to understand then the expression

plus(a, times(b, c) )

Page 4: Operator Overloading

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

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

Most operators can be overloaded by writing thefunction either as a member function of the classor as a non-member function.

There are some exceptions. For example, you haveto use a non-member function to overload <<.

Page 7: Operator Overloading

Examples of Operator Overloading:

The Number class

Page 8: Operator Overloading

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

Page 9: Operator Overloading

Overloading a Binary OperatorAs a Member Function

Page 10: Operator Overloading

Note that when you write

a = b + c;

the values of b and c do not change.

Page 11: Operator Overloading

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 12: Operator Overloading

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 13: Operator Overloading

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 14: Operator Overloading

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 15: Operator Overloading

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 16: Operator Overloading

Overloading the Stream Functions

Page 17: Operator Overloading

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

Page 18: Operator Overloading

Overloading the Increment Operators

Page 19: Operator Overloading

Number& Number::operator++( ){

theValue++; return *this;

}Can you explain what happenshere? What gets returned?

This is the pre-increment operator

Page 20: Operator Overloading

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 21: Operator Overloading

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

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

}

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

Page 22: Operator Overloading

Overloading a Comparison Operator

Page 23: Operator Overloading

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


Recommended