0% found this document useful (0 votes)
2 views7 pages

output

The document explains C++ templates, focusing on their compile-time expansion and how they allow for generic programming through function and class templates. It provides examples of function templates like myMax() and bubbleSort(), demonstrating their ability to work with any comparable type. Additionally, it covers class templates for data structures and the use of multiple and default template parameters.

Uploaded by

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

output

The document explains C++ templates, focusing on their compile-time expansion and how they allow for generic programming through function and class templates. It provides examples of function templates like myMax() and bubbleSort(), demonstrating their ability to work with any comparable type. Additionally, it covers class templates for data structures and the use of multiple and default template parameters.

Uploaded by

dikshakarna696
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

UNIT V · GENERIC PROGRAMMING

<
Templates in C++, by Example
myMax()

1
bubbleSort() Array<T> — one blueprint, compiled per type

2 3
>
Compile-Time Expansion Function Templates Class Templates
How the compiler turns one template into myMax() and a full bubble sort, generic over A generic Array<T>, plus multiple & default
many functions type parameters

C++ TEMPLATES 1
THE MECHANISM

How do templates work?


Templates expand at compile time, like macros — but the compiler type-checks first. Your source has one function; the compiled binary can hold several.

source (written once) generated for int

template <typename T> int myMax(int x, int y) {


T myMax(T x, T y) { return (x > y) ? x : y;
return (x > y) ? x : y; }
}

int main() {
→ generated for char
myMax<int>(3, 7); compiler
char myMax(char x, char y) {
myMax<char>('g', 'e'); expands
return (x > y) ? x : y;
}
}

myMax<int>(3, 7) → 7 myMax<char>('g','e') → g

The compiler picks the T you request in angle brackets — myMax<int> and myMax<char> become two separate, fully type-checked functions from one written definition.

C++ TEMPLATES 2
FUNCTION TEMPLATES · 01

A generic comparison function


Function templates power everyday tools like sort(), max(), min(), and printArray() — one definition, any comparable type.

[Link]
Explicit type argument
1
template <typename T> T myMax(T x, T y) myMax<int>(...) tells the compiler exactly which T to instantiate.
{
return (x > y) ? x : y;
}
Works for any comparable type
int main() { 2
Even user-defined types work, as long as operator > is overloaded.
cout << myMax<int>(3, 7) << endl;
cout << myMax<double>(3.0, 7.0)
<< endl;
cout << myMax<char>('g', 'e') << endl; Return type follows T
return 0; 3
T myMax(T x, T y) — the return type is generic too, not just the
} parameters.

▸ Output

7
7
g
C++ TEMPLATES 3
FUNCTION TEMPLATES · 02

A real algorithm: generic bubble sort


Templates aren't limited to one-liners — any algorithm built only from comparison and swap works for every type.

bubble_sort.cpp
class T vs typename T
template <class T> void bubbleSort(T a[], int n) 1
class here means the same thing as typename — pick either
{ keyword.
for (int i = 0; i < n - 1; i++)
for (int j = n - 1; i < j; j--)
if (a[j] < a[j - 1])
swap(a[j], a[j - 1]); Built from < and swap()
} 2
Any type supporting operator< and swap works — int,
double, or your own class.
int main() {
int a[5] = { 10, 50, 30, 40, 20 };
bubbleSort<int>(a, 5);
// print a[] ...
}

▸ Output

Sorted array : 10 20 30 40 50

C++ TEMPLATES 4
CLASS TEMPLATES · 01

A generic Array class


Class templates work the same way for data structures: LinkedList, Stack, Queue, BinaryTree — all can be written once, generically.

[Link] main()

template <typename T> class Array { int main() {


T* ptr; int size; int arr[5] = {1,2,3,4,5};
public: Array<int> a(arr, 5);
Array(T arr[], int s); [Link]();
void print(); }
};

template <typename T> Array<T>::Array(T arr[], int s)


{ ▸ Output
ptr = new T[s]; size = s; 1 2 3 4 5
for (int i = 0; i < size; i++)
ptr[i] = arr[i];
}
Array<int> fixes T = int for that object — Array<double> would
be a different type entirely.

C++ TEMPLATES 5
CLASS TEMPLATES · 02

Multiple & default template arguments


Templates accept more than one type parameter, and — just like function parameters — those parameters can have defaults.

multi_param.cpp default_param.cpp

template <class T, class U> class A { template <class T, class U = char> class A {
T x; U y; public:
public: T x; U y;
A() { cout << "Constructor Called"; } A() { cout << "Constructor Called"; }
}; };

A<char, char> a; A<char> a; // U defaults to char


A<int, double> b;

▸ Output ▸ Output

Constructor Called Constructor Called


Constructor Called
A<char> a; is equivalent to A<char, char> a;
Two independent placeholders — T and U never have to match.

C++ TEMPLATES 6
RECAP

What we covered

1 Compile-time expansion 2 Function templates

One template definition; the compiler generates a type-checked copy per Generic algorithms like myMax() and bubbleSort() — built from comparisons
instantiation, e.g. myMax<int> and myMax<char>. that work for any type.

3 Class templates 4 Multiple & default parameters

Generic data structures like Array<T> — the same idea applied to a whole template<class T, class U = char> — more than one placeholder, and defaults
class, not just a function. just like ordinary function arguments.

C++ TEMPLATES 7

You might also like