IGNOU BCS-031-Programming in C++, Latest Solved Assignment (July 2023 - January 2024 )


Q2. What is a Template? What are its applications? Give an example.

Ans. In C++, a template is a programming construct that allows us to create generictypes or functions. It enables code reusability and flexibility by allowing us todefine a template that can be used with different data types.

In C++, a template is a powerful feature that allows you to create generic classes and functions that can work with different data types or values. Templates enable you to write code that is not tied to a specific data type but can be reused with various data types, enhancing code reusability and maintainability.

A C++ template is a powerful feature added to C++. It allows you to define thegeneric classes and generic functions and thus provides support for generic programming. Generic programming is a technique where generic types are usedas parameters in algorithms so that they can work for a variety of data types.

Templates can be represented in two ways:

  • Function templates:

Generic functions use the concept of a function template. Generic functions define a set of operations that can be applied to the various types of data. The type of the data that the function will operate on depends on the type of the data passed as a parameter.

#include <iostream>
using namespace std;
template<class T> T add(T &a,T &b)
T result = a+b;
return result;
int main()
int i =2;
int j =3;
float m = 2.3;
float n = 1.2;
cout<<“Addition of i and j is :”<<add(i,j);
cout<<“Addition of m and n is :”<<add(m,n);
return 0;

  • Class templates:

Class Template can also be defined similarly to the Function Template. When a class uses the concept of Template, then the class is known as generic class.

#include <iostream>
using namespace std;
template<class T>
class A
T num1 = 5;
T num2 = 6;
void add()
std::cout << “Addition of num1 and num2 : ” <<
int main()
A<int> d;
return 0;

Templates have various applications, such as:

Generic Containers: Templates are used to define generic container classes likevectors, lists, maps, and queues that can store elements of various data types.

Algorithms: Template functions can be used to define generic algorithms such as sorting, searching, and manipulation that work with different data types.

Function Templates: You can create functions that can operate on different datatypes. This helps avoid code duplication for similar logic.

Class Templates: Templates can be used to define generic classes, allowing you to create classes that can work with various data types.

Smart Pointers: C++ introduced smart pointers like std::shared_ptr and std::unique_ptr using templates. These pointers manage the lifetime of dynamically allocated objects.

Custom Data Types: Templates can be used to create custom data types like matrices, complex numbers, and more, parameterized by data types or values.

STL (Standard Template Library): The C++ Standard Template Library (STL) is a collection of template classes and functions that provide data structures and algorithms.

Templates are a fundamental feature of C++ that contribute to the language’s flexibility, code reusability, and support for generic programming paradigms.



Handwriting Assignment


Other Questions With Answers

Other Subjects

Click Here

Assignment Submission Last Date

The IGNOU open learning format requires students to submit study Assignments. Here is the final end date of the submission of this particular assignment according to the university calendar.

30th April (if Enrolled in the June Exams)

31st October (if Enrolled in the December Exams)

Student Quick Services

error: Content is protected !!