Post on 26-Mar-2015
INTRODUCTION,FUNCTION TEMPLATES, CLASS TEMPLATES, NESTED CLASS TEMPLATES,STL,
TEMPLATES
www.bookspar.com | Website for students | VTU NOTES
WE FREQUENTLY COME ACROSS FUNCTIONS THAT WORK EXACTLY SAME WAY FOR
DIFFERENT DATA TYPES
• Each of these functions is designed to handle a a specific datatype.
• For different types of variables, only the keyword used to declare variables upon which they work changes.
• The algorithm implementation of these functions and structure of the function remains the same.
• Eg – function to swap 2 values
www.bookspar.com | Website for students | VTU NOTES
A FUNCTION TO SWAP 2 INTEGERS
void swap(int &a, int &b)
{
int temp;
temp = a;
a = b;
b = temp;
}
www.bookspar.com | Website for students | VTU NOTES
A FUNCTION TO SWAP 2 FLOAT TYPE NUMBERS
void swap( float &a, float &b)
{
float temp;
temp=a;
a=b;
b=temp;
www.bookspar.com | Website for students | VTU NOTES
NOTE THAT 2 SWAP FUNCTIONS ARE SAME EXCEPT FOR DATATYPE OF VARIABLES UPON WHOM THEY
WORK.
• C++ provides facility to write a common function i.e. independent of a datatype but embodies common algorithm and C++ on its own creates the actual function as and when the need arises.
• Having the code at a common place has advantages 1. Ease in code Maintenance and this facility is provided in form of Templates.
• Programmer can create a template with some or all variables therein having unspecified datatypes.
www.bookspar.com | Website for students | VTU NOTES
• Whenever the template is invoked by passing arguments of a certain type, the C++ language on its own replaces the unspecified type with type of arguments passed. Such templates can be created for individual functions as well as entire classes.
www.bookspar.com | Website for students | VTU NOTES
FUNCTION TEMPLATES - SYNTAX FOR CREATING A TEMPLATE FOR
A GENERIC FUNCTION
template <class T,…>
return_type function_name(T arg1,…)
{
//statements
}
Syntax for a fucntion templatewww.bookspar.com | Website for students |
VTU NOTES
THE TEMPLATE DEFINITION BEGINS WITH A TEMPLATE KEYWORD THAT IS
FOLLOWED BY A LIST OF GENERIC DATATYPES IN ANGULAR BRACKETS
Each generic type is prefixed with a class keywordand , if a template function work with more than onegeneric type, commas separate them.
Hence function template is defined as any ordinaryfunction with return type coming first , followed byfunction_name in turn followed by a pair ofparentheses enclosing the list of formal argumentsthe function takes. In the least, there should be
atleastone formal argument of each one of generic typeswithin angular bracketswww.bookspar.com | Website for students |
VTU NOTES
TEMPLATE FOR THE FUNCTION SWAP CAN BE AS FOLLOWS :
//swap .hTemplate <class T>Void swap(T &a, T &b){
T temp;temp=a;a=b;b=temp;
}www.bookspar.com | Website for students |
VTU NOTES
SUPPOSE THE FUNCTION SWAP IS CALLED BY PASSING 2 INTEGERS. THE COMPILER GENERATES AN ACTUAL DEFINITION FOR THE FUNCTION BY
REPLACING EACH OCCURRENCE OF OF T THE KEYWORD INT
/*swap1.cpp*/
Similarly if the function swap is called by passing 2 floats, the compiler generates an actual definition for function by replacing each occurrence of T by the keyword float an so on.
/*swap2.cpp*/
www.bookspar.com | Website for students | VTU NOTES
OBJECTS OF A CLASS CAN ALSO BE PASSED TO THE FUNCTION SWAP. THE COMPILER WILL
GENERATE AN ACTUAL DEFINITIONBY REPLACING EACH OCCURRENCE OF T BY THE NAME OF CORRESPONDING CLASS //SWAP3.CPP
• Note – The amount of effort saved in code development is significant development. One definition suffices for all possible types.• Templates are very handy tool provided by C++
for implementing code reusability.• Compiler generates an actual function from a
template only once for a given datatype.• Eg – if swap is called by passing integers for first
time, compiler will generate the a function definition from its template
www.bookspar.com | Website for students | VTU NOTES
SUBSEQUENT CALLS WITH SAME DATATYPE WILL NOT GENERATE THE SAME DEFINITION AGAIN.
REASON – COMPILER FIRST LOOKS FOR AN EXACT MATCH
• Reason – Compiler first looks for an exact match to resolve a function call before looking for a template.
• If match found, it will not look for template.
• Since first call itself generates function definition, subsequent calls don’t do so.
• Definition of a function must appear in the header file. Compiler will not generate correct definition
www.bookspar.com | Website for students | VTU NOTES
EXPORT KEYWORD PUTS DEFINITON OF TEMPLATE FUNCTION IN A LIBRARY KEEPING ONLY
PROTOTYPE IN A HEADER FILE. ALL COMPILERS MAY NOT SUPPORT THIS KEYWORD.
www.bookspar.com | Website for students | VTU NOTES