Template in C ++
The template is the foundation of generic programming, ie code according to which is independent of any specific type.
A Template is a blueprint or method to create a class or a general function. Container libraries like iterators and algorithms are examples of general programming and have been developed using the concept of Template.
Each container has a single definition, vector example, but we can define many different vector series, for example: vector or vector .
Template is a keyword in C ++, we can understand that it is an abstract data type, specific to the basic data types. A template is a keyword that tells the compiler that the following code defines multiple data types and that its source code will be compiled to correspond to each data type during compilation. There are two types of templates in C ++:
Function Template: is a function template that allows you to define general functions for multiple data types.
Class template: is a class template that allows to define general classes for many data types
Function Template in C ++
Here is the general syntax of a Function Template definition in C ++:
template < class kieu_du_lieu > kieu_tham_chieu ten_ham ( danh sach tham so ) { // phan than ham }
Here, kieu_du_lieu is a name of a data type used by the function. This name can be used within the function definition.
The following is an example of Function Template that returns the maximum value of two values:
#include #include using namespace std ; template < typename QTM > inline QTM const & Max ( QTM const & a , QTM const & b ) { return a < b ? b : a ; } int main () { int i = 15 ; int j = 26 ; cout << "Gia tri lon nhat cua (i, j) la: " << Max ( i , j ) << endl ; double f1 = 4.5 ; double f2 = 14.2 ; cout << "Gia tri lon nhat cua (f1, f2) la: " << Max ( f1 , f2 ) << endl ; string s1 = "HocLapTrinhCplusplus" ; string s2 = "TaiQTM" ; cout << "Gia tri lon nhat cua (s1, s2) la: " << Max ( s1 , s2 ) << endl ; return 0 ; }
At the template line, if you use the class instead of typename as in the syntax, the program still runs normally. Because with the class and typename defined in C ++. As for other names, like tenKieuCuaToi , it won't run !!
Compile and run the above C ++ program to see the results:
Class Template in C ++
Just like when we can define Function Template, we can also define the Class Template in C ++. The general syntax of defining a Class Template in C ++ is:
template < class kieu_du_lieu > class ten_lop { . . . }
Here, kieu_du_lieu is the type name, which will be determined when a class is declared. You can define more than one generic data type by using a list separated by commas.
The following example defines the Stack class and implements general methods to push and pop elements from that Stack. (Stack: stack, push: add a new node to the top of the stack, pop: operation takes 1 element from the stack top).
#include #include #include #include #include using namespace std ; template < class QTM > class Stack { private : vector < QTM > phantu ; // cac phan tu public : void push ( QTM const &); // hoat dong push phan tu void pop (); // hoat dong pop phan tu QTM top () const ; // tra ve phan tu tren cung bool empty () const { // tra ve true neu la trong. return phantu . empty (); } }; template < class QTM > void Stack < QTM >:: push ( QTM const & elem ) { // phu them ban sao cua phan tu da truyen phantu . push_back ( elem ); } template < class QTM > void Stack < QTM >:: pop () { if ( phantu . empty ()) { throw out_of_range ( "Stack<>::pop(): stack da trong!" ); } // xoa phan tu cuoi cung phantu . pop_back (); } template < class QTM > QTM Stack < QTM >:: top () const { if ( phantu . empty ()) { throw out_of_range ( "Stack<>::top(): stack da trong!" ); } // tra ve ban sao cua phan tu cuoi cung return phantu . back (); } int main () { try { Stack stackSoNguyen ; // mot stack cua cac so nguyen Stack stackChuoi ; // mot stack cua cac chuoi // thao tac tren stack cac so nguyen stackSoNguyen . push ( 10 ); cout << stackSoNguyen . top () << endl ; // thao tac tren stack cua chuoi stackChuoi . push ( "QTMXinChaoCacBan" ); cout << stackChuoi . top () << std :: endl ; stackChuoi . pop (); stackChuoi . pop (); } catch ( exception const & ex ) { cerr << "Exception: " << ex . what () << endl ; return - 1 ; } }
In the above program, the out_of_range exception is already defined in C ++. Compile and run the above C ++ program to see the results:
According to Tutorialspoint
Previous article: Namespace in C ++
Next article: Preprocessor in C ++
You should read it
- 10 awesome PowerPoint templates make the presentation 'shine'
- How to Use Function Template Parameter Packs in C++
- Lesson 25: Use the Template
- Invite download Profile template Curriculum vitae, job application, ... extremely professional from Microsoft
- The best free Microsoft Word template download websites
- 5 free HTML templates for creating web pages quickly
- Top gorgeous Word cover templates for books, reports, lesson plans
- How to use templates in Adobe Stock
- How to use OneNote template
- 9 great websites to download mockup templates for design
- How to Add Templates in Microsoft Word
- How to create a master template for presentations in Figma
Maybe you are interested
Download standard and beautiful Word file experience initiative cover template
How to fix hidden templates in Google Docs
How to create custom templates in Google Docs
The Best Festival Presentation Slide Templates for Google Slides and PowerPoint on Slidesgo
5 reliable sources of slideshow templates for presentations
Phishing campaign via email, abusing Microsoft Office templates to spread malicious code