Chapter_16

Chapter 16: Exceptions, Templates, and the Standard Template Library (STL)

16.1 Function Templates

  • Concept: A function template is a generic function that can operate with any data type. The programmer specifies the function's behavior using type parameters instead of concrete data types.

  • Compilation: On encountering a function call, the compiler generates specific code for the data types utilized in that call.

Benefits of Function Templates
  • Reduce Redundancy:

    • Overloaded functions may lead to redundancy as each function is individually created for each data type.

    • Example:

      int square(int number) { return number * number; }
      double square(double number) { return number * number; }
    • By using a template:

      template <class T>  
      T square(T number) {  
          return number * number;  
      }
    • This allows a single definition to work with multiple data types seamlessly.

Structure of a Function Template
  • Syntax:

    template <class T>  
    T square(T number) {  
        return number * number;  
    }
  • Components:

    • template keyword – indicates a template definition.

    • <class T> – declares a type parameter.

    • The function uses T as a placeholder for any data type.

16.2 Usage of Function Templates

  • Example Program:

    #include <iostream>
    using namespace std;
    
    template <class T>
    T square(T number) {
        return number * number;
    }
    
    int main() {
        int userInt;
        double userDouble;
        cout << "Enter an integer and a floating-point value: ";
        cin >> userInt >> userDouble;
        cout << "Here are their squares: ";
        cout << square(userInt) << " and " << square(userDouble) << endl;
        return 0;
    }
  • Expected Output: Provides the squared values of the input numbers (e.g., input 4 and 2.5 may output 16 and 6.25).

  • Compiler Behavior: The compiler generates definitions for int square(int number) and double square(double number) based on the runtime data types.

16.3 Implementing Multiple and Overloaded Function Templates

  • Overloading Function Templates:

    • Function templates can be overloaded by varying the number or types of parameters:

      template <class T>  
      T sum(T val1, T val2) {
          return val1 + val2;
      }
      
      template <class T>  
      T sum(T val1, T val2, T val3) {
          return val1 + val2 + val3;
      }
  • Usage in Main Function:

    int main() {
        double num1, num2, num3;
        cout << "Enter two values: ";
        cin >> num1 >> num2;
        cout << "Their sum is " << sum(num1, num2) << endl;
        cout << "Enter three values: ";
        cin >> num1 >> num2 >> num3;
        cout << "Their sum is " << sum(num1, num2, num3) << endl;
        return 0;
    }

16.4 Class Templates

  • Concept: Class templates allow the creation of generic classes that can handle multiple data types.

  • Declaration:

    template <class T>  
    class SimpleVector { 
        // Class members and methods defined here
    };  
  • Implementation Example: An implementation might be defined in both SimpleVector.h and SimpleVector.cpp, including constructors, destructors, and member functions that manipulate the internal storage of the class. For example, the ListNode structure manages a linked list where member functions allow appending, inserting, and deleting nodes efficiently.

16.5 Introduction to the Standard Template Library (STL)

  • Concept: STL is a collection of template classes and functions designed for generic programming, allowing developers to use pre-built templates for common programming tasks.

  • Containers and Iterators: Contain data and iterators provide access to data. Types of containers include:

    • Sequence Containers: such as vectors, deques, and lists.

    • Associative Containers: like sets and maps.

  • STL Algorithms: STL includes many useful algorithms, such as sort, find, and binary_search, which are also implemented as function templates.

  • Example Program: Using STL’s vector class template to manage a dynamic array while demonstrating various member functions for adding and accessing elements.

Conclusion

  • Templates significantly decrease redundancy in code for functions and classes by enabling the definition of type parameters generically. The STL equips developers with robust tools and collections for efficient data management, making programming faster and more manageable.