-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtemplate_example.cpp
More file actions
30 lines (27 loc) · 1.13 KB
/
template_example.cpp
File metadata and controls
30 lines (27 loc) · 1.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <cmath>
#include <iostream>
// TODO: Create a generic function Product that multiplies two parameters
template <typename T> T Product (T a, T b)
{
return a * b;
}
template <typename T>
T Max(T a, T b) {
return a > b ? a : b;
}
int main() {
double var = 0.01;
std::cout << Product<int>(10, 2) << std::endl;
std::cout << Product<double>(10., 2.) << std::endl;
std::cout << Product<float>(10.f, 2.f) << std::endl;
std::cout << Product(10.f, 2.f) << std::endl; // can call without the <type> as well
int a = 2;
double d = 10.1;
std::cout << Product<int>(d, a) << std::endl; // casts the double to an int inherently
std::cout << Product<double>(d, a) << std::endl; // casts the int to a double inherently
// std::cout << Product(d, a) << std::endl; // no casting occurs and won't compile due to d and a being different types
std::cout << "-------------------------" << std::endl;
std::cout << Max<int>(10, 50) << std::endl;
std::cout << Max<int>(5.7, 1.436246) << std::endl; // Returns 5 since this casts to ints
std::cout << Max(5.7, 1.436246) << std::endl; // Returns 5.7 since it knows these are doubles
}