Templates
Unit-5
Content
Need of template
Types of templates
Class template,
Function template,
overloading of function template
recursion with template function
class template and inheritance
difference between templates and macro
Templates
A template is a blueprint or formula for
creating a generic class or a function.
Templates are the foundation of generic
programming, which involves writing
code in a way that is independent of any
particular type.
Generic Programming
Generic programming is a technique where
generic types are used as parameters in
algorithms so that they can work for a
variety of data types.
Need for templates
A template allows us to create a family of
classes or family of functions to handle
different data types.
Template classes and functions eliminate
the code duplication of different data types
and thus makes the development easier
and faster.
Multiple parameters can be used in both
class and function template.
Template functions can also be overloaded.
We can also use non-type arguments such
as built-in or derived data types as
Types of templates
Function template
• We can define a template for a function.
• Generic functions use the concept a
of template. function
• The type of the data that the function will define it
depends up on the type of the data passed as a
parameter.
• It is created by using the keyword template
class or typename
Syntax:
template <class Ttype>
returnType functionName(parameterList)
{
// body of function.
}
Function template
We can define a template for a function.
For example, if we have an min() function,
we can create versions of the min function
for finding the minimum value from the int,
float or double type values.
Example on function
template
#include <iostream>
using namespace std;
template<typename T>
T mul(T a, T b)
{
return a * b;
int main()
{
cout<<mul(5.2,6.5);
return 0;
}
Which keyword can be used in
template?
a) class
b) typename
c)both class & typename
d) function
Restrictions of Generic Functions
Generic functions perform the same
operation for all the different data type.
For example If function is performing
addition then it will perform addition only it
will not perform subtraction, multiplication
etc.…
There is the difference between function
overloading and function template.
Class Template
A class template starts with the
keyword template followed by template
parameter(s) inside <> which is followed
by the class declaration.
Once we've declared and defined a class
template, we can create its objects in other
classes or functions (such as
the main() function) with the following
syntax
ClassName<dataType> classObject;
Class template
Class template :
To create class in generic form.
Syntax
template <class type>
class className
{
}
Example on class template
#include <iostream>
using namespace std;
template <class T>
class MCA{
public:T a,b;
T add(T x,T y)
{a=x;b=y;
return(a+b);}
};
int main(){
MCA<int>ob;
cout<<ob.add(5,10)<<endl;
MCA<double>ob1;cout<<ob1.add(2.
3,7.6);
}
What is the output of this program?
#include < iostream >
#include < string >
using namespace std; A. 5.5 Hello World
template < typename T >
void print_mydata(T output)
B. 5.5
{ C. Hello World
cout << output << endl;
} D. Error
int main()
{
double d = 5.5;
string s("Hello World");
print_mydata( d );
print_mydata( s );
return 0;
}
Which is used to describe the
function using placeholder types?
A. template parameters
B. template type parameters
C. template type
D. none of the mentioned
overloading of function
template
• Function template also can be overload
• Same function name with different signature
is called function overloading and same thing we can
apply for template also.
For example there is a template function for insert
which take one parameter and with the same name
insert we can create one more template with two
parameters.
recursion with template function
• The process in which a function call by itself is
called recursion and the corresponding
function is called as recursive function.
•We can create recursive template function
How the template class is different from
the normal class?
a)Template class generate objects of
classes based on the template type
b) Template class saves system memory
c)Template class helps in making genetic
classes
d) All of the mentioned
Class template and
inheritance
Templates can be used to implement the
concept of inheritance.
Reusability can be achieved
Example
#include <iostream> template<typename T1>
using namespace std; class demo1 : public demo<T1>
template<typename T> {
class demo{ public:
public: T a,b; void display()
void show() {
{ demo <int>ob;
ob.show();
cout<<"Enter a and b value";
cin>>a>>b; cout<<ob.a<<ob.b;
}}; }};
int main(){
demo1 <int> ob;
ob.display();
return 0;
}
Macros
The preprocessors statements means a instructions to the
compiler to preprocess the information before actual
compilation starts.
All preprocessor statement begin with #
There are number of preprocessor statements like
#include,
#define, #if, #else, #line, etc.
The #define preprocessor statement creates symbolic
constants. The symbolic constant is called a macro
syntax:
#define macro-name replacement-text
Exampl
e:
#include <iostream>
using namespace std;
#define PI 3.14159
int main () {
cout << "Value of PI :" << PI << endl;
return 0;
}
What will be the output of the following C++ code?
#include <iostream>
using namespace std;
int main ()
{
cout << "Value of LINE : " << LINE <<
endl; cout << "Value of FILE : " << FILE <<
endl; cout << "Value of DATE : " << DATE <<
endl; cout << "Value of TIME : " << TIME <<
endl; return 0;
}
a) 5
b) details about your file
c) compile time error
d) runtime error
Macr
o
• No longer
• No repetition
• Define in short
OBJECT ORIENTED PROGRAMMING USING
#include <iostream>
#define ks cout<<"kanika"<<endl; using
namespace std;
int main()
{
kv
return 0;
}
OBJECT ORIENTED PROGRAMMING USING
difference between macro and function:
MACRO FUNCTION
Macro is Preprocessed Function is Compiled
No Type Checking is done in Macro Type Checking is Done in Function
Using Function keeps the code length
Using Macro increases the code length unaffected
Speed of Execution using Function is
Speed of Execution using Macro is Faster Slower
Before Compilation, macro name is During function call, transfer of control
replaced by macro value takes place
Macros are useful when small code is Functions are useful when large code is
repeated many times to be written
Macro does not check any Compile-Time
Errors Function checks Compile-Time Errors