function overloading - lecture 21 section 6people.hsc.edu/faculty-staff/robbk/coms261/lectures...

21
Function Overloading Lecture 21 Section 6.14 Robb T. Koether Hampden-Sydney College Wed, Oct 16, 2019 Robb T. Koether (Hampden-Sydney College) Function Overloading Wed, Oct 16, 2019 1 / 20

Upload: others

Post on 02-Jun-2020

13 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Function Overloading - Lecture 21 Section 6people.hsc.edu/faculty-staff/robbk/Coms261/Lectures 2019... · 2019-10-15 · Function Overloading A function name isoverloadedif the same

Function OverloadingLecture 21

Section 6.14

Robb T. Koether

Hampden-Sydney College

Wed, Oct 16, 2019

Robb T. Koether (Hampden-Sydney College) Function Overloading Wed, Oct 16, 2019 1 / 20

Page 2: Function Overloading - Lecture 21 Section 6people.hsc.edu/faculty-staff/robbk/Coms261/Lectures 2019... · 2019-10-15 · Function Overloading A function name isoverloadedif the same

1 Function Overloading

2 Examples

3 Ambiguous Function Calls

Robb T. Koether (Hampden-Sydney College) Function Overloading Wed, Oct 16, 2019 2 / 20

Page 3: Function Overloading - Lecture 21 Section 6people.hsc.edu/faculty-staff/robbk/Coms261/Lectures 2019... · 2019-10-15 · Function Overloading A function name isoverloadedif the same

Outline

1 Function Overloading

2 Examples

3 Ambiguous Function Calls

Robb T. Koether (Hampden-Sydney College) Function Overloading Wed, Oct 16, 2019 3 / 20

Page 4: Function Overloading - Lecture 21 Section 6people.hsc.edu/faculty-staff/robbk/Coms261/Lectures 2019... · 2019-10-15 · Function Overloading A function name isoverloadedif the same

Function Overloading

A function name is overloaded if the same name is used for two ormore distinct functions.Function overloading allows the use of the same name forfunctions with a similar purpose.

Robb T. Koether (Hampden-Sydney College) Function Overloading Wed, Oct 16, 2019 4 / 20

Page 5: Function Overloading - Lecture 21 Section 6people.hsc.edu/faculty-staff/robbk/Coms261/Lectures 2019... · 2019-10-15 · Function Overloading A function name isoverloadedif the same

Function Overloading

Consider the square-root function sqrt().Without function overloading, we would have to make up a distinctname for the square root function for each different data type

float sqrti(int);float sqrtf(float);double sqrtd(double);

Robb T. Koether (Hampden-Sydney College) Function Overloading Wed, Oct 16, 2019 5 / 20

Page 6: Function Overloading - Lecture 21 Section 6people.hsc.edu/faculty-staff/robbk/Coms261/Lectures 2019... · 2019-10-15 · Function Overloading A function name isoverloadedif the same

The Signature of a Function

A function’s signature is the set of characteristics that can bedetermined from the actual parameter list.

The number of parameters.The types of the parameters.The order of the parameters.

Robb T. Koether (Hampden-Sydney College) Function Overloading Wed, Oct 16, 2019 6 / 20

Page 7: Function Overloading - Lecture 21 Section 6people.hsc.edu/faculty-staff/robbk/Coms261/Lectures 2019... · 2019-10-15 · Function Overloading A function name isoverloadedif the same

The Signature of a Function

A function’s signature does not include those characteristics thatcannot be determined from the actual parameter list.

The names of the formal parameters.Whether the parameters are reference parameters.Whether the parameters are constant parameters.The function’s return type.

Robb T. Koether (Hampden-Sydney College) Function Overloading Wed, Oct 16, 2019 7 / 20

Page 8: Function Overloading - Lecture 21 Section 6people.hsc.edu/faculty-staff/robbk/Coms261/Lectures 2019... · 2019-10-15 · Function Overloading A function name isoverloadedif the same

Function Overloading

The compiler uses the actual parameter list to determine whichinstance of an overloaded function to use.Therefore, each instance of an overloaded function must have adistinct signature.Otherwise, the compiler could not determine which one to use.

Robb T. Koether (Hampden-Sydney College) Function Overloading Wed, Oct 16, 2019 8 / 20

Page 9: Function Overloading - Lecture 21 Section 6people.hsc.edu/faculty-staff/robbk/Coms261/Lectures 2019... · 2019-10-15 · Function Overloading A function name isoverloadedif the same

Outline

1 Function Overloading

2 Examples

3 Ambiguous Function Calls

Robb T. Koether (Hampden-Sydney College) Function Overloading Wed, Oct 16, 2019 9 / 20

Page 10: Function Overloading - Lecture 21 Section 6people.hsc.edu/faculty-staff/robbk/Coms261/Lectures 2019... · 2019-10-15 · Function Overloading A function name isoverloadedif the same

Examples of Legal Overloading

Legal Overloadingint max(int x, int y);float max(float x, float y);float max(float x, int y);float max(int x, float y);

It is legal to define simultaneously all of the above functions.

Robb T. Koether (Hampden-Sydney College) Function Overloading Wed, Oct 16, 2019 10 / 20

Page 11: Function Overloading - Lecture 21 Section 6people.hsc.edu/faculty-staff/robbk/Coms261/Lectures 2019... · 2019-10-15 · Function Overloading A function name isoverloadedif the same

Example of an Overloaded Function

ExampleOverloadedMax.cpp

Robb T. Koether (Hampden-Sydney College) Function Overloading Wed, Oct 16, 2019 11 / 20

Page 12: Function Overloading - Lecture 21 Section 6people.hsc.edu/faculty-staff/robbk/Coms261/Lectures 2019... · 2019-10-15 · Function Overloading A function name isoverloadedif the same

Examples of Illegal Overloading

Illegal Overloadingint max(int x, int y);int max(int x, int& y);int& max(int x, int& y);int max(const int x, int y);const int max(int x, int y);float max(int x, int y);

It is illegal to define simultaneously any two of the above functions.

Robb T. Koether (Hampden-Sydney College) Function Overloading Wed, Oct 16, 2019 12 / 20

Page 13: Function Overloading - Lecture 21 Section 6people.hsc.edu/faculty-staff/robbk/Coms261/Lectures 2019... · 2019-10-15 · Function Overloading A function name isoverloadedif the same

Outline

1 Function Overloading

2 Examples

3 Ambiguous Function Calls

Robb T. Koether (Hampden-Sydney College) Function Overloading Wed, Oct 16, 2019 13 / 20

Page 14: Function Overloading - Lecture 21 Section 6people.hsc.edu/faculty-staff/robbk/Coms261/Lectures 2019... · 2019-10-15 · Function Overloading A function name isoverloadedif the same

Ambiguous Function Calls

An Ambiguous Function Callint max(int x, int y);float max(float x, float y);

:float m = max(4, 5.6f);

The function call in the example below is ambiguous.Which instance of max() should be invoked?

Robb T. Koether (Hampden-Sydney College) Function Overloading Wed, Oct 16, 2019 14 / 20

Page 15: Function Overloading - Lecture 21 Section 6people.hsc.edu/faculty-staff/robbk/Coms261/Lectures 2019... · 2019-10-15 · Function Overloading A function name isoverloadedif the same

Type-Casting Parameters

It is not always necessary that the type of the actual parametermatch exactly the type of the formal parameter.The compiler will type-cast the parameter, if possible.The following will occur automatically.

Promotion among the integer types.Promotion among the floating-point types.Conversions between integer and floating-point types.

Robb T. Koether (Hampden-Sydney College) Function Overloading Wed, Oct 16, 2019 15 / 20

Page 16: Function Overloading - Lecture 21 Section 6people.hsc.edu/faculty-staff/robbk/Coms261/Lectures 2019... · 2019-10-15 · Function Overloading A function name isoverloadedif the same

Type-Casting Parameters

Implicit Type-Castingfloat max(float x, float y);double max(double x, double y);

...float m1 = max(1.2, 3.4);int m2 = max(5, 6);double m3 = max(7.8f, 9);

Which function is called?

Run the function OverloadChoice.cpp to find out.

Robb T. Koether (Hampden-Sydney College) Function Overloading Wed, Oct 16, 2019 16 / 20

Page 17: Function Overloading - Lecture 21 Section 6people.hsc.edu/faculty-staff/robbk/Coms261/Lectures 2019... · 2019-10-15 · Function Overloading A function name isoverloadedif the same

Type-Casting Parameters

Implicit Type-Castingfloat max(float x, float y);double max(double x, double y);

...float m1 = max(1.2, 3.4);int m2 = max(5, 6);double m3 = max(7.8f, 9);

Which function is called?Run the function OverloadChoice.cpp to find out.

Robb T. Koether (Hampden-Sydney College) Function Overloading Wed, Oct 16, 2019 16 / 20

Page 18: Function Overloading - Lecture 21 Section 6people.hsc.edu/faculty-staff/robbk/Coms261/Lectures 2019... · 2019-10-15 · Function Overloading A function name isoverloadedif the same

Type-Casting Parameters

Implicit type-casting is automatic whenever there is anunambiguous rule, based on constructors.

Robb T. Koether (Hampden-Sydney College) Function Overloading Wed, Oct 16, 2019 17 / 20

Page 19: Function Overloading - Lecture 21 Section 6people.hsc.edu/faculty-staff/robbk/Coms261/Lectures 2019... · 2019-10-15 · Function Overloading A function name isoverloadedif the same

Explicit Type-Casting

Explicit type-casting may be used to force a type change.There must be a conversion rule available, based on constructors.To explicitly type-cast an object, write the name of the new type inparentheses before the object.

Robb T. Koether (Hampden-Sydney College) Function Overloading Wed, Oct 16, 2019 18 / 20

Page 20: Function Overloading - Lecture 21 Section 6people.hsc.edu/faculty-staff/robbk/Coms261/Lectures 2019... · 2019-10-15 · Function Overloading A function name isoverloadedif the same

Explicit Type-Casting

Explicit Type-Castingint max(int, int);float max(float, float);int a;float b;

...float m1 = max((float)a, b);int m2 = max(a, (int)b);

Explicit type-casting may resolve ambiguous references tooverloaded functions.

Robb T. Koether (Hampden-Sydney College) Function Overloading Wed, Oct 16, 2019 19 / 20

Page 21: Function Overloading - Lecture 21 Section 6people.hsc.edu/faculty-staff/robbk/Coms261/Lectures 2019... · 2019-10-15 · Function Overloading A function name isoverloadedif the same

Assignment

AssignmentRead Section 6.14.

Robb T. Koether (Hampden-Sydney College) Function Overloading Wed, Oct 16, 2019 20 / 20