C++ Tutorial
Class and Object
Reference
Inheritance and Derivation
Polymorphism and Virtual Functions
Operator Overloading
Template
Exception
Object Oriented Advanced
Input/Output Stream
File Operations
In C++, class templates are a powerful feature that allows you to create generic classes that can work with different data types. A class template is a blueprint for generating classes, depending on the data type provided.
Defining a Class Template
To define a class template, use the template
keyword followed by a template parameter list enclosed in angle brackets <>
. Then, define the class as usual, but use the template parameter as a placeholder for the data type.
Here's an example of a simple class template for a Box
that can store a single item of any data type:
template <typename T> class Box { T item; public: void setItem(T newItem) { item = newItem; } T getItem() { return item; } };
In this example, T
is a template parameter that serves as a placeholder for the data type. When creating an object of the Box
class, you'll provide the data type you want to use.
Creating Objects of a Class Template
To create an object of a class template, you need to provide the data type you want to use within angle brackets <>
.
Example:
Box<int> intBox; // A Box that can store an int Box<double> dblBox; // A Box that can store a double Box<std::string> strBox; // A Box that can store a std::string
Using Class Template Objects
You can use objects of class templates just like you would use objects of regular classes.
Example:
#include <iostream> #include <string> template <typename T> class Box { T item; public: void setItem(T newItem) { item = newItem; } T getItem() { return item; } }; int main() { Box<int> intBox; intBox.setItem(42); std::cout << "Integer box contains: " << intBox.getItem() << std::endl; Box<double> dblBox; dblBox.setItem(3.14); std::cout << "Double box contains: " << dblBox.getItem() << std::endl; Box<std::string> strBox; strBox.setItem("Hello, World!"); std::cout << "String box contains: " << strBox.getItem() << std::endl; return 0; }
In this example, we create objects of the Box
class template for different data types (int
, double
, and std::string
) and use their member functions to set and get items.
In conclusion, class templates in C++ enable you to create generic classes that can work with different data types. By defining a class template, you can create a single class that can be reused with various data types, making your code more modular and maintainable.
Introduction to class templates in C++:
#include <iostream> // Class template definition template <typename T> class MyTemplate { public: T data; void display() { std::cout << "Data: " << data << std::endl; } }; int main() { // Instantiating class template with int type MyTemplate<int> intObj; intObj.data = 42; intObj.display(); // Instantiating class template with double type MyTemplate<double> doubleObj; doubleObj.data = 3.14; doubleObj.display(); return 0; }
C++ template specialization for classes:
#include <iostream> // Primary template template <typename T> class MyTemplate { public: void display() { std::cout << "Generic Display" << std::endl; } }; // Template specialization for int template <> class MyTemplate<int> { public: void display() { std::cout << "Specialized Display for int" << std::endl; } };
C++ template constraints for class templates:
#include <iostream> #include <type_traits> // Template with constraints template <typename T, typename = std::enable_if_t<std::is_arithmetic_v<T>>> class MyTemplate { public: T data; void display() { std::cout << "Data: " << data << std::endl; } };
C++ template inheritance with class templates:
#include <iostream> // Base class template template <typename T> class BaseTemplate { public: T data; void display() { std::cout << "Data: " << data << std::endl; } }; // Derived class template inheriting from BaseTemplate template <typename T> class DerivedTemplate : public BaseTemplate<T> { public: void derivedDisplay() { std::cout << "Derived Display" << std::endl; } };
Template member functions in C++ classes:
#include <iostream> // Class template with template member function template <typename T> class MyTemplate { public: T data; // Template member function template <typename U> void displayTemplate(U value) { std::cout << "Template Display: " << value << std::endl; } };
Using multiple template parameters in C++ classes:
#include <iostream> // Class template with multiple parameters template <typename T, typename U> class Pair { public: T first; U second; void displayPair() { std::cout << "Pair: {" << first << ", " << second << "}" << std::endl; } };