Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
4003CEM-Exam-Practise/Functions/templates.cpp
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
30 lines (25 sloc)
1.09 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
using namespace std; | |
/* | |
Templates. They are complex to look at. | |
*/ | |
template<class T> T add(T &x, T &y) { // template to make a function to add numbers | |
// the T means type meaning that the values of the parameters can be any data-type | |
// &x, &y are references to values that can be anything -> float, int, long, double etc | |
T result = x + y; // makes a return variable from the T class part; can be anything | |
// -> is now dynamically typed as it will find itself on runtime not beforehand | |
return result; // return the result | |
} | |
int main() { | |
// as the add function takes in variable references, it needs explicitily defined variables to reference. | |
// it does not work if you just use `cout << add(1, 3) << endl;` | |
// for templates, it requires references to variables | |
int x = 10; | |
int y = 100; | |
float xx = 100.100; | |
float yy = 10.10; | |
cout << add(x, y) << endl; | |
cout << add(xx, yy) << endl; // the variables MUST be the same data-type and cannot be different. | |
// cannot have int + float or double + int | |
return 0; | |
} |