C++ Interview Questions

22 Questions
C++ Programming

C++ Programming

Embedded SystemsIoTOther

Question 19

How does the C++ 'template' mechanism support generic programming? Provide an example.

Answer:

Templates in C++ are a powerful feature that supports generic programming by allowing functions and classes to operate with generic types. This enables code reuse and type safety, making it possible to write more flexible and maintainable code. Templates are one of the key features that make C++ a powerful and versatile language for both system and application programming.

A template in C++ is a blueprint or formula for creating a generic class or function. The idea is to write a function or class once and then use it with different data types without rewriting the entire code for each type. This is achieved by parameterizing the data types used in the function or class definition.

There are two main types of templates in C++: function templates and class templates.

Function Templates:
Function templates allow you to define a generic function that can work with any data type. The function template is defined using the template keyword, followed by a list of template parameters enclosed in angle brackets (<>).

For example, consider a function template that adds two numbers:

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

int main() {
    std::cout << add(2, 3) << std::endl; // Works with int
    std::cout << add(2.5, 3.5) << std::endl; // Works with double
    return 0;
}

In this example, the add function template can be used with different data types, such as int and double. The template parameter T is a placeholder for the actual data type that will be used when the function is called. The compiler generates a specific version of the add function for each data type used.

Class Templates:
Class templates allow you to define a generic class that can operate with any data type. The class template is defined in a similar way to the function template, using the template keyword followed by a list of template parameters.

For example, consider a class template for a simple generic container:

template<typename T>
class Container {
public:
    Container(T value) : m_value(value) {}
    T getValue() const { return m_value; }
private:
    T m_value;
};

int main() {
    Container<int> intContainer(42);
    Container<double> doubleContainer(3.14);
    std::cout << intContainer.getValue() << std::endl; // Outputs 42
    std::cout << doubleContainer.getValue() << std::endl; // Outputs 3.14
    return 0;
}

In this example, the Container class template can hold values of different data types. The template parameter T allows the class to be instantiated with any type, such as int or double. The getValue member function returns the stored value.

Templates also support non-type parameters, which can be used to specify values such as array sizes or constant values. For example, a template for a fixed-size array class might look like this:

template<typename T, size_t N>
class FixedArray {
public:
    T& operator[](size_t index) { return m_array[index]; }
    const T& operator[](size_t index) const { return m_array[index]; }
private:
    T m_array[N];
};

int main() {
    FixedArray<int, 5> intArray;
    for (size_t i = 0; i < 5; ++i) {
        intArray[i] = i * 2;
    }
    for (size_t i = 0; i < 5; ++i) {
        std::cout << intArray[i] << ' '; // Outputs 0 2 4 6 8
    }
    return 0;
}

In this example, the FixedArray class template takes a non-type parameter N that specifies the size of the array. The template allows the creation of arrays of different sizes and types.

In summary, templates in C++ provide a powerful mechanism for supporting generic programming. They enable the creation of functions and classes that can operate with any data type, promoting code reuse and type safety. By using templates, C++ developers can write more flexible and maintainable code, leveraging the full power of generic programming. Templates are a fundamental feature of C++ that contribute to its versatility and efficiency as a programming language.

Recent job openings