https://github.com/izzypt/cpp_module_07
This module is designed to help you understand Templates in CPP.
https://github.com/izzypt/cpp_module_07
cpp templates
Last synced: about 1 month ago
JSON representation
This module is designed to help you understand Templates in CPP.
- Host: GitHub
- URL: https://github.com/izzypt/cpp_module_07
- Owner: izzypt
- Created: 2023-12-11T13:21:52.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2023-12-12T13:49:30.000Z (over 2 years ago)
- Last Synced: 2025-03-04T15:48:02.149Z (over 1 year ago)
- Topics: cpp, templates
- Language: C++
- Homepage:
- Size: 14.6 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# CPP_Module_07
This module is designed to help you understand Templates in CPP.
# Table of Content
- [What are templates](#templates)
- [Templates types](#templates_types)
- [How templates work](#templates_work)
A template is a simple yet very powerful tool in C++.
The simple idea is to pass the data type as a parameter so that we don’t need to write the same code for different data types.
For example, a software company may need to ```sort()``` for different data types. Rather than writing and maintaining multiple codes, we can write one ```sort()``` and pass the datatype as a parameter.
C++ adds two new keywords to support templates: ````template```` and ```typename```. The second keyword can always be replaced by the keyword ```class```.

### Function template
A function template in c++ is a single function template that works with multiple data types simultaneously, but a standard function works only with one set of data types.
A function template starts with the keyword ```template``` followed by template parameter(s) inside <> which is followed by the function definition.
```cpp
template
T functionName(T parameter1, T parameter2, ...)
{
// code
}
```
In the above code, T is a template argument that accepts different data types (int, float, etc.), and ```typename``` is a keyword.
When an argument of a data type is passed to functionName(), the compiler generates a new version of functionName() for the given data type.
### Calling a Function Template
Once we've declared and defined a function template, we can call it in other functions or templates (such as the main() function) with the following syntax
functionName(parameter1, parameter2,...);
For example, let us consider a template that adds two numbers:
```cpp
template
T add(T num1, T num2)
{
return (num1 + num2);
}
```
We can then call it in the main() function to add int and double numbers.
```cpp
int main() {
int result1;
double result2;
// calling with int parameters
result1 = add(2, 3);
cout << result1 << endl;
// calling with double parameters
result2 = add(2.2, 3.3);
cout << result2 << endl;
return 0;
}
```
### Class template
The class template in c++ is like function templates. They are known as generic templates. They define a family of classes in C++.
Syntax of Class Template:
```cpp
template
class class_name
{
//class body;
}
```
A class template starts with the keyword template followed by template parameter(s) inside <> which is followed by the class declaration.
```cpp
template
class className
{
private:
T var;
... .. ...
public:
T functionName(T arg);
... .. ...
};
```
In the above declaration, T is the template argument which is a placeholder for the data type used, and class is a keyword.
Inside the class body, a member variable var and a member function functionName() are both of type T.
### Creating a Class Template Object
Once we've declared and defined a class template, we can create its objects in other classes or functions (such as the main() function) with the following syntax
```cpp
className classObject;
```
For example,
```cpp
className classObject;
className classObject;
className classObject;
```
Templates are expanded at compiler time. This is like macros.
The difference is, that the compiler does type-checking before template expansion.
The idea is simple, source code contains only function/class, but compiled code may contain multiple copies of the same function/class.
