C++ Tutorial

Define C++

C++ First Program

Difference C and C++

C++ Operators

C++ Type Casting

C++ Function Overloading

C++ Default Arguments

C++ Inline Function

Define OOP's

C++ Keywords

C++ Classes

Exercise Classes

C++ Nested Classes

C++ Constructors

Exercise Constructor

C++ Friend function

C++ Operator Overloading

Ex: Operator Overloading

C++ Inheritance

C++ this pointer

C++ Polymorphism

Virtual Class & Function

C++ File Handling

Exercise File Handling

C++ Templates

C++ Exception Handling

C++ Graphics

Exercise Graphics

Page Stats

Visitor: 470

C++ Templates

Templates is a new concept which enable us to define generic functions and classes. Generic programming is an approach where any particular data type can be used as parameters, So, template provide a variety of suitable data type.

Function Templates

A Function templates work in similar manner as function works, but with one difference. A single function cannot work with different data types but A single function template can work on different data types at once.

If you need to perform identical operation on two or more data types then, you can use function overloading. But better approach is to use function templates because you can perform this task by writing less code and code is easier to maintain.

Example : Input 2 numbers and find largest number using function template.

#include<iostream.h>
#include<conio.h>

int max(t x, t y)
{
    if(x>y)
	{
		return x;
	}
	else
	{
		return y;
	}
}
void main()
{
	int a, b, c;
	clrscr();
	cout<<"Enter 2 numbers: ";
	cin>>a>>b;
	c=max(a,b);
	cout<<"Largest Number is :"<<c;
	getch();
}

Template Functions Overloading

Example : Find input number is odd or even using template function overloading. Create two function, 1st function that accept integer numbers and 2nd function that accept float numbers.

#include<iostream.h>
#include<conio.h>

template <class t>
void even (t x)
{
	if(fmod(x,2)==0)
		cout<<"Number is Even";
	else
		cout<<"Number is Odd";
}
void even (int x)
{
	if(x%2==0)
	{
		cout<<"Number is Even";
	}
	else
	{
		cout<<"Number is Odd";
	}
}
void main()
{
	int a;
	clrscr();
	cout<<"Enter Integer Number";
	cin>>a;
	even(a);
	double d;
	cout<<"\nEnter Decimal Number";
	cin>>d;
	even(d);
	getch();
}

Class Template

Like Function template, we can also define class templates. The class template syntax is similar to function template except that we are defining classes instead of functions.

Example : Input 2 number and find sum using class template

#include<iostream.h>
#include<conio.h>

template <class t>
class sum
{
	t a, b, c;
	public:
	  void input()
	  {
		  cout<<"Enter 2 numbers";
		  cin>>a>>b;
	  }
	  void output()
	  {
		  c = a + b;
		  cout<<"Sum = <<c;
	  }
};
void main()
{
	clrscr();
	sum <int> x;
	x.input();
	x.output();
	
	sum <float> y;
	y.input();
	y.output();
	
	getch();
}

Class Templates with Multiple Parameters

Example : Input 2 numbers and find square of both the numbers using multiple parameters with class template

#include<iostream.h>
#include<conio.h>

template <class t1, class t2>
class square
{
	t1 a;
	t2 b;
	public:
	  void input()
	  {
		  cout<<"Enter 2 numbers";
		  cin>>a>>b;
	  }
	  void output()
	  {
		  a = a * a;
		  b = b * b;
		  cout<<"Square of 1st number is = <<a;
		  cout<<"Square of 2nd number is = <<b;
	  }
};
void main()
{
	clrscr();
	square <int,float> x;
	x.input();
	x.output();
	getch();
}