T07_OLT - Tutorial 7 COMP152 Spring 2010 Overloading and...

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

View Full Document Right Arrow Icon
Overloading and Template Tutorial 7 COMP152 Spring 2010
Background image of page 1

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

View Full DocumentRight Arrow Icon
Outline Function overloading Operator overloading Function Templates Class Templates 2
Background image of page 2
Function overloading Overloaded functions have Same name Different sets of parameters Overloaded functions are distinguished by their signatures Compiler selects proper function to execute based on number, types and order of arguments in the function call Commonly used to create several functions of the same name that perform similar tasks, but on different data types Creating overloaded functions with identical parameter lists and different return types is a compilation error 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
Example void AddAndDisplay(int x, int y) { cout<<" Integer result: "<<(x+y); } void AddAndDisplay(double x, double y) { cout<< " Double result: "<<(x+y); } void AddAndDisplay(float x, float y) { cout<< “ Float result: "<<(x+y); } 4
Background image of page 4
Operator Overloading Use operators with objects (operator overloading) Clearer than function calls for certain classes Operator sensitive to context Almost all operators in C++ can be overloaded except: . :: ?: sizeof Can only redefine existing operators, but CANNOT define new operators. CANNOT change the properties of an operator Number of arguments an operator takes. (So you are not allowed to re-define the plus operator to take 3 arguments instead of 2.) Associativity. E.g.: a+b+c is always identical to (a+b)+c. Precedence. E.g.: a+b*c is treated as a+(b*c). 5
Background image of page 5

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

View Full DocumentRight Arrow Icon
Operator Overloading For a user-defined class type , every operator defined must have at least one argument. For a global function , operator+ has two arguments. When it is called in an expression such as a+b , this is equivalent to writing operator+(a, b). 6
Background image of page 6
Example: Non-member operator function class Vector { double vx, vy; public: Vector(double x, double y) : vx(x), vy(y) { } double x() const { return vx; } double y() const { return vy; } }; To add 2 vectors, traditionally we would do like this: { return Vector( a.x() + b.x(), a.y() + b.y() ); } d = add(a, add(b, c)); // d = a + b + c 7
Background image of page 7

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

View Full DocumentRight Arrow Icon
Example: Non-member Operator Function (cont.) Using operator overloading, we can do like this:
Background image of page 8
Image of page 9
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 27

T07_OLT - Tutorial 7 COMP152 Spring 2010 Overloading and...

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

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