100% found this document useful (1 vote)
61 views19 pages

Templates C++

Templates allow functions and classes to work with different data types. Templates define generic classes and functions that can handle a variety of data types specified as template arguments. There are three approaches to writing functions that work with different types: defining unique functions for each type, function overloading with the same name but different parameters, and function templates which allow the compiler to generate versions of a function for different types. Class templates similarly allow defining generic classes that can be instantiated with specific types.

Uploaded by

Anishma Arora
Copyright
© Attribution Non-Commercial (BY-NC)
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
100% found this document useful (1 vote)
61 views19 pages

Templates C++

Templates allow functions and classes to work with different data types. Templates define generic classes and functions that can handle a variety of data types specified as template arguments. There are three approaches to writing functions that work with different types: defining unique functions for each type, function overloading with the same name but different parameters, and function templates which allow the compiler to generate versions of a function for different types. Class templates similarly allow defining generic classes that can be instantiated with specific types.

Uploaded by

Anishma Arora
Copyright
© Attribution Non-Commercial (BY-NC)
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
You are on page 1/ 19

Templates

(using C++)

Introduction
Templates are part of ANSI C++ Standard. Template is a technique that allows using a single function or class to work with different data types. A single function is created to process any type of data with in template.

Introduction contd.
The template provides generic programming by defining generic classes. In Templates, generic data types are used as arguments and

they can handle a variety of data types.


A function that works for all C++ Data types is called a generic function. The formal arguments of template functions are of template(generic) type.

Types of Templates

Function Templates

Class Templates

C++ Function Templates

Approaches for functions that implement identical tasks for different data types

Nave Approach

Function Overloading Function Template

Instantiating a Function Templates

Approach 1: Nave Approach

create unique functions with unique names for each combination of data types

difficult to keeping track of multiple function names lead to programming errors

Example
void PrintInt( int n ) { cout << "***Debug" << endl; cout << "Value is " << n << endl; } void PrintChar( char ch ) { cout << "***Debug" << endl; cout << "Value is " << ch << endl; } void PrintFloat( float x ) { To output the traced values, we insert: } PrintInt(sum); void PrintDouble( double d ) { PrintChar(initial); }

PrintFloat(angle);

Approach 2:Function Overloading (Review)


The use of the same name for different C++ functions, distinguished from each other by their parameter lists
Eliminates need to come up with many different names for identical tasks. Reduces the chance of unexpected results caused by using the wrong function name.

Example of Function Overloading


void Print( { cout << cout << } void Print( { cout << cout << } void Print( { } int n ) "***Debug" << endl; "Value is " << n << endl;

char ch )
"***Debug" << endl; "Value is " << ch << endl; float x )
To output the traced values, we insert:

Print(someInt); Print(someChar); Print(someFloat);

Approach 3: Function Template


A C++ language construct that allows the compiler to generate multiple versions of a function by allowing parameterized data types.

FunctionTemplate Template < TemplateParamList > FunctionDefinition

TemplateParamDeclaration: placeholder

class typeIdentifier typename variableIdentifier

Template <class SomeType> //Template declaration Class cname { // data members and member functions }

1. The first statement tells the compiler that the following function or class declaration can use the template data type. 2. Templates can not be declared inside classes or functions. 3. They must be global and should not be local.

Example of a Function Template


template<class SomeType>
Template parameter (class, user defined type, built-in types)

void Print( SomeType val ) { cout << "***Debug" << endl; cout << "Value is " << val << endl; }

Template argument

To output the traced values, we insert:

Print<int>(sum); Print<char>(initial); Print<float>(angle);

The first statement tells the compiler that the following function declaration can use the template data type.

Instantiating a Function Template


When the compiler instantiates a template, it substitutes the template argument for the template parameter throughout the function template.
TemplateFunction Call

Function < TemplateArgList > (FunctionArgList)

Summary of Three Approaches


Nave Approach Different Function Definitions Different Function Names
Function Overloading Different Function Definitions Same Function Name

Template Functions One Function Definition (a function template) Compiler Generates Individual Functions

//Function Template
#include<iostream.h> #include<conio.h> template <class SS> void show(SS tv) { cout<<"\n Non member output:"<<tv; } void main() { clrscr(); char x='a'; int y=33; show(x); show(y); getch(); }

Class Template
A C++ language construct that allows the compiler to generate multiple versions of a class by allowing parameterized data types.

Class Template Template < TemplateParamList > ClassDefinition

TemplateParamDeclaration: placeholder

class typeIdentifier typename variableIdentifier

Instantiating a Class Template


Class template arguments must be explicit. The compiler generates distinct class types called template classes or generated classes. When instantiating a template, a compiler substitutes the template argument for the template parameter throughout the class template.

// Class template with member function template <class T> class demo { T mem; public: demo(T tvar) { mem=tvar; } void show(T oth) void main() { { T s; clrscr(); s=oth+mem; demo <int> obj(5); cout<<endl<<"Result is :"<<s; demo <float> obj1(2.3); } obj.show(6); }; obj1.show(2);
getch();
}

//Class template with multiple arguments - constructor


#include<iostream.h> #include<conio.h> template <class T,class T1> class demo { public: demo(T tvar, T1 tv) { cout<<"\n tvar = "<<tvar<<"Size in bytes :"<<sizeof(tvar); cout<<"\n tv = "<<tv<<"Size in bytes :"<<sizeof(tv); } }; void main() { clrscr(); demo <char,int> obj('B',45); demo <int,float> obj1(45,67.3); getch(); }

You might also like