9 Templates And Generic Programming

Object Oriented Programming · Unit 9

Templates and Generic Programming

Exam-focused notes for Templates and Generic Programming (Object Oriented Programming, BIT153): what the TU syllabus asks and how it has actually been tested, with 5 solved past questions from this unit.

What this unit covers

  • Template concept and importance
  • Function templates
  • Class templates
  • Template instantiation
  • Generic programming with templates

Function templates

20825 marks

What are function templates? Also, write a program that swaps two values using function templates. [1+4]

A function template is a blueprint or formula for creating a family of related functions. It allows a single function definition to work with different data types, where the actual data type is determined at compile time when the function is called. Key Poi...

Full solved answer →
2080.15 marks

What is function template? Write a program to demonstrate the use of function template that returns the greatest among integers, and float passed to it. [1+4]

A function template is a blueprint or generic function that allows a single function definition to work with different data types. Instead of writing separate functions for each data type, a function template uses a type parameter (placeholder) that is repl...

Full solved answer →
05 marks

Why concept of templates is important? Explain function templates with example. [5]

Templates are important in C++ for the following reasons: 1. Generic Programming: Templates allow writing generic (type-independent) code that works with any data type, avoiding code duplication. 2. Code Reusability: A single template function or class can ...

Full solved answer →

Class templates

20805 marks

What do you mean by class template in C++? Write a program in C++ containing function template which determines the greater number between two integer inputs and two floating point inputs. [5]

A class template in C++ is a blueprint for creating a family of classes where the data type is parameterized. Instead of writing separate classes for different data types, a single class template can be defined and the compiler generates the appropriate cla...

Full solved answer →

Template concept and importance

207910 marks

What do we need template? Why the program terminate after the occurrence of exception?Describe the types of constructor.[4+6]

A template is a feature in C++ that allows writing generic programs -- functions or classes that work with any data type without rewriting the code for each type. 1. Code Reusability Without templates, we must write separate functions for each data type: Wi...

Full solved answer →