Programming

C++ – Template, Class and Function Template

Templates are a way of making our classes more abstract by letting us define the behavior of the class without actually knowing what data type will be handled by the operations of the class.

In essence, this is what is known as generic programming; this term is a useful way to think about templates because it helps remind the programmer that a templated class does not depend on the data types.

To a large degree, a templated class is more focused on algorithmic thought rather than the specific single data type. The template can be used in conjunction with abstract data types in order to allow them to handle any type of data. A template provides the backbone of flexibility in C++ for the generic programming paradigm, Their flexibility is resolved at compile time thus retaining efficiency.

Advantages of Template

  1. Templates are easier to write.
  2. We can create only one generic version of our class or function instead of manually creating specializations.
  3. Templates can be easier to understand
  4. since they can provide a straightforward way of abstracting type information
  5. Templates are type-safe. Because the types that the template act upon are known at compile time the compiler can perform the type-safe function

To understand the function templates in object-oriented programming you must have to know how function overloading works let us take an example of function overloading.

In function overloading, we do have to make the different functions as per the parameter going to be supplied to that function even though the actual logic behind the function is the same.

Source Code

#include<iostream.h>
#include<conio.h>
int min(int a, int b){
 return (a<b)?a:b;
}

float min(float a, float b){
 return (a<b)?a:b;
}
char min(char a, char b){
 return (a<b)?a:b;
}

int main(){
 int a=10, b=5;
 cout<<min(a,b)<<endl;
 char p="A", q= "Z";
 cout<<min(p,q)<<endl;
 float z=1.8, x=2.3;
 cout<<min(z,x)<<endl;
 return 0;
}

In the above example, a different function is made according to the argument supplied from the main function. The Same function is overloaded for a different purpose.

While using function overloading the same function is copied more times and it looks like the complexity and coding ratio will increase.

Hereby using a function template we can reduce the coding ratio that function overloading takes. The function templates are the function whose argument types are decided when the function is called and not when the function is declared or defined.

The syntax for Function Templates

template<class T>
T function_name(Parameter Declaration){
 //statements 
}

In the above syntax, the T is the generic type. Each parameter declaration begins with T and then space and then the identifier for each object.

C++ Source

#include<iostream.h>
#include<conio.h>
template<class T>
T min(T a, T b){
 return (a<b)?a:b;
}
int main(){
 int a=10, b=5;
 cout<<min(a,b);
 float x=2.2, y=3.4;
 cout<<min(x,y);
 char q="A", p="B";
 cout<<min(q,p);
 getch();
 return 0;
}

The basic concept behind the class template and function template is quite similar. In the case of a function template, we make a single function a generic function but in the case of a class template concept, we make the whole class a generic class.

Indeed, the class is going to accept any type of parameter as per the user demand. Suppose that we are writing a C++ program that required three stacks-one for integer, next for string, and another for float we could implement these classes as follows.

The syntax for Class Template

Template<class T>
class classname{
 ..........
 ..........
 ..........
}

Source Code

#include<iostream.h>
#include<conio.h>
define max 30
tempalte <class foop>
class Stack
{
private:
 foop st[max];
 int top;
public:
 Stack(){
  top=-1;
 }
 void push(T i){
  st[++top]=i;
 }
 T pop(){
  return st[pop--];
 }
};
void main(){
 Stack<int> ii;cout<<"Intger type"<<endl;
 ii.push(10);
 ii.push(20);
 cout<<"Number popped"<<ii.pop();
 cout<<"Number popped"<<ii.pop();
 ii.push(40);
 cout<<"Number popped:"<<ii.pop();
 cout<<"floatdata";
 Stack<float>ff;
 ff.push(10.5);
 ff.push(20.2);
 cout<<"Number popped"<<ff.pop();
 cout<<"Number popped"<<ff.pop();
 ii.push(40.5);
 cout<<"Number popped:"<<ff.pop();
 Stack<char>ss;
 ss.push("A");
 ss.push("B");
 cout<<"char popped"<<ss.pop();
 cout<<"char popped"<<ss.pop();
 ii.push("c");
 cout<<"char popped:"<<ss.pop();
 getch();
}

Tuts

About Author

Tutsmaster.org provides tutorials related to tech and programmings. We are also setting up a community for the users and students.