Understanding Class Templates in C++

Introduction to Class Templates

1. Definition and Purpose of Class Templates

(1)Classes often serve to store and manage data

(2)The organization of data within a class is independent of the specific data types involved

(3)Examples include array classes, linked list classes, stack classes, and queue classes

(4)C++ introduces templates for classes, enabling implementation that abstracts away specific data types while focusing on functional behavior

2. Implementation in C++

(1)Enables uniform handling of various data types

(2)Class declarations begin with the template keyword

(3)<typename T> indicates the use of a generic type T within the class

3. Practical Usage

(1)Requires explicit type specification; automatic deduction is not supported

(2)Instantiation uses specific types like <Type> to create objects

(3)The generic type T can appear anywhere within the template class

(4)Compilation process mirrors function templtaes

A. Generating distinct classes from a template using specific types

B. Compiling the template code at the point of declaration

C. Compiling parameterized code at the point of usage

<strong>#include<iostream>
#include<string>

using namespace std;

template<typename T>
class Calculator
{
public:
    T sum(T x, T y)
    {
        return x + y;
    }
    T subtract(T x, T y)
    {
        return x - y;
    }
    T product(T x, T y)
    {
        return x * y;
    }
    T quotient(T x, T y)
    {
        return x / y;
    }
};

string operator - (string& left, string& right)
{
    return left + "-" + right;
}

int main()
{
    Calculator<int> calc1;
    cout << "5 + 3 = " << calc1.sum(5, 3) << endl;
    
    Calculator<string> calc2;
    cout << "Hello - World = " << calc2.subtract("Hello", "World") << endl;
    return 0; 
}</strong>

Engineering Applications of Class Templates

1. Template definitions must reside in header files

2. Implementation cannot be separated into different files

3. Member functions defined outside the class require template declarations

//Calculator.h

<strong>#ifndef _CALCULATOR_H_
#define _CALCULATOR_H_

template<typename T>
class Calculator
{
public:
    T sum(T x, T y);
    T subtract(T x, T y);
    T product(T x, T y);
    T quotient(T x, T y);       
};

template<typename T>
T Calculator<T>::sum(T x, T y)
{
    return x + y;
}

template<typename T>
T Calculator<T>::subtract(T x, T y)
{
    return x - y;
}

template<typename T>
T Calculator<T>::product(T x, T y)
{
    return x * y;
}

template<typename T>
T Calculator<T>::quotient(T x, T y)
{
    return x / y;
}

#endif</strong>

//main.cpp

<strong>#include<iostream>
#include"Calculator.h"

using namespace std;

int main()
{
    Calculator<int> calc1;
    cout << "10 + 5 = " << calc1.sum(10, 5) << endl;
    
    return 0; 
}</strong>

Key Points

(1)Generic programming principles apply to class design

(2)Class templates provide consistent operations across different data types

(3)They are particularly useful for implementing data structures

(4)Type specification must be explicit during instantiation

Tags: C++ Templates class-template generic-programming data-structures

Posted on Tue, 30 Jun 2026 17:47:37 +0000 by shibiny