Hi there, we’re Harisystems
"Unlock your potential and soar to new heights with our exclusive online courses! Ignite your passion, acquire valuable skills, and embrace limitless possibilities. Don't miss out on our limited-time sale - invest in yourself today and embark on a journey of personal and professional growth. Enroll now and shape your future with knowledge that lasts a lifetime!".
For corporate trainings, projects, and real world experience reach us. We believe that education should be accessible to all, regardless of geographical location or background.
1C++ Templates
C++ templates are a powerful feature that allows you to write generic code that can be used with different data types. Templates enable code reusability and provide a way to create functions and classes that work with multiple data types. In this article, we will explore the usage of templates in C++ with examples.
1. Function Templates
Function templates allow you to define a generic function that can work with different data types. Here's an example:
#include <iostream>
template <typename T>
T getMax(T a, T b) {
return (a > b) ? a : b;
}
int main() {
int maxInt = getMax(5, 10);
double maxDouble = getMax(3.14, 2.71);
std::cout << "Max integer: " << maxInt << std::endl;
std::cout << "Max double: " << maxDouble << std::endl;
return 0;
}
In the above code, we define a function template getMax()
that takes two arguments of type T
. The T
represents a placeholder for any data type. Inside the function, we compare the two arguments and return the maximum value. In the main()
function, we call the getMax()
function with different data types (int
and double
) and print the results.
2. Class Templates
Class templates allow you to create a generic class that can be instantiated with different data types. Here's an example:
#include <iostream>
template <typename T>
class Stack {
private:
T* elements;
int top;
int capacity;
public:
Stack(int _capacity) : capacity(_capacity), top(-1) {
elements = new T[capacity];
}
void push(T item) {
if (top < capacity - 1) {
elements[++top] = item;
}
}
T pop() {
if (top >= 0) {
return elements[top--];
}
return T();
}
};
int main() {
Stack<int> intStack(5);
intStack.push(1);
intStack.push(2);
intStack.push(3);
std::cout << intStack.pop() << std::endl;
Stack<double> doubleStack(3);
doubleStack.push(3.14);
doubleStack.push(2.71);
std::cout << doubleStack.pop() << std::endl;
return 0;
}
In the above code, we define a class template Stack
that represents a stack data structure. The class template takes a type parameter T
. Inside the class, we have a dynamically allocated array of type T
to store the elements. The class provides methods to push and pop elements from the stack. In the main()
function, we create instances of the Stack
class with different data types (int
and double
) and perform stack operations.
3. Template Specialization
Template specialization allows you to provide specialized implementations for specific data types. Here's an example:
#include <iostream>
#include <string>
template <typename T>
void printValue(T value) {
std::cout << "Generic: " << value << std::endl;
}
template <>
void printValue(std::string value) {
std::cout << "Specialized: " << value << std::endl;
}
int main() {
printValue(5);
printValue(3.14);
printValue("Hello");
return 0;
}
In the above code, we define a function template printValue()
that takes an argument of type T
and prints it. We then provide a specialization of the template for the std::string
data type. Inside the specialization, we print a specific message for strings. In the main()
function, we call the printValue()
function with different data types and observe the specialized behavior for strings.
C++ templates provide a powerful way to write generic code that can work with different data types. Function templates and class templates enable code reusability and flexibility in your programs. Utilize templates to create generic algorithms and data structures that can handle various types effortlessly.
4.5L
Learners
20+
Instructors
50+
Courses
6.0L
Course enrollments
Future Trending Courses
When selecting, a course, Here are a few areas that are expected to be in demand in the future:.
Future Learning for all
If you’re passionate and ready to dive in, we’d love to join 1:1 classes for you. We’re committed to support our learners and professionals their development and well-being.
View CoursesMost Popular Course topics
These are the most popular course topics among Software Courses for learners