UNIT IV
• Generic Programming with Templates:
• Introduction, Need for Templates, Definition
of class Templates, Normal Function
Templates, Over Loading of Template
Function, Bubble Sort Using Function
Templates Difference between Templates and
Macros, Linked list with templates.
Generic Programming
• Generic programming is an approach where generic types are
used as parameters in algorithms so that they work for a
variety of specific data types.
• In C++ generic programming is implemented with “templates”.
• Templates allow a programmer to write a function or class to
work on many different data types with out having to recode
specific versions for each data type.
• Using templates it is possible to create generic functions and
generic classes.
Generic Functions
• A function which works for all types of data is called a Generic
function or a function template.
• A generic function is created using the keyword template.
• While defining the function template we do not know the data
types of parameters and return type also.
• The data types of parameters and return types are decided
while the function is called.
• The general form of generic function definition is as follows:
template <class Ttype>
return-type func-name(Ttype v1, Ttype v2,…)
{
// body of function
}
4
Generic Functions
• C++ routines work on specific types. We often
need to write different routines to perform the
same operation on different data types.
int maximum(int a, int b, int c)
{
int max = a;
if (b > max)
max = b;
if (c > max)
max = c;
return max;
}
5
Generic Functions
float maximum(float a, float b, float c)
{
float max = a;
if (b > max) max = b;
if (c > max) max = c;
return max;
}
6
Generic Functions
double maximum(double a, double b, double c)
{
double max = a;
if (b > max) max = b;
if (c > max) max = c;
return max;
}
The logic is exactly the same, but the data type is different.
Function templates allow the logic to be written once and
used for all data types.
7
Generic Functions
• Generic function to find a maximum value
(see maximum example).
template <class T>
T maximum(T a, T b, T c)
{
T max = a;
if (b > max) max = b;
if (c > max) max = c;
return max;
}
8
Function Templates Usage
• After a function template is included (or defined),
the function can be used by passing parameters of
real types.
Template <class T>
T maximum(T a, T b, T c)
…
int i1, i2, i3;
…
Int m = maximum(i1, i2, i3);
• maximum(i1, i2, i3) will invoke the template function with T as int.
• The function returns a value of int type.
Normal Function Template
Simple Template Function
template <class T>
T findMax(T x, T y)
{
if (x > y)
{
return x;
}
else
{
return y;
}
}
int main()
{
int x = 0;
cout << findMax(x, ++x); // prints 1
cout << x; // prints 1
string s = “hello”;
string t = “world”;
cout << findMax(s, t); // prints “world”
}
Function Template
Overloading of generic functions
• A generic function can be overloaded.
• A template function can be overloaded either by a non-
template function or using an ordinary function
template.
• The matching of the overloaded function is done in the
following manner.
– a. The ordinary function that has an exact match is
called.
– b. A template function that could be created with an
exact match is called.
– c. If no match is found, an error message is generated.
A Function with multiple generic Types
• You can define more than one generic data type
in the template statement by using a comma-
separated list.
template<class T1, class T2,.....>
return_type function_name (T1 v1, T2 v2,….)
{
// body of function.
}
#include <iostream>
using namespace std;
template <class T1, class T2>
void myfunc(T1 x, T2 y)
{
cout << x << “ “ << y << “n”;
}
int main()
{
myfunc(1201, “Bhavani");
myfunc(98.6, ‘A’);
}
Bubble sort using Template Function
Bubble sort using Template Function
Generic Classes
• Like functions ,we can also declare classes to operate
on different data types. Such classes are called
Generic classes.
• Before creating an object of the Generic class,
the actual type of the data being manipulated must
be specified.
• This is done by specifying the data-type as parameter
when the object is created for the template class.
Generic Classes
•The general form of a generic class declaration is as
follows:
template <class Ttype>
class class-name
{
//class body
} ;
•Once you have created a generic class, you create a
specific instance of that class using the following
general form:
class-name <type> object_name;
#include <iostreamh>
using namespace std;
template<class T1,class T2>
class sample
{
T1 a;
T2 b;
public:
void getdata()
{ cout<<"Enter a and b: ";
cin>>a>>b;
}
void display()
{ cout<<"Displaying values"<<endl;
cout<<"a="<<a<<endl; cout<<"b="<<b<<endl;
}
};
int main()
{
sample<int,int> s1;
sample<int,char> s2;
sample<int,float> s3;
cout <<"Two Integer data"<<endl;
s1.getdata();
s1.display();
cout <<"Integer and Character data"<<end
s2.getdata();
s2.display();
cout <<"Integer and Float data"<<endl;
s3.getdata();
s3.display();
return 0;
}
Difference between Templates and Macros
• There are 4 main types of preprocessor directives:
1. Macros
2. File Inclusion
3. Conditional Compilation
Preprocessor directives
Macros
• Macro is a piece of code in a program which is given some name.
• Whenever this name is encountered by the compiler the compiler
replaces the name with the actual piece of code.
• The ‘# define’ directive is used to define a macro.
#define LIMIT 5 //macro definition
int main()
{
for (int i = 0; i < LIMIT; i++)
{
cout << i << "n";
}
}
Macros with arguments
• We can also pass arguments to macros. Macros defined with
arguments works similarly as functions.
#define AREA(l, b) (l * b) //// macro with parameters
int main()
{
int a = 10, b = 5, area;
area = AREA(a, b);
cout << "Area of rectangle is: " << area;
}
Difference between Templates and Macros

TEMPLATES in C++ are one of important topics in Object Oriented Programming

  • 1.
    UNIT IV • GenericProgramming with Templates: • Introduction, Need for Templates, Definition of class Templates, Normal Function Templates, Over Loading of Template Function, Bubble Sort Using Function Templates Difference between Templates and Macros, Linked list with templates.
  • 2.
    Generic Programming • Genericprogramming is an approach where generic types are used as parameters in algorithms so that they work for a variety of specific data types. • In C++ generic programming is implemented with “templates”. • Templates allow a programmer to write a function or class to work on many different data types with out having to recode specific versions for each data type. • Using templates it is possible to create generic functions and generic classes.
  • 3.
    Generic Functions • Afunction which works for all types of data is called a Generic function or a function template. • A generic function is created using the keyword template. • While defining the function template we do not know the data types of parameters and return type also. • The data types of parameters and return types are decided while the function is called. • The general form of generic function definition is as follows: template <class Ttype> return-type func-name(Ttype v1, Ttype v2,…) { // body of function }
  • 4.
    4 Generic Functions • C++routines work on specific types. We often need to write different routines to perform the same operation on different data types. int maximum(int a, int b, int c) { int max = a; if (b > max) max = b; if (c > max) max = c; return max; }
  • 5.
    5 Generic Functions float maximum(floata, float b, float c) { float max = a; if (b > max) max = b; if (c > max) max = c; return max; }
  • 6.
    6 Generic Functions double maximum(doublea, double b, double c) { double max = a; if (b > max) max = b; if (c > max) max = c; return max; } The logic is exactly the same, but the data type is different. Function templates allow the logic to be written once and used for all data types.
  • 7.
    7 Generic Functions • Genericfunction to find a maximum value (see maximum example). template <class T> T maximum(T a, T b, T c) { T max = a; if (b > max) max = b; if (c > max) max = c; return max; }
  • 8.
    8 Function Templates Usage •After a function template is included (or defined), the function can be used by passing parameters of real types. Template <class T> T maximum(T a, T b, T c) … int i1, i2, i3; … Int m = maximum(i1, i2, i3); • maximum(i1, i2, i3) will invoke the template function with T as int. • The function returns a value of int type.
  • 9.
  • 10.
    Simple Template Function template<class T> T findMax(T x, T y) { if (x > y) { return x; } else { return y; } } int main() { int x = 0; cout << findMax(x, ++x); // prints 1 cout << x; // prints 1 string s = “hello”; string t = “world”; cout << findMax(s, t); // prints “world” }
  • 11.
  • 14.
    Overloading of genericfunctions • A generic function can be overloaded. • A template function can be overloaded either by a non- template function or using an ordinary function template. • The matching of the overloaded function is done in the following manner. – a. The ordinary function that has an exact match is called. – b. A template function that could be created with an exact match is called. – c. If no match is found, an error message is generated.
  • 16.
    A Function withmultiple generic Types • You can define more than one generic data type in the template statement by using a comma- separated list. template<class T1, class T2,.....> return_type function_name (T1 v1, T2 v2,….) { // body of function. }
  • 17.
    #include <iostream> using namespacestd; template <class T1, class T2> void myfunc(T1 x, T2 y) { cout << x << “ “ << y << “n”; } int main() { myfunc(1201, “Bhavani"); myfunc(98.6, ‘A’); }
  • 18.
    Bubble sort usingTemplate Function
  • 19.
    Bubble sort usingTemplate Function
  • 20.
    Generic Classes • Likefunctions ,we can also declare classes to operate on different data types. Such classes are called Generic classes. • Before creating an object of the Generic class, the actual type of the data being manipulated must be specified. • This is done by specifying the data-type as parameter when the object is created for the template class.
  • 21.
    Generic Classes •The generalform of a generic class declaration is as follows: template <class Ttype> class class-name { //class body } ; •Once you have created a generic class, you create a specific instance of that class using the following general form: class-name <type> object_name;
  • 22.
    #include <iostreamh> using namespacestd; template<class T1,class T2> class sample { T1 a; T2 b; public: void getdata() { cout<<"Enter a and b: "; cin>>a>>b; } void display() { cout<<"Displaying values"<<endl; cout<<"a="<<a<<endl; cout<<"b="<<b<<endl; } }; int main() { sample<int,int> s1; sample<int,char> s2; sample<int,float> s3; cout <<"Two Integer data"<<endl; s1.getdata(); s1.display(); cout <<"Integer and Character data"<<end s2.getdata(); s2.display(); cout <<"Integer and Float data"<<endl; s3.getdata(); s3.display(); return 0; }
  • 23.
    Difference between Templatesand Macros • There are 4 main types of preprocessor directives: 1. Macros 2. File Inclusion 3. Conditional Compilation
  • 24.
  • 25.
    Macros • Macro isa piece of code in a program which is given some name. • Whenever this name is encountered by the compiler the compiler replaces the name with the actual piece of code. • The ‘# define’ directive is used to define a macro. #define LIMIT 5 //macro definition int main() { for (int i = 0; i < LIMIT; i++) { cout << i << "n"; } }
  • 26.
    Macros with arguments •We can also pass arguments to macros. Macros defined with arguments works similarly as functions. #define AREA(l, b) (l * b) //// macro with parameters int main() { int a = 10, b = 5, area; area = AREA(a, b); cout << "Area of rectangle is: " << area; }
  • 27.