C++ Code Example 1: Function Templates – Swap two Numbers

This C++ program demonstrates the use of templates to create a versatile swap function that can interchange the values of two variables of any data type. By utilizing pointers, the function directly modifies the original variables. The program showcases the functionality with integer, float, and double types, highlighting the power and flexibility of templates in C++.

#include <iostream>
using namespace std;

template <class T>
void swap(T* a, T* b) {
    T temp;

    cout << "Before swapping:\na: " << *a << "\nb: " << *b << endl; 
    temp = *a;
    *a = *b;
    *b = temp;
    cout << "After swapping:\na: " << *a << "\nb: " << *b << endl; 
}

int main() {
    int a = 5, b = 10;
    float c = 6.7f, d = 3.45f;
    double e = 4.34, f = 6.75;

    swap<int>(&a, &b);
    swap<float>(&c, &d);
    swap<double>(&e, &f);

    /*or without the specification of a type
    swap(&a, &b);
    swap(&c, &d);
    swap(&e, &f); */

    return 0;
}
Output
Before swapping:
a: 5
b: 10
After swapping:
a: 10
b: 5
Before swapping:
a: 6.7
b: 3.45
After swapping:
a: 3.45
b: 6.7
Before swapping:
a: 4.34
b: 6.75
After swapping:
a: 6.75
b: 4.34

Code Explanation

Template Function Definition

template <class T>
void swap(T* a, T* b) {
    T temp;

    cout << "Before swapping:\na: " << *a << "\nb: " << *b << endl; 
    temp = *a;
    *a = *b;
    *b = temp;
    cout << "After swapping:\na: " << *a << "\nb: " << *b << endl; 
}
  • template <class T>: This line defines a template. A template allows the function to work with any data type. The class T part indicates that T is a placeholder for any data type.
  • void swap(T* a, T* b): This declares a function named swap that takes two pointers to a type T (the type will be specified when the function is called).
  • Inside the function:
    • T temp;: This creates a variable temp of type T.
    • cout << "Before swapping:\na: " << *a << "\nb: " << *b << endl;: This prints the values pointed to by a and b before swapping.
    • temp = *a;: This saves the value pointed to by a into temp.
    • *a = *b;: This assigns the value pointed to by b to the location pointed to by a.
    • *b = temp;: This assigns the value stored in temp to the location pointed to by b.
    • cout << "After swapping:\na: " << *a << "\nb: " << *b << endl;: This prints the values pointed to by a and b after swapping.

Main Function

int main() {
    int a = 5, b = 10;
    float c = 6.7f, d = 3.45f;
    double e = 4.34, f = 6.75;

    swap<int>(&a, &b);
    swap<float>(&c, &d);
    swap<double>(&e, &f);

    /*or without the specification of a type
    swap(&a, &b);
    swap(&c, &d);
    swap(&e, &f); */

    return 0;
}
  • int main() { ... }: This defines the main function where the execution of the program begins.
  • Variable declarations:
    • int a = 5, b = 10;: Two integer variables a and b are declared and initialized.
    • float c = 6.7f, d = 3.45f;: Two float variables c and d are declared and initialized.
    • double e = 4.34, f = 6.75;: Two double variables e and f are declared and initialized.
  • Function calls to swap:
    • swap<int>(&a, &b);: This calls the swap function with int as the type T. The addresses of a and b are passed to the function.
    • swap<float>(&c, &d);: This calls the swap function with float as the type T. The addresses of c and d are passed to the function.
    • swap<double>(&e, &f);: This calls the swap function with double as the type T. The addresses of e and f are passed to the function.
  • The commented-out section shows that we can also call the swap function without explicitly specifying the type because the compiler can infer the type from the arguments passed.
  • return 0;: This indicates that the program terminated successfully.

Explanation

This program demonstrates the use of function templates to create a generic swap function that can swap the values of two variables of any data type. The template mechanism ensures type safety and avoids code duplication. By passing pointers to the function, it directly modifies the original variables.