0% found this document useful (0 votes)
0 views9 pages

Lecture22 New

The document discusses the concept of templates in C++, highlighting their role in code reuse and the advantages they offer over traditional C methods. It explains how template functions and classes allow for compile-time code generation and specialization for different data types. Additionally, it provides examples of template functions and practice problems to reinforce the learning objectives.

Uploaded by

haleemajamil170
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views9 pages

Lecture22 New

The document discusses the concept of templates in C++, highlighting their role in code reuse and the advantages they offer over traditional C methods. It explains how template functions and classes allow for compile-time code generation and specialization for different data types. Additionally, it provides examples of template functions and practice problems to reinforce the learning objectives.

Uploaded by

haleemajamil170
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Templates

Learning Objectives
• Template Functions
• Template Classes
Code Reuse
• Object-orientation provides strong code reuse
approaches
• Inheritance
• Composition
• Aggregation
• Templates is another powerful tool
Scenario
• We’ve implemented several algorithms so far:

12 -45 12 < -45 42 9 4 -7

“Doe
-45 12

< “Joe” ✓ -7 4 9 42

Compariso
Swapping Sorting
n

Did we reuse code or did we copy-


paste?
Code Reuse
• In C, we:
• Either salvaged code after copy-paste
• Or passed around void* along with typecasting
• C++ provides templates as another mechanism for
code reuse
• Remember: This is a compile-time mechanism
• Compiler writes code for us, based on the code we write
Template Functions Example
template<typename T> int main() {
void swap(T& a, T& b) { int a = 43, b = 19;
T temp = a; swap<int>(a, b);
swap(a, b);
a = b;
b = temp;
std::string p1 = "John“;
} std::string p2 = "Jane";
swap(p1, p2);
return 0;
}
Summary
• Compiler matches function calls against template functions:
• Substitutes template parameters
• Produces specialized source code and compiles it
• Template parameters can be:
• A type
• None-type: For example, a constant
• Type template parameter can be used as argument type
• Type template parameter can be used as the return type
• Template parameters can have default values
• Specialized templates can be created for specific data types
• Non-specialized template will be used for all other data types
Practice Problems
• A template function that:
• Returns the maximum of three values
• Reverses the elements of an array
• Finds the maximum element in an array
• Takes an array and swaps the elements at two indices passed
as args
• Takes an array, and its size, and checks if the array is sorted
Template Classes
• Template syntax may be applied to classes (or structs)
• This allows us to create many different data types

You might also like