0% found this document useful (0 votes)
13 views2 pages

Introduction to Templates

Uploaded by

ankurccet
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views2 pages

Introduction to Templates

Uploaded by

ankurccet
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Research Paper Engineering Volume : 2 | Issue : 1 | October 2012 | ISSN - 2249-555X

Introduction to Templates

Keywords
Mr. A. N. Shah Mr. N. N. Shah
P.G. Student,Computer Department, C.U.Shah College of Lecturer (E.C.), R.K. University school of Engineering,
Engineering & Technology, Wadhwan City Rajkot.
ABSTRACT Templates allow us to define generic class and function. So it supports generic programming. Generic
programming is an approach where generic types are used as parameters in algorithms so that they work for a variety of
suitable data types and data structures. A template can be used to create a family of classes or functions. For example, it can
be used with class like student etc and it also can be used for function mul() etc. A template is also known as a kind of macro.
Whenever template is created for any class it required substituted data types. Since template is define with a parameter for
classes or functions so it is sometime called as parameterized classes or functions.
The main aim of this thesis is to show various ways to use template either with function or class.

INTRODUCTION };
Template is one which supports generic class and function. void main()
So it supports generic programming. Generic programming {
is an approach where generic types are used as parameters clrscr();
in algorithms so it supports variety of suitable data types and student <int> s;
data structures. s.input();
s.display();
CLASS TEMPLATE student <float> s1;
Following is syntax of class template. s1.input();
template <class T> s1.display();
class class-name getch();
}
{ OUTPUT:
//………… Enter rno : 4
// class members specification Rno : 4
// with anonymous type T Enter rno : 1.3
// whenever appropriate Rno : 1.3
//………… As shown in example first we have pass integer value and
}; then we have pass float value this can be done easily using
template no need to redefine class.
As shown in syntax we can give any valid name in place of T.
Now consider the following class, class student CLASS TEMPLATE WITH MULTIPLE PARAMETERS
We can use more then one generic data types in a class tem-
{ plate. Following is an example which illustrates this.
int rno;
public: #include<iostream.h>
void input() #include<conio.h>
{ template <class T1,class T2,class T3>
cout<<”\n Enter rno:”; class student
cin>>rno; } {
void display() T1 rno;
{ cout<<”\n Rno : “<<rno; } T2 name[20];
}; T3 per;
public:
Here now if I want to change data of rno from int to float void input()
then I have to redefine whole class instead if doing this use {
template. Consider same example using template. cout<<”\n Enter rno,name,per:”;
cin>>rno>>name>>per;
#include<iostream.h> }
#include<conio.h> void display()
template <class ans> {
class student cout<<”\n Name : “<<name;
{ cout<<”\t Rno : “<<rno;
ans rno; cout<<”\t Per : “<<per;
public: }
void input() };
{ void main()
cout<<”\n Enter rno:”; {
cin>>rno; student <int,char,float> s;
} s.input();
void display() s.display();
{ cout<<”\n Rno : “<<rno; } getch();

INDIAN JOURNAL OF APPLIED RESEARCH X 69


Research Paper Volume : 2 | Issue : 1 | October 2012 | ISSN - 2249-555X

} cout<<endl<<a<<” + “<<b;
OUTPUT: cout<<”\t = “<<c;
Enter rno,name,per:1 ans 80 }
Name : ans Rno : 1 Per : 80 void main()
{
FUNCTION TEMPLATE clrscr();
We can also create template for function, following is syntax, sum(5,4.3);
template<class T> sum(1.4,2);
return-type function-name(arguments of type T) getch();
{ }
//………… OUTPUT:
// Body of function 5 + 4.3 = 9.3
// with anonymous type T 1.4 + 2 = 3.4
// whenever appropriate
//………… OVERLOADING TEMPLATE FUNCTIONS
} A template function may be overloaded either by template
This syntax is much similar as template syntax for class. In functions or ordinary functions of its name. In such case com-
place of T we can give any valid name. Following is example. piler find match as follow.
[1] Call an ordinary function that has an exact match.
#include<iostream.h> [2] Call a template function that could be created with an
#include<conio.h> exact match.
template <class t> [3] Try normal overloading resolution to ordinary functions
void display(t a)//function with template and call the one that matches.
{ An error is generated if no match found. Note that no auto-
cout<<”\n Value : “<<a; matic conversions are applied to arguments on the template
} functions. Following is example.
void main()
{ #include<iostream.h>
clrscr(); #include<conio.h>
display(5); template <class t>
display(“ans”); void display(t a)
getch(); { cout<<”\n Value [using template]: “<<a; }
} void display(int b)
OUTPUT: { cout<<”\n Value [without using template]: “<<b; }
Value : 5 void main()
Value : ans {
clrscr();
FUNCTION TEMPLATE WITH MULTIPLE PARAMETERS display(5);
Like template class we can use more than one generic data display(2.5);
type in function template. Following is syntax, display(“hi”);
getch();
template<class T1,class T2> }
return-type function-name(arguments of types T1,T2,…) OUTPUT:
{ // Body of function } Value [without using template]: 5
Value [using template]: 2.5
Following is an example. Value [using template]: hi

#include<iostream.h> CONCLUSION
#include<conio.h> When in any program we need to change our arguments
template <class t1,class t2> data type then template is best option to use. So by using
void sum(t1 a,t2 b) template each time we do not need to rewrite our class or
{ function to change arguments data types.
float c;
c = a + b;

REFERENCES
[1] Object-Oriented Programming With C++ by E Balagurusamy | [2] T. Becker. Type erasure in C++: The glue between object oriented and generic programming.
In K. Davis and J. Striegnitz, editors, Multiparadigm Programming 2007: Proceedings of the MPOOL Workshop at ECOOP'07, July 2007. | [3] J. C. Dehnert and A.
A. Stepanov. Fundamentals of Generic Programming. In Selected Papers from the International Seminar on Generic Programming, pages 1-11, London, UK, 2000.
Springer-Verlag. | [4] Peter Pirkelbauer, Sean Parent, Mat Marcus and Bjarne Stroustrup “Runtime Concepts for the C++ Standard Template Library”

70 X INDIAN JOURNAL OF APPLIED RESEARCH

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy