topic09_OperatorOverloading

topic09_OperatorOverloading - Operator Overloading in C+ CS...

Info iconThis preview shows pages 1–8. Sign up to view the full content.

View Full Document Right Arrow Icon
Operator Overloading in C++ CS 1037a – Topic 9
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Some standard operators • some operators are defined for all basic types like int , float , char ,… (some are defined for pointers too) - operator+ , operator- , operator+= , operator* , operator/ - operator= ( copy or assignment operator) - operator== , operator!= - operator< , operator> , operator<= • standard operator [ ] is defined for pointers only - if p is a pointer, p[i] returns *(p+i) (more in Topic 2) 9-2 C++ allows to define/ overload operators for objects of any class !
Background image of page 2
Overloading Operators • Examples of operator overloading we saw earlier: - operator [ ] overloaded (defined) for class Table2D (topic 2) - assignment (copy) operator= overloaded for Point (topic 1) 9-3
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Overloading Operators • In many cases operator overloading simplifies code making it look like basic math . For example: operator+ can be overloaded for - two Points (vector addition) - two Table2D objects (matrix summation) - two Lists, Bags,… - ……. operator* can be overloaded for - two Table2D objects (matrix multiplication) - two Points (scalar or dot product) - ……. . 9-4
Background image of page 4
Example 1: adding points (vectors) 9-5 Point operator +( const const Point& b) { return Point( a.x+b.x, a.y+b.y ); } operator+ above allows to simplify equivalent expression c = Point(a.x+b.x,a.y+b.y); Expressions like c = a+b; for three Points a , b , c can be used if there exists operator
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Example 2: multiplication for points 9-6 Point operator *( const const Point& a) { return Point( s*a.x, s*a.y ); } Expressions like b = s*a; for two Points a , b , and scalar s can be used if there exists operator operator* above allows to simplify equivalent expression b = Point(s*a.x,s*a.y);
Background image of page 6
Example 3: “dot” product for points 9-7 double operator *( const const Point& b) { return a.x*b.x+a.y*b.y; } Expressions like
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 8
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 22

topic09_OperatorOverloading - Operator Overloading in C+ CS...

This preview shows document pages 1 - 8. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online