0% found this document useful (0 votes)
246 views16 pages

Understanding C++ Template Basics

Templates allow defining generic classes and functions that can work with multiple data types. Some key points: - Function templates define functions that can accept different data types as parameters specified using a template parameter like <class T>. - Class templates define generic classes that can work with different data types specified as template parameters. - Template functions and classes can be overloaded like regular functions and classes. - Member functions of class templates can be defined outside the class using the class name and scope resolution operator. - Non-template friend functions of class templates need the template parameter specified to access members of the template class.

Uploaded by

Amit Kumar
Copyright
© Attribution Non-Commercial (BY-NC)
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)
246 views16 pages

Understanding C++ Template Basics

Templates allow defining generic classes and functions that can work with multiple data types. Some key points: - Function templates define functions that can accept different data types as parameters specified using a template parameter like <class T>. - Class templates define generic classes that can work with different data types specified as template parameters. - Template functions and classes can be overloaded like regular functions and classes. - Member functions of class templates can be defined outside the class using the class name and scope resolution operator. - Non-template friend functions of class templates need the template parameter specified to access members of the template class.

Uploaded by

Amit Kumar
Copyright
© Attribution Non-Commercial (BY-NC)
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

TEMPLATES

WhatisTemplate???
Template isanaddedfeaturewhichcreates anddefinesgenericclassandgeneric functions,andhenceitsalsoreferredas genericprogramming.

FunctionTemplates
Syntax:
template<classuserdefineddatatype> returntypefunctionname(listofarguments) { Bodyofthefunction }

Example:
template<classT> () voidfun() { cout <<x; }

FindtheSquareofanumber
template p <classT> Tsquare(Tx) { returnx*x; } int main() { int a=10,b; b=square( (a); ) cout<<b; floatp=2.5,q; q=square(p); cout<<q; }

Output: 100 6 25 6.25

Swappingoftwovalues
template<class p T> voidswap(T&x,T&y) { Ttemp; temp=x; x=y; y=temp; } int main() { int a=10,b=20; cout<<Beforeswapping::<<a<<<<b; swap(a,b); cout<<endl; cout<<After Af swapping:: i <<a<< <<b; b cout<<endl; floatm=12.35,n=7.28; pp g <<m<<<<n; ; cout<<Beforeswapping:: swap(m,n); cout<<endl; cout<<Afterswapping::<<m<<<<n; }

Output: BeforeSwapping::1020 AfterSwapping::2010 B f S Before Swapping::12.35 i 12 357.28 7 28 AfterSwapping::7.2812.35

FunctionTemplateswithmultiple parameters
Syntax:
template<classT1,classT2,> returntypefunctionname(ArgumentstypeofT1,T2,) { //Bodyofthefunction }

Example
template<classT1,classT2> voidstdinfo (T1sno ,T2mark) { cout<<sno<<<<mark; } int main() { stdinfo(S001,72); stdinfo(12.55,67); }

Output S00172 12.5567

ClassTemplate
Syntax:
template<classT> classClass Name { private: <M.V.>; public: <M.F.>; };

Sumoftwodatausinggenericclass
template<classT> classSample { Tx,y,z; public: voidget() { cout <<Entertwovaluesofxandy:: <<endl; cin>>x>>y; } voidsum() { z=x+y; cout<<z; } }; voidmain() { Sample<int>s1; Sample<float>s2; s1.get(); s1.sum(); s2.get(); s2.sum(); }

ClassTemplatewithMultiple Parameters
Syntax:
template<class T1,class T2,> class Class - Name { //Body of the Class };

Example
template<classT1,classT2> classSample { T1x;T2y; public: Sample(T1a,T2b) { x=a;y=b; } voidshow() { cout<<x<<<<y; cout<<x<< <<y; } }; voidmain() { Sample<int ,float>s1(22,12.33); Sample<float,char>s2(12.44,c) ; s1.show(); s2.show(); }

OverLoading ofTemplateFunctions
Atemplatefunctionmaybe overloadedeitherbytemplatefunctionsor ordinaryfunctionsofitname.Insuch situationstheoverloadingresolutionisdone asfollows: 1) Callanordinaryfunctionthathasanexact match. 2) Callatemplatefunctionthatcouldbe createdwithanexactmatch.

Example
template<classT> voiddisp(Tx) { cout<<GenericFunction<<x; } voiddisp(int y) { cout<<Ordinary cout<< OrdinaryFunction<<y; <<y; } voidmain() { disp(100); disp(13.22); disp(Z); disp( Z ); }

Output OrdinaryFunction100 Generic Function13.22 Generic FunctionZ

MemberFunctionTemplate
Whenwecreateaclasstemplatewe candefinethememberfunctionoftheclass outsidebyusingscoperesolutionoperator. operator Syntax:
t template<class l t < l T> T returntypeclassname<T>::functionname(ArgumentList) { //Bodyoftheclass }

Example
template<classT> classSample { Tx,y,z; public: voidget(T,T); voidshow(); }; template<classT> voidSample<T>::get(Ta,Tb) { x=a; y=b; } template<class p T> voidSample<T>::show() { cout <<x+y;endl ; } Voidmain() { Sample<int>s1; Sample<float>s2; p ; s1.get(7,5); s1.show(); s2.get(12.33,66.88); s2.show(); }

UsageoffriendfunctioninTemplate
template<class p T> ClassSample { Tx; friendvoiddisp(Sample<T>) }; template<classT> voiddisp(Sample<T>obj) p( p j) { Ty; cout<<Enter cout<< EntertheValue:: Value::; ; cin>>obj .x; y=obj .x; cout<<y; } int main() () { Sample<int>s1; Sample<char>s2; disp (s1); disp (s2); }

You might also like