Templates in C++
Generic Programming-Templates in C++
Here, in this section we will discuss about templates in C++.A function declared with integer parameters allows only integer data to be passed during the function call where as a function declared with TEMPLATE data type can accept int, char, float….any data type during the function call .this mechanism is known as generic programming.
Template is a mechanism where one function or class operates on different data types instead of creating separate class/function for each type
Function templates
Templates applied with functions are function templates
Demerits of overloaded function
- Program consumes more disk space
- Time consuming as the function body is replicated with different data types
- Presence of error in one function needs correction in all overloaded functions
Merits of Templates
- Source Code reusability which in turn promotes readability, reduces redundancy, and increases code flexibility
- A single name for function template can be represented for many data types
Syntax
return type function name([template-name var1, template-name var2])
{
Body;
}
- Here template name act as template argument type
- Template argument is substituted whenever a specific data type is declared, we substitute the template argument
Type 1:
#include <iostream>
using namespace std;
template
T sum( T x, T y)
{
return x + y;
}
int main()
{
cout << "Sum : " << sum(3, 5) << endl;
cout << "Sum : " << sum(3.0, 5.2) << endl;
cout << "Sum : " << sum(3.24234, 5.24144) << endl;
return 0;
}
Output –
Sum : 8
Sum : 8.2
Sum : 8.4837
Above code is interpreted internally as
#include <iostream>
using namespace std;
int sum( int x, int y)
{
return x + y;
}
float sum( float x, float y)
{
return x + y;
}
double sum( double x, double y)
{
return x + y;
}
int main()
{
cout << "Sum : " << sum(3, 5) << endl;
cout << "Sum : " << sum(3.0, 5.2) << endl;
cout << "Sum : " << sum(3.24234, 5.24144) << endl;
return 0;
}
Type 2:
Here we declared the function template with two types of template parameters T1 and T2, where T1 is the data type of its first parameter which is an integer value and T2 is of the second parameter which is a floating value. Since the product of an int and a float is a float, hence its return type is also T2.
#include<iostream>
using namespace std;
template < typename T1, typename T2 >
T2 product( T1 x, T2 y)
{
return (T2)(x * y);
}
int main()
{
cout << product(3, 4.7) << endl;
cout << product(4, 5.6) << endl;
return 0;
}
O/P
14.1
22.4
- We can have any number of template arguments and of any type
Class Template
- Templates can be extended at class level and creating class templates is similar to that of function templates
- Class templates are generally used for data storage classes [containers]
Syntax
Template
class
{
body;
};
Difference between function and class templates
- Class Template argument can be used at anywhere in the class specification where as function template argument can be used only within the function specification when there is a reference to the types
- Function templates are instantiated when a function call is encountered where as class templates are instantiated by defining an object using template argument
- Class templates can be used by all member functions of a class
class-nameobject ;
#include<iostream>
using namespace std;
template
class Student{
T marks1;
T marks2;
public:
Student( T m1, T m2 )
{
marks1 = m1;
marks2 = m2;
}
T totalMarks()
{
return marks1 + marks2;
}
T percent()
{
return (totalMarks()/200)*100;
};
int main()
{
Student s1 ( 48 59 );
Student s2 ( 65.5, 96.4 );
cout << "Total marks of student1 : " << s1.totalMarks() << endl;
cout << "Total marks of student2 : " << s2.totalMarks() << endl;
cout << "percent of student1 : " << s1.percent() << endl;
cout << "percent of student2 : " << s2.percent() << endl;
return 0;
}
Here, s1 and s2 are the two objects of class Student and marks1 and marks2 are the marks of the students in first and second subject respectively.
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others
Login/Signup to comment