Written by students who passed Immediately available after payment Read online or as PDF Wrong document? Swap it for free 4.6 TrustPilot
logo-home
Class notes

template and its types in C++

Rating
-
Sold
-
Pages
7
Uploaded on
27-08-2025
Written in
2025/2026

template and its types in C++ in detail

Institution
Course

Content preview

Template
A template is a simple and most powerful tool in C++. It is a frame
which is used to pass the data type as a parameter so that we do not need
to write the same code for different data types.
For example, a programmer may need to sort() elements for different
data types. Rather writing and maintaining multiple codes, programmer
can write one sort() function and pass the datatype as a parameter.
It uses keyword like ‘template’ and ‘type_name’ or ‘class’.
Types of templates:
1. Function Templates (Generic Function- Template for Function)
2. Class Templates (Generic Class – Template for class)
How templates works
Templates are expanded at compile time as macros. The difference is,
the compiler does type-checking before template expansion.
 The source code contains only function/ class.
 But the compiled code may contain multiple copies of the same
function/class.
Function Templates
A function templates or generic function can be used for different data
types.
Syntax:
template <class/ typename Ttype> return_type function_name
(parameter_list)
{
//body of function
}

, class : It is a keyword is used to specify a generic type in a template
declaration.
Ttype : It is a placeholder name for a data type used by the function. It is
used within the function definition. It is only a placeholder that the
compiler will automatically replace with the actual data type.


Example 1
#include<iostream>
using namespace std;
template<typename T>T mymax(T x, T y)
{
return (x>y)?x:y;
}
main()
{
cout<<mymax<int>(3,7)<<"\n";
cout<<mymax<double>(3.0,7.0)<<"\n";
cout<<mymax<char>('g','e');
}



Output
7
7
g

Written for

Course

Document information

Uploaded on
August 27, 2025
Number of pages
7
Written in
2025/2026
Type
Class notes
Professor(s)
Monika sharma
Contains
All classes

Subjects

$8.49
Get access to the full document:

Wrong document? Swap it for free Within 14 days of purchase and before downloading, you can choose a different document. You can simply spend the amount again.
Written by students who passed
Immediately available after payment
Read online or as PDF

Get to know the seller
Seller avatar
monika19sharmaa

Get to know the seller

Seller avatar
monika19sharmaa A private College
Follow You need to be logged in order to follow users or courses
Sold
-
Member since
8 months
Number of followers
0
Documents
20
Last sold
-

0.0

0 reviews

5
0
4
0
3
0
2
0
1
0

Recently viewed by you

Why students choose Stuvia

Created by fellow students, verified by reviews

Quality you can trust: written by students who passed their tests and reviewed by others who've used these notes.

Didn't get what you expected? Choose another document

No worries! You can instantly pick a different document that better fits what you're looking for.

Pay as you like, start learning right away

No subscription, no commitments. Pay the way you're used to via credit card and download your PDF document instantly.

Student with book image

“Bought, downloaded, and aced it. It really can be that simple.”

Alisha Student

Working on your references?

Create accurate citations in APA, MLA and Harvard with our free citation generator.

Working on your references?

Frequently asked questions