Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 17: 4/4/2003CS148 Spring 20031 CS148 Introduction to Programming II Ayman Abdel-Hamid Department of Computer Science Old Dominion University Lecture.

Similar presentations


Presentation on theme: "Lecture 17: 4/4/2003CS148 Spring 20031 CS148 Introduction to Programming II Ayman Abdel-Hamid Department of Computer Science Old Dominion University Lecture."— Presentation transcript:

1 Lecture 17: 4/4/2003CS148 Spring 20031 CS148 Introduction to Programming II Ayman Abdel-Hamid Department of Computer Science Old Dominion University Lecture 17: 4/4/2003

2 CS148 Spring 20032 Outline Function overloading Function templates Chapter 17

3 Lecture 17: 4/4/2003CS148 Spring 20033 Generic Algorithms Sometimes, we need a single algorithm that might be applied to objects of different data types at different times We need to be able to describe the algorithm without having to specify the data types of the items being manipulated Such algorithm is referred to as a Generic algorithm C++ supports generic algorithms using two mechanisms  Function overloading  Function templates

4 Lecture 17: 4/4/2003CS148 Spring 20034 Function overloading //print an integer variable void print (string vname, int n) { cout << "Value of " << vname << " = " << n <<endl; } //print a character variable void print (string vname, char ch) { cout << "Value of " << vname << " = " << ch <<endl; } //print a float variable void print (string vname, float fl) { cout << "Value of " << vname << " = " << fl <<endl; } Function overloading is the use of the same name for different functions, as long as their parameters types are sufficiently different for the compiler to tell them apart There are really 3 distinct print functions The compiler generates distinct names for each function When it encounters a call to print, it must determine which function to invoke. The compiler compares the type of the second argument with the type of the second parameter. If the second argument is of type int, the compiler generates code to the call the print function that has a second int parameter

5 Lecture 17: 4/4/2003CS148 Spring 20035 Function templates 1/2 using function overloading has its benefits, but we had to supply three different function definitions (nearly identical functions) We use a function template, which allows us to write a function definition and leave out the data types //print a variable name and its value using a function template //SomeType is called the template parameter template void print (string vname, SomeType val) { cout << "Value of " << vname << " = " << val <<endl; } Syntax for function template Template FunctionDefinition TemplateParamList is a sequence of one or more parameter declarations separated by commas, each defined as classidentifer or typename identifier

6 Lecture 17: 4/4/2003CS148 Spring 20036 Function templates 2/2 //print a variable name and its value //SomeType is called the template parameter template void print (string vname, SomeType val) { cout << "Value of " << vname << " = " << val <<endl; } int main (){ int n = 10; char ch = 'a'; float fl = 3.5; print ("n",n); print ("ch",ch); print ("fl",fl); return 0; }// end main () At compile time, the compiler generates three different functions and gives its own internal name to each of these functions (because there are 3 function calls in the main function) The three new functions are called template functions or generated functions as opposed to the function template from which they were created Another form of making a function call is print (“n”,n); where int enclosed in angle brackets is called the template argument If the template argument is omitted, it is deduced from the data type of the second argument


Download ppt "Lecture 17: 4/4/2003CS148 Spring 20031 CS148 Introduction to Programming II Ayman Abdel-Hamid Department of Computer Science Old Dominion University Lecture."

Similar presentations


Ads by Google