Template C++
Templates are the basis for generic programming in C++. which involves writing code in a way that is independent of any particular type.
It allows you to write generic programs. In simple words, you can create a single function or a class to work with different data types using templates.
- The concept of templates can be used in two different ways:
1. Function Templates
2. Class Templates
The keyword template is use to create Generic class or Generic function.
Function Template:
A single function template can work with different data types at once but, a single normal function can only work with one set of data types.
The general form of a template function is:
Syntax:
template < class type > return-type function-name(arg-list) { T var; //YOUR CODE }
In the above code, T is a template argument that accepts different data types (int, float), and class is a keyword.
You can also use keyword typename instead of class.
Example:
A function template that returns the maximum of two values:#include<iostream> #include<string> using namespace std; template <class T> T getMax (T const& a, T const& b) { return a > b ? a:b; } int main () { int i = 20; int j = 50; cout << "getMax(i, j): "; cout<< getMax(i, j) << endl; double f1 = 10.5; double f2 = 30.5; cout << "getMax(f1, f2): "; cout<< getMax(f1, f2) << endl; string s1 = "Hello"; string s2 = "World"; cout << "getMax(s1, s2): "; cout << getMax(s1, s2) << endl; return 0; }OUTPUT:
Max(i, j): 50 Max(f1, f2): 30.5 Max(s1, s2): World
Example:
Program to swap data using function templates:#include<iostream> using namespace std; template <class T> void Swap(T &n1, T &n2) { T temp; temp = n1; n1 = n2; n2 = temp; } int main() { int a = 10, b = 20; float f1 = 3.5, f2 = 2.5; char c1 = 'a', c2 = 'b'; cout << "Before Swapping:\n"; cout << " a = " << a << " b = " << b; cout << "\nf1 = " << f1 << " f2 = " << f2; cout << "\nc1 = " << c1 << " c2 = " << c2; Swap(a, b); Swap(f1, f2); Swap(c1, c2); cout << "\n\nAfter Swapping:\n"; cout << " a = " << a << " b = " << b; cout << "\nf1 = " << f1 << " f2 = " << f2; cout << "\nc1 = " << c1 << " c2 = " << c2; return 0; }OUTPUT:
Before Swapping: a = 10 b = 20 f1 = 3.5 f2 = 2.5 c1 = a c2 = b After Swapping: a = 20 b = 10 f1 = 2.5 f2 = 3.5 c1 = b c2 = a
Class Template:
Like function templates, you can also create class templates for generic class operations.
Syntax:
template<class T> class className { ... .. ... public: T var; T function(T arg); ... .. ... };
Here, type is the placeholder type name, which will be specified when a class is instantiated. You can define more than one generic data type by using a comma-separated list.
Object Creation of template class:
className<dataType> classObject;
Code Example:
#include<iostream> using namespace std; template<class T> class Calculator{ public : void sum(T n1, T n2){ T res = n1 + n2; cout<<"ADD RESULT : "; cout<< res<< endl; } void mul(T n1, T n2){ T res = n1 * n2; cout<<"MULTIPLY RESULT : "; cout<< res<< endl; } void square(T n1){ T res = n1 * n1; cout<<"SQUARE RESULT : "; cout<< res<< endl; } }; int main() { Calculator<int> ob1; ob1.sum(20,10); ob1.mul(20,10); ob1.square(8); Calculator<float> ob2; ob2.sum(20.2f,10.7f); ob2.mul(1.5f,10.0f); ob2.square(4.0f); return 0; }OUTPUT:
ADD RESULT : 30 MULTIPLY RESULT : 200 SQUARE RESULT : 64 ADD RESULT : 30.9 MULTIPLY RESULT : 15.0 SQUARE RESULT : 16.0
Training For College Campus
We offers college campus training for all streams like CS, IT, ECE, Mechanical, Civil etc. on different technologies
like
C, C++, Data Structure, Core Java, Advance Java, Struts Framework, Hibernate, Python, Android, Big-Data, Ebedded & Robotics etc.
Please mail your requirement at info@prowessapps.in
Projects For Students
Students can contact us for their projects on different technologies Core Java, Advance Java, Android etc.
Students can mail requirement at info@prowessapps.in