C++ – Template, Class and Function Template

0
4
digitalample eye
Templates are a way of making our classes more abstract by letting us to 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 tempalted class does not depend on the data types.

To a large degree, a templated class is more focused on the algorithmic thought rather than the specific single data type.

Template can be used in conjunction with abstract data type in order to allow them to handle any type of data.

 Template provide 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 template act upon are known at compile time the compiler can perform 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 function as per the parameter going to be supplied to that function even though the actual logic behind the function is same.

C++ Source

#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 different function are made according to the argument supplied form the main function. The Same function is overloaded for the different purpose.

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

 Here by using a function template we can reduce the coding ratio which 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.

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 an 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 class template and function template is quite similar. In a case of function template we make a single function as a generic function but in the case of 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 class as follows.

Syntax for Class Template 

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

C++ Source

#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();
}

LEAVE A REPLY

Please enter your comment!
Please enter your name here