Generic programming is a programming paradigm that aims to make code more flexible, reusable, and type-agnostic. It focuses on writing algorithms and data structures that can work with any data type. In C++, generic programming is primarily achieved using templates. Templates allow functions, classes, and structs to operate with generic types without specifying the exact data types upfront.
Templates in C++:
Function Templates:
Syntax of Function Template:
template <typename T>
T add(T a, T b) {
return a + b;
}
In this example, the function add can add values of any type (e.g., int, double, etc.), and the exact type is determined at compile-time when the function is called.
Example:
#include <iostream>
using namespace std;
template <typename T>
T add(T a, T b) {
return a + b;
}
int main() {
cout << add(3, 4) << endl; // int
cout << add(3.5, 4.5) << endl; // double
cout << add(1.2f, 2.3f) << endl; // float
return 0;
}
Output:
7
8
3.5
Class Templates:
Syntax of Class Template:
template <typename T>
class Box {
private:
T value;
public:
Box(T v) : value(v) {}
T getValue() {
return value;
}
};
Example:
#include <iostream>
using namespace std;
template <typename T>
class Box {
private:
T value;
public:
Box(T v) : value(v) {}
T getValue() {
return value;
}
};
int main() {
Box<int> box1(5);
Box<double> box2(3.14);
cout << "Box1 contains: " << box1.getValue() << endl;
cout << "Box2 contains: " << box2.getValue() << endl;
return 0;
}
Output:
Box1 contains: 5
Box2 contains: 3.14
Template Specialization:
Syntax of Template Specialization:
template <typename T>
void print(T value) {
cout << "Generic print: " << value << endl;
}
// Specialization for int
template <>
void print<int>(int value) {
cout << "Specialized print for int: " << value << endl;
}
Example:
#include <iostream>
using namespace std;
template <typename T>
void print(T value) {
cout << "Generic print: " << value << endl;
}
template <>
void print<int>(int value) {
cout << "Specialized print for int: " << value << endl;
}
int main() {
print(3.14); // Calls generic print
print(5); // Calls specialized print for int
return 0;
}
Output:
Generic print: 3.14
Specialized print for int: 5
Template Parameters:
typename or class): Used to specify the type for the template.Example with Non-type Parameters:
template <typename T, int size>
class Array {
private:
T arr[size];
public:
void setElement(int index, T value) {
if (index < size) arr[index] = value;
}
T getElement(int index) {
if (index < size) return arr[index];
return T(); // Return default value if out of bounds
}
};
int main() {
Array<int, 5> arr;
arr.setElement(0, 10);
arr.setElement(1, 20);
cout << arr.getElement(0) << endl; // 10
cout << arr.getElement(1) << endl; // 20
return 0;
}
Variadic Templates:
Example with Variadic Templates:
template <typename... Args>
void print(Args... args) {
(cout << ... << args) << endl; // Fold expression (C++17 feature)
}
int main() {
print(1, 2.5, "Hello", 'A'); // Can print any number and type of arguments
return 0;
}
Output:
12.5HelloA
Code Reusability:
Type Safety:
Efficiency:
Extensibility:
Abstraction:
Code Bloat:
Compilation Time:
Error Messages:
Complexity:
Open this section to load past papers