×

Advanced Coding & Software Engineering Program

Duration: 1 Year (12 Months)

Join our premium 1-year program to master cutting-edge technologies and become an industry-ready Software Engineer!

Course Coverage

  • Languages: C, C++, Java, JavaScript, Python
  • Web Technologies: HTML, CSS, Bootstrap 5, MERN Stack, Full Stack Development
  • Databases: MySQL, MongoDB
  • Data Science Libraries: Pandas, NumPy
  • Development Tools: Visual Studio Code, IntelliJ IDEA, PyCharm, Postman, Git, GitHub
  • Cloud Platforms: Vercel, MongoDB Atlas

Program Highlights

  • Live Classes: Interactive sessions with real-time doubt resolution
  • Hands-On Sessions: Practical coding exercises to build real-world skills
  • Industry Experts: Learn from professionals with years of experience
  • Live Project: Work on real-world projects to apply your skills
  • Get Certificate: Earn a professional certificate upon program completion

Course Fee: Only ₹1020 / month
Limited Period Offer!

Function Template and Class Template in C++ – Complete Tutorial



Last Updated on: 22nd Dec 2025 13:40:45 PM

In C++, Templates allow you to write generic programs—programs that work with any data type without rewriting code.

 

Instead of writing multiple versions of the same function or class for int, float, double, etc., templates let you write once and use for all.

 

👉 Templates support code reusability, type safety, and clean design.

 

What is a Template?

Concept Explanation

A template is a blueprint that allows creating functions or classes that can work with different data types without rewriting code.

 

Definition

A template is a C++ feature that enables generic programming by defining functions or classes with placeholders for data types.

 

Real-Life Example of Templates

Example: Universal Remote Control

  • One remote

  • Works with TV, AC, Sound System

  • Same buttons, different devices

 

👉 Template = Universal logic
👉 Data type = Device

 

There are two main types of templates:

  • Function Template

  • Class Template

 

PART 1: FUNCTION TEMPLATE IN C++

 

1. What is a Function Template?

A Function Template is a blueprint that allows a function to work with different data types using a single definition.

 

2. Why Function Templates are Needed?

Without templates ❌:

int add(int a, int b);
float add(float a, float b);
double add(double a, double b);

 

With templates 

template <typename T>
T add(T a, T b);

 

3. Syntax of Function Template

template <class T>
return_type function_name(T variable);

 

or

template <typename T>

 

👉  class  and  typename  are interchangeable.

 

4. Simple Function Template Example

The same function performs addition for multiple data types.

 

Example 

#include <iostream>
using namespace std;

template <typename T>
T add(T a, T b) {
    return a + b;
}

int main() {
    cout << "Integer Addition: " << add(10, 20) << endl;
    cout << "Float Addition: " << add(10.5f, 20.3f) << endl;
    cout << "Double Addition: " << add(5.25, 3.75) << endl;

    return 0;
}

 

Output

Integer Addition: 30
Float Addition: 30.8
Double Addition: 9

 

Explanation of Output

  • Compiler generates different versions of add()

  • Same logic, different data types

  • Type is decided at compile time

 

5. Function Template with Multiple Data Types

Concept Explanation

Templates can accept more than one data type.

Example

#include <iostream>
using namespace std;

template <typename T1, typename T2>
void display(T1 a, T2 b) {
    cout << "Value 1: " << a << endl;
    cout << "Value 2: " << b << endl;
}

int main() {
    display(10, 20.5);
    display("Age", 25);

    return 0;
}

 

Output

Value 1: 10
Value 2: 20.5
Value 1: Age
Value 2: 25

 

Explanation of Output

  • Template supports different parameter types

  • Function adapts automatically

 

6. Real-Life Use of Function Template

Example: Calculator App

  • Same operations (+, −, ×)

  • Works for int, float, double

  • No duplicate code

 

7. Advantages of Function Templates

✔ Code reusability
✔ Type safety
✔ Cleaner and shorter programs
✔ Compile-time efficiency

 

PART 2: CLASS TEMPLATE IN C++

 

8. What is a Class Template?

A Class Template allows you to define a class once and use it for multiple data types.

It is widely used in STL containers like vector, stack, queue.

 

9. Real-Life Example of Class Template

Example: Storage Box

  • One box design

  • Can store books, clothes, electronics

👉 Class Template = Box design
👉 Data type = Item stored

 

10. Syntax of Class Template

template <class T>
class ClassName {
    T variable;
};

 

11. Simple Class Template Example

A class stores and displays a value of any data type.

 

Example

#include <iostream>
using namespace std;

template <class T>
class Data {
    T value;
public:
    Data(T v) {
        value = v;
    }

    void show() {
        cout << "Value: " << value << endl;
    }
};

int main() {
    Data<int> d1(100);
    Data<float> d2(45.6f);
    Data<string> d3("iKeySkills");

    d1.show();
    d2.show();
    d3.show();

    return 0;
}

 

Output

Value: 100
Value: 45.6
Value: iKeySkills

 

Explanation of Output

  • Same class used for different types

  • Memory allocated based on type

 

12. Class Template with Multiple Parameters

A class can accept multiple data types.

 

Example

#include <iostream>
using namespace std;

template <class T1, class T2>
class Pair {
    T1 first;
    T2 second;
public:
    Pair(T1 a, T2 b) {
        first = a;
        second = b;
    }

    void display() {
        cout << "First: " << first << endl;
        cout << "Second: " << second << endl;
    }
};

int main() {
    Pair<int, float> p1(10, 20.5f);
    Pair<string, int> p2("Age", 25);

    p1.display();
    p2.display();

    return 0;
}

 

Output

First: 10
Second: 20.5
First: Age
Second: 25

 

Explanation of Output

  • Template class handles mixed types

  • Useful for key-value pairs

 

13. Function Template vs Class Template 

Function Template Class Template
Generic function Generic class
Used for operations Used for data storage
Less memory usage More flexible
Simple logic Complex structures

 

14. Template Specialization (Intro)

Concept Explanation

Template specialization allows custom behavior for a specific data type.

Example 

template <>
class Data<char> {
public:
    void show() {
        cout << "Character type detected" << endl;
    }
};

 

15. Common Mistakes

❌ Forgetting template keyword
❌ Type mismatch during object creation
❌ Overusing templates unnecessarily

 

16. Interview Questions

  1. What are templates in C++?

  2. Difference between function and class templates?

  3. What is template specialization?

  4. Are templates resolved at compile time?

  5. Why STL uses templates?

 

17. Summary

  • Templates support generic programming

  • Function templates avoid code duplication

  • Class templates create flexible data structures

  • Used heavily in STL and modern C++

  • Improve scalability and maintainability

 

Keep practicing — you're doing amazing!

Happy Coding!    yes


Online - Chat Now
Let’s Connect

Inquiry Sent!

Your message has been successfully sent. We'll get back to you soon!

iKeySkills Logo