3.6 Generic Programming and Exception Handling
1. Function Templates
template <typename T> T add(T a, T b) { return a + b; }
#include <iostream>
using namespace std;
template <typename T> // Function template for any type T
T add(T a, T b) {
return a + b;
}
int main() {
cout << add(5, 10) << endl; // Output: 15 (int)
cout << add(3.5, 2.5) << endl; // Output: 6.0 (double)
cout << add('A', 'B') << endl; // Output: 131 (char)
return 0;
}2. Class Templates
3. Standard Template Library (STL)
4. Exception Handling
Conclusion
Last updated