10. Can you describe the concept of templates in C++?

Basic

10. Can you describe the concept of templates in C++?

Overview

Templates in C++ are a powerful feature that allows functions and classes to operate with generic types. This means you can write a function or a class to work with any data type without rewriting the entire code for each type. Templates are essential for writing flexible and reusable code in C++.

Key Concepts

  1. Function Templates - Create functions that can operate on different data types.
  2. Class Templates - Define classes that can handle any data type.
  3. Template Specialization - Customize templates for specific data types to optimize performance or functionality.

Common Interview Questions

Basic Level

  1. What is a template in C++?
  2. Can you write a simple template function for swapping two elements?

Intermediate Level

  1. Explain the difference between class templates and function templates.

Advanced Level

  1. How does template specialization work in C++?

Detailed Answers

1. What is a template in C++?

Answer: A template in C++ is a blueprint or formula for creating generic classes or functions. Templates allow you to write code that works with any data type. You can use templates to create functions or classes that defer the specification of one or more types until the function or class is instantiated.

Key Points:
- Templates increase code reusability and flexibility.
- They use a keyword template followed by a template parameter list.
- Templates can be used for both functions and classes.

Example:

// Example of a simple function template
template<typename T>
void swap(T& x, T& y) {
    T temp = x;
    x = y;
    y = temp;
}

2. Can you write a simple template function for swapping two elements?

Answer: Sure, the function swap takes two elements of any data type (denoted by template parameter T) and swaps their values.

Key Points:
- typename T or class T can be used to declare a template parameter.
- The function works with any data type that supports assignment.
- This approach eliminates the need for overloading the function for different data types.

Example:

template<typename T>
void swap(T& a, T& b) {
    T temp = a;
    a = b;
    b = temp;
}

3. Explain the difference between class templates and function templates.

Answer: Function templates are used to create functions that can operate on different data types, whereas class templates are used to define a blueprint for generating classes that can manage any data type.

Key Points:
- Function Templates: Allow writing a single function to work with different data types.
- Class Templates: Enable the creation of a class to work with any type of data.
- Templates need to be instantiated with specific types before use.

Example:

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

// Class template example
template<class T>
class Box {
public:
    T content;
    void setContent(T newContent) {
        content = newContent;
    }
};

4. How does template specialization work in C++?

Answer: Template specialization allows defining a custom implementation of a template for a specific data type. This is useful for optimizing the template or providing special behavior for certain types.

Key Points:
- Provides a way to customize template behavior for specific types.
- Uses the template<> syntax with the specific type(s) for which the specialization is intended.
- Can be partial (for a class template with multiple parameters, specialize only some of the parameters) or explicit (all parameters are specialized).

Example:

// General template
template<class T>
class Storage {
public:
    void print() {
        cout << "Storage for generic types" << endl;
    }
};

// Specialization for int type
template<>
class Storage<int> {
public:
    void print() {
        cout << "Storage for int" << endl;
    }
};

This structure provides a clear and comprehensive guide to understanding the concept of templates in C++, tailored for interview preparation at various levels of expertise.