Home | | Programming and Data Structure II | Templates: Class and function templates

Chapter: Programming and Data structures : C++ Programming Advanced Features

Templates: Class and function templates

A significant benefit of object oriented programming is reusability of code which eliminates redundant coding.

TEMPLATES:

 

·        A significant benefit of object oriented programming is reusability of code which eliminates redundant coding.

 

·        An important feature of oops called Templates makes this benefit stronger and provides the greater flexibility to the languages.

 

·        A template allows the construction of family of functions and classes to perform the same operation on different types.

 

·        This provides the generic programming which allows developing the reusable software component such as classes and function.

 

·        There are two types of templates.

Ø   Function templates

 

Ø   Class templates

 

·        Terms which means use the same function or class for different purpose without changing their basic meaning where the function or class should be defined only once.

 

·        Templates are used to achieve reusability which eliminates redundant code.

 


CLASS TEMPLATES:

 

Class templates are used to create generic class witch support different data types.

 

 

Syntax:

 

template <class T>

class <class_name>

{

 

Member;

Member function;

};

Example:

 

#include<iostream.h>

 

#include<conio.h>

template <class T> class complex

 

{

 

T real, image; public:

void getdata()

 

{

 

cout<<"\n Enter the complex values"; cin>>real>>image;

 

}

 

void putdata()

 

{

 

if(image>0) cout<<real<<"+"<<image<<"i";

 else cout<<real<<image<"i";

}

 

 

}

 

;

 

void main()

 

{

 

clrscr();

 

complex <int> obj1;

 obj1.getdata();

 obj1.putdata();

complex <float> obj2;

 obj2.getdata();

obj2.putdata();

getch();

 

}

 

FUNCTION TEMPLATES:

 

The template declared for function is function template. Function templates are generic function, which work for any data that is passed to them. The data type is not specified while declaring the function. It performs appropriate operation depending on the data type we passed to them.

 

 

Syntax:

 

Template<typename T ,….>

 

Return Type Function-name(argument)

 

{

 

Function body

 

}

 

 

Example:

 

Template < class T>

 

void generic Bubblesort(T Temp GenericArray[]) // template function 1 {

 

for( int i=0;i<5; i++)

 

{

 

for( int j=i+1;j<5;j++)

 

{

 

if(TempGenericArray[i]<TempGenericArray[j])

 

{

 

int temp=TempGenericArray[i];

TempGenericArray[i]=TempGenericArray[j];

 TempGenericArray[j]=temp;

 

}

 

}

 

Template <class T>

 

void Generic Display(T TempGenericArray[]) // template function2

 

{

 

cout<<“\n”;

 

for(int i=0;i<5;i++)

 

{

cout<<TempGenericArray[i]<<“\t”;

 

}

 

}

 

void main()

 

{

 

int Array1[]={1,4,6,2,6};

 

GenericBubbleSort(Array 1);  //calling function template 1

 

GenericDisplay(Array2);         // calling function template 2

 

Char Array2[]=“sdfla”;

 

GenaricBubbleSort(Array2)    //calling function template 1

 

Generic Display(Array 2);

 

Foat Array3[]={ 7.0,9.4,5.2,2.5,0.5);

 

GenericBubble Sort (Array 3);          // calling function template 1

GenericBubbleSort( Array3 ); // calling function template 2

}                                    

Sample output:                       


 

 

FUNCTION TEMPLATES WITH MULTIPLE ARGUMENTS:

 

Function templates can have different data type to represent the parameters.

 

Syntax:

 

template <class T1, class T2> return_type function_name(arguments)

{

 

<function body>

 

}

 

 

Example:

 

#include<iostream.h>

 

#include<conio.h>

 

template <class T1, class T2>

 

 

void display(T1 a, T2 b)

 

{

 

cout<<a<<"\n"<<b<<"\n";

 

}

 

void main()

 

{

 

clrscr();

 

void display(T1,T2);

 display("sample","program");

 display(10,20); display(7.5,2);

 display(10.75,10.256);

getch()

 

 

 

}

 

STANDARD TEMPLATE LIBRARY:

 

STL is collection C++ libraries that allow you to use several well known kinds of data structures without having to program them. They are designed so that the code runs efficiently.

 

The compiler does most of the work of generating the efficient implementations. The libraries include a large number of possibilities.

 

 

The STL contains three components.

§    Containers

 

o   A Container is an object that stores other objects (its elements), and that has methods for accessing its elements.

§    Algorithms

 

o   An algorithm is a procedure that is used to process the data contained in the containers. The STL includes many different kinds of algorithms to provide support to tasks such as initializing, searching, copying, and sorting and merging

§    Iterators

 

Iterators are a generalization of pointers: they are objects that point to other objects.

 

Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail
Programming and Data structures : C++ Programming Advanced Features : Templates: Class and function templates |


Privacy Policy, Terms and Conditions, DMCA Policy and Compliant

Copyright © 2018-2024 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.