OperatorOverloading

Information about OperatorOverloading

Published on December 30, 2007

Author: Garrick

Source: authorstream.com

Content

CS-240:  CS-240 Operator Overloading Dick Steflik Operator Overloading:  Operator Overloading What is it? assigning a new meaning to a specific operator when used in the context of a specific class ex. << is normally the shift left operator; but when used in conjunction with the class iostream it becomes the operator insertion; this is because the writers of the iostream class overloaded << to be insertion cout << “some string” << endl; remember cout is a predefined object of class iostream in another class, << could be overloaded to mean something. Operator Overloading:  Operator Overloading Why do we do it? the c++ developers felt that it made more sense to overload an operator than to come up with some name for a function than meant the same thing as the operator. using an overloaded operator takes fewer keystrokes many people feel that an overloaded operator is more self documenting Operator Overloading:  Operator Overloading Three basic ways: as a free function (not part of a class) as a member function as a friend function Operator Overloading:  Operator Overloading class Rational { Rational (int , int); const Rational & operator = (const Rational & rhs); const Rational & operator + (const Rational & rhs); bool operator == (const Rational & rhs); int getNumer() const { return numer; } int getDenom() const { return denom; } private: int numer; int denom; } Overloading = as a member:  Overloading = as a member const Rational & Rational :: operator = (const Rational & rhs) { if (this != &rhs) { numer = rhs.numer; denom = rhs.denom; } return *this } Rational r1 , r2; r1 = r2; -note: the if statement catches the special case where someone is trying r1=r1, the if statement makes the code faster Overloading + as a member:  Overloading + as a member const Rational Rational::operator + (const Rational & rhs) { Rational answer ( *this); // initialize the answer with the current object answer += rhs; // add the second operand (assumes += has been overloaded) return answer; // return the answer via the copy constructor } Overloading == as a free function :  Overloading == as a free function // not defined in the Rational class bool operator == (const & Rational lhs , const & Rational rhs) { return (lhs.getDenom() * rhs.getNumer() == lhs.getNumer() * rhs.getDenom()); } Rational r1 , r2; if ( r1 == r2 ) cout……… else cout Overloading == as a member:  Overloading == as a member const bool Rational::operator == (const Rational & rhs ) { return ( numer * rhs.denom == denom * rhs.numer); } Rational r1 , r2 ; if (r1 == r2 ) cout ……. else cout …… Friend Functions:  Friend Functions not a member function has access to private data member functions work with the current (named object) friend functions work with multiple objects of the same class tag as a friend of the class as part of class definition identify, by prototype, each friend of the class Friend Functions (cont.):  Friend Functions (cont.) Friend functions are needed in C++ due to C++’s flawed object model, Java has a better model (all objects are derived from a single object). define the prototype in the public section of the class definition precede the prototype with the keyword “friend” Friend Functions (more):  Friend Functions (more) define the friend implementation in the .cpp file with the member functions do not precede the function name with the class name and the scoping operator (ex. classname::) Overloading == as a friend:  Overloading == as a friend class Rational { Rational (int , int); const Rational & operator = (const Rational & rhs); const Rational & operator + (const Rational & rhs); friend bool operator == (const Rational & lhs , const Rational & rhs); private: int numer; int denom; } bool operator == (const Rational & lhs , const Rational & rhs) { return ( lhs.numer * rhs.denom == lhs.denom * rhs.numer); } Overloading << as a friend:  Overloading << as a friend class Rational { Rational (int , int); const Rational & operator = (const Rational & rhs); const Rational & operator + (const Rational & rhs); friend ostream & operator << (ostream & ostr , const Rational * rhs); private: int numer; int denom; } ostream & operator << (ostream & ostr , const Rational * rhs) { ostr << “numerator = “ << rhs.numer << “ denominator = “ << rhs.denom; } More Overloading Thoughts:  More Overloading Thoughts = overload as a member function == != <= >= < > overload as a member >> << (insertion and extraction) overload as non-members (friends) returning type iostream +-*/% (arithmetics) overload as members += -= ... overload same as + and - Please note::  Please note: The only operators that cannot be overloaded are . (dot operator) .* (pointer-to-member) sizeof ?: (three operands) Slide17:  Only existing operators can be overloaded. new operators cannot be created (have to be made a named function member)

Related presentations


Other presentations created by Garrick

1 3 Dynamic Stability
06. 11. 2007
0 views

1 3 Dynamic Stability

adobe flex
28. 11. 2007
0 views

adobe flex

Pennington Cape Cod
10. 12. 2007
0 views

Pennington Cape Cod

Basko EPS2006 iac
29. 10. 2007
0 views

Basko EPS2006 iac

Stalker
01. 11. 2007
0 views

Stalker

H113j
02. 11. 2007
0 views

H113j

carnival
06. 11. 2007
0 views

carnival

a gavin
06. 11. 2007
0 views

a gavin

demo
12. 11. 2007
0 views

demo

Qiu
15. 11. 2007
0 views

Qiu

Name That Pit Bull
16. 11. 2007
0 views

Name That Pit Bull

20031212 SUPC overview
19. 11. 2007
0 views

20031212 SUPC overview

pe conference presentation
22. 11. 2007
0 views

pe conference presentation

Blind Men
26. 11. 2007
0 views

Blind Men

power point for final project
17. 12. 2007
0 views

power point for final project

CUX Forced Ranking Webinar
14. 11. 2007
0 views

CUX Forced Ranking Webinar

PCIJ PHILIPPINES
07. 12. 2007
0 views

PCIJ PHILIPPINES

Nefropatia
28. 12. 2007
0 views

Nefropatia

self determination handbook
29. 12. 2007
0 views

self determination handbook

christie workshop presentation
03. 01. 2008
0 views

christie workshop presentation

AGU 2002 Presentation
03. 01. 2008
0 views

AGU 2002 Presentation

Rhetoric
04. 01. 2008
0 views

Rhetoric

lec4 robot classification
07. 01. 2008
0 views

lec4 robot classification

EE541 451 class18
07. 01. 2008
0 views

EE541 451 class18

Livermore Seminar
21. 11. 2007
0 views

Livermore Seminar

GVJ seminar 2000
04. 10. 2007
0 views

GVJ seminar 2000

CA1
23. 11. 2007
0 views

CA1

fre intro curator
28. 11. 2007
0 views

fre intro curator

RocketFuelPumpJPC39
08. 11. 2007
0 views

RocketFuelPumpJPC39

12121
11. 12. 2007
0 views

12121

ToyStory2007studycar ds
20. 02. 2008
0 views

ToyStory2007studycar ds

TURKISHYACHTINGSECTOR
23. 11. 2007
0 views

TURKISHYACHTINGSECTOR

REMI Structure
24. 02. 2008
0 views

REMI Structure

12 3 07
27. 02. 2008
0 views

12 3 07

II 5 Meloe3powerpt
30. 12. 2007
0 views

II 5 Meloe3powerpt

marie midterm
19. 11. 2007
0 views

marie midterm

Canadell Kobe2002
26. 11. 2007
0 views

Canadell Kobe2002

2C Plex Status and Plans
30. 11. 2007
0 views

2C Plex Status and Plans

BecomingaLibraryAdvo cate
23. 12. 2007
0 views

BecomingaLibraryAdvo cate

HLAtimeManagement
13. 11. 2007
0 views

HLAtimeManagement

dania planowanie podatkowe
06. 11. 2007
0 views

dania planowanie podatkowe

icot00zimmer
31. 10. 2007
0 views

icot00zimmer