Download presentation
Presentation is loading. Please wait.
1
Template Functions Lecture 9 Fri, Feb 9, 2007
2
Algorithms and Functions
A function expresses an algorithm. Often an algorithm can be applied to many different data types without change. A selection sort will sort a list of objects, provided The operator < is defined. The operator = is defined. 2/24/2019 Template Functions
3
Example: SelectionSort.cpp
2/24/2019 Template Functions
4
Template Functions A template function contains the code necessary for an algorithm, but does not specify the data types involved. The data types will be specified later, at which point All necessary operators and functions must be defined for those types. 2/24/2019 Template Functions
5
C++ Template Functions
Write at the beginning of the function definition. Use T in place of the type in the function. template <class T> template <class T> void Sort(T list[], int size); 2/24/2019 Template Functions
6
Example: TemplateSortC++.cpp
2/24/2019 Template Functions
7
C++ Template Functions
A C++ template function is automatically and separately instantiated for as many types as necessary in a program. 2/24/2019 Template Functions
8
C++ Template Functions
More than one template type is possible in a single function. 2/24/2019 Template Functions
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.