C++ Programming
- Template
Template involves writing code in a way that is independent of any particular type. The idea of template is to pass data type as a parameter so that the same code can be used for different data types.
(a) How does template work?(ref: https://www.geeksforgeeks.org/templates-cpp/)
Templates are expanded at a compiler time. A template can be used to define functions and/or classes. Two keywords: ‘template’ and ‘typename’ are to support template.
Function Template Example:
Output:
7
7
g
Class Template can be used when a class defines something that is independent of the data type, such as LinkedList, BinaryTree, Stack, Queue, Array et.al;
Example:
Outputs:
1 2 3 4 5
- Static Declaration
(a) Member Variable
Static member variables belong to the class other than objects of the class.
(b) Static Member Function
Static member functions can be used to work with static member variables in the class. An object of the class is not required to call them.