Template Example in C++


Templates are powerful features of C++ which allows you to write generic programs. In simple terms, you can create a single function or a class to work with different data types using templates. Templates are often used in larger codebase for the purpose of code reusability and flexibility of the programs.

Example Program:

Here is the simple program template in C++.

Source Code

using namespace std;
template <class T>
T maximum(T x, T y)
    return x>y?x:y;
template <class T,class R>
void add(T x,R y)
    cout<<"Adding Int Double "<<x+y<<endl;
int main()
    cout<<"Integer Max "<<maximum(10,50)<<endl;
    cout<<"Float Max "<<maximum(10.5,5.0)<<endl;
    return 0;
To download raw file Click Here


Integer Max 50Float Max 10.5Adding Int Double 15.5

Basic Programs

Flow Control

IF Statement Examples

Switch Case

Goto Statement

Break and Continue

While Loop

Do While Loop

For Loop

Friend Function in C++

String Examples

Array Examples

Structure Examples

Structure & Pointer Examples

Structure & Functions Examples

Enumeration Examples

Template Examples


Inheritance Examples

Hierarchical Inheritance

Hybrid Inheritance

Multilevel Inheritance

Multiple Inheritance

Single Level Inheritance

Class and Objects

Constructor Example

Destructor Example

Operator Overloading Example

Operator and Function Example

List of Programs

Pointer Examples

Memory Management Examples

Pointers and Arrays

Virtual Function Examples

Learn All in Tamil © Designed & Developed By Tutor Joes