Template function template class in C++

 C++ Template


In this article of the cpp programming, we will discuss template, its functions, and its classes.


In generic programming, it is a very powerful and important part of the cpp language.


Why template used?


For generic programming,this allows the programmer to use one code for different types rather than write the same code for the different data types. This feature makes it powerful, the code is reusable and flexible.


Let's understand the concept:


Template function:


The function is not different from the regular function but only one difference. It works for different data types.


The regular function works with only a specific type. For different data types, regular function again define for different data types.


The template function defines only one time and uses for different data types.you can do the same work with less code.


How to declare the function template?

Keyword:

Template

The keyword used at the start, then followed by parameters inside the ( ) with the class keyword.


The syntax of a template function


Typename keyword also used in the placed of the class keyword. From the above syntax T accept the arguments of different data types such as int, float, and char, etc.



Examples of the Template functions:



// function
#include <iostream>
using namespace std;

template <class T> //create function 
T GetMin (T num1, T num2) { //function declaration and definition
  T res;
  if(num1<num2)
  {
  res=num1;
  }
  else
  {
  res=num2;
  }
  
  
  return (res);
}

int main () {
  int i=5, j=6, firstnum; //declare and define integers
  long l=18, m=8, secondnum; //declare and define long variables
 
  firstnum=GetMin<int>(i,j); //calling the function
  secondnum=GetMin<long>(l,m); //calling the function
  cout << firstnum << endl;
  cout << secondnum << endl;
  return 0;
}





Template class:

Likewise, function we can also make the generic class in the cpp.


Regular class work with only specific data type but template work with the different data types.

In the class case, the class template is more important than a function because it is hard to write again the whole class code for the different data types.


How to declare the template class?


template<class T>
Class classname{

private:
T variable;
public:
T function(T argu);

};

The T is a template argument that is a place holder of the data type. Inside the class where you write T instead of data type then data type work for all data types. In the above code snippet, T is used for the variable data type, Function return data type, and data type of argument.In these places, we use different data types.

How to create the object of the template class?


For the creation of the object of the class, write the data type inside the < > brackets before the object name.

classname <datatype> objectname;

Examples of the template class:

#include<iostream>
using namespace std;
template <class T>
class cricle{ //create the class

public:
T radius;
T area()
{
T ar;
ar=(3.14)*(radius)*(radius);
return ar;
}
T diameter(){
T daim;
daim=radius+radius;
return daim;
}

};
int main()
{
cricle <int>c1; //object of  class
cricle <float>c2; //object of  class
c1.radius=5;
c2.radius=10.5;
cout<<"area of the cricle 1 = "<<c1.area()<<endl;
cout<<"diameter cricle 1 = "<<c1.diameter()<<endl;
cout<<"area of the cricle 2 ="<<c2.area()<<endl;
cout<<"diameter cricle 2 = "<<c2.diameter()<<endl;
return 0;
}

Output:

area of the cricle 1 = 78
diameter cricle 1 = 10
area of the cricle 2 =346.185
diameter cricle 2 = 21

In this example, two different data types tested on  the same class. The circle class has two parameters with two member function diameter and area. Declare and define the one object by passing int in the brackets.


second object with the float data type. Automatically, the output of int arguments and float arguments in respective data types. The reusability is increased in this way.we use the different data type with one class


Please write the comment,if you find anything incorrect and any problem in the above topic or you want to share more information about the above topic.

Comments

Hire a programmer

Popular posts from this blog

CSS selector

C program to convert decimal to binary number

C Program to convert binary to decimal number