logo

C++ šablonai

C++ šablonas yra galinga funkcija, pridėta prie C++. Tai leidžia apibrėžti bendrąsias klases ir bendrąsias funkcijas ir taip palaiko bendrąjį programavimą. Bendrasis programavimas yra metodas, kai bendrieji tipai naudojami kaip algoritmų parametrai, kad jie galėtų dirbti su įvairiais duomenų tipais.

Šablonai gali būti pateikiami dviem būdais:

  • Funkcijų šablonai
  • Klasių šablonai
C++ šablonai

Funkcijų šablonai:

Galime apibrėžti funkcijos šabloną. Pavyzdžiui, jei turime funkciją add(), galime sukurti pridėti funkcijos versijas, kad pridėtume int, float arba double tipo reikšmes.

Klasės šablonas:

Galime apibrėžti klasės šabloną. Pavyzdžiui, masyvo klasei gali būti sukurtas klasės šablonas, kuris gali priimti įvairių tipų masyvus, tokius kaip int masyvas, float masyvas arba dvigubas masyvas.


Funkcijų šablonas

  • Bendrosiose funkcijose naudojama funkcijos šablono sąvoka. Bendrosios funkcijos apibrėžia operacijų, kurios gali būti taikomos įvairių tipų duomenims, rinkinį.
  • Duomenų, kuriais veiks funkcija, tipas priklauso nuo duomenų, perduodamų kaip parametras, tipo.
  • Pavyzdžiui, greito rūšiavimo algoritmas įgyvendinamas naudojant bendrąją funkciją, jis gali būti įgyvendintas sveikųjų skaičių masyve arba slankiųjų masyvų.
  • Bendroji funkcija sukuriama naudojant raktinio žodžio šabloną. Šablonas apibrėžia, kokia funkcija veiks.

Funkcijų šablono sintaksė

 template ret_type func_name(parameter_list) { // body of function. } 

Kur Ttipas : tai funkcijos naudojamo duomenų tipo rezervuotos vietos pavadinimas. Jis naudojamas funkcijos apibrėžime. Tai tik vietos rezervavimo priemonė, kurią kompiliatorius automatiškai pakeis faktiniu duomenų tipu.

klasė : Klasės raktinis žodis naudojamas norint nurodyti bendrą tipą šablono deklaracijoje.

pakeisti iš eilutės java

Pažiūrėkime paprastą funkcijos šablono pavyzdį:

 #include using namespace std; template T add(T &amp;a,T &amp;b) { T result = a+b; return result; } int main() { int i =2; int j =3; float m = 2.3; float n = 1.2; cout&lt;<'addition of i and j is :'< <add(i,j); cout<<'
'; cout<<'addition m n <add(m,n); return 0; } < pre> <p> <strong>Output:</strong> </p> <pre> Addition of i and j is :5 Addition of m and n is :3.5 </pre> <p>In the above example, we create the function template which can perform the addition operation on any type either it can be integer, float or double.</p> <h3>Function Templates with Multiple Parameters</h3> <p>We can use more than one generic type in the template function by using the comma to separate the list.</p> <h2>Syntax</h2> <pre> template return_type function_name (arguments of type T1, T2....) { // body of function. } </pre> <p>In the above syntax, we have seen that the template function can accept any number of arguments of a different type.</p> <p> <strong>Let&apos;s see a simple example:</strong> </p> <pre> #include using namespace std; template void fun(X a,Y b) { std::cout &lt;&lt; &apos;Value of a is : &apos; &lt; <a<< std::endl; std::cout << 'value of b is : ' < <b<< } int main() { fun(15,12.3); return 0; pre> <p> <strong>Output:</strong> </p> <pre> Value of a is : 15 Value of b is : 12.3 </pre> <p>In the above example, we use two generic types in the template function, i.e., X and Y.</p> <h3>Overloading a Function Template</h3> <p>We can overload the generic function means that the overloaded template functions can differ in the parameter list.</p> <p> <strong>Let&apos;s understand this through a simple example:</strong> </p> <pre> #include using namespace std; template void fun(X a) { std::cout &lt;&lt; &apos;Value of a is : &apos; &lt; <a<< std::endl; } template void fun(x b ,y c) { std::cout << 'value of is : ' < <b<< c <<c<< int main() fun(10); fun(20,30.5); return 0; pre> <p> <strong>Output:</strong> </p> <pre> Value of a is : 10 Value of b is : 20 Value of c is : 30.5 </pre> <p>In the above example, template of fun() function is overloaded.</p> <h3>Restrictions of Generic Functions</h3> <p>Generic functions perform the same operation for all the versions of a function except the data type differs. Let&apos;s see a simple example of an overloaded function which cannot be replaced by the generic function as both the functions have different functionalities.</p> <p> <strong>Let&apos;s understand this through a simple example:</strong> </p> <pre> #include using namespace std; void fun(double a) { cout&lt;<'value of a is : '< <a<<'
'; } void fun(int b) { if(b%2="=0)" cout<<'number even'; else odd'; int main() fun(4.6); fun(6); return 0; < pre> <p> <strong>Output:</strong> </p> <pre> value of a is : 4.6 Number is even </pre> <p>In the above example, we overload the ordinary functions. We cannot overload the generic functions as both the functions have different functionalities. First one is displaying the value and the second one determines whether the number is even or not.</p> <hr> <h2>CLASS TEMPLATE</h2> <p> <strong>Class Template</strong> can also be defined similarly to the Function Template. When a class uses the concept of Template, then the class is known as generic class.</p> <h2>Syntax</h2> <pre> template class class_name { . . } </pre> <p> <strong>Ttype</strong> is a placeholder name which will be determined when the class is instantiated. We can define more than one generic data type using a comma-separated list. The Ttype can be used inside the class body.</p> <p>Now, we create an instance of a class</p> <pre> class_name ob; </pre> <p> <strong>where class_name</strong> : It is the name of the class.</p> <p> <strong>type</strong> : It is the type of the data that the class is operating on.</p> <p> <strong>ob</strong> : It is the name of the object.</p> <p> <strong>Let&apos;s see a simple example:</strong> </p> <pre> #include using namespace std; template class A { public: T num1 = 5; T num2 = 6; void add() { std::cout &lt;&lt; &apos;Addition of num1 and num2 : &apos; &lt;&lt; num1+num2&lt;<std::endl; } }; int main() { a d; d.add(); return 0; < pre> <p> <strong>Output:</strong> </p> <pre> Addition of num1 and num2 : 11 </pre> <p>In the above example, we create a template for class A. Inside the main() method, we create the instance of class A named as, &apos;d&apos;.</p> <h3>CLASS TEMPLATE WITH MULTIPLE PARAMETERS</h3> <p>We can use more than one generic data type in a class template, and each generic data type is separated by the comma.</p> <h2>Syntax</h2> <pre> template class class_name { // Body of the class. } </pre> <p> <strong>Let&apos;s see a simple example when class template contains two generic data types.</strong> </p> <pre> #include using namespace std; template class A { T1 a; T2 b; public: A(T1 x,T2 y) { a = x; b = y; } void display() { std::cout &lt;&lt; &apos;Values of a and b are : &apos; &lt;&lt; a&lt;<' ,'< <b<<std::endl; } }; int main() { a d(5,6.5); d.display(); return 0; < pre> <p> <strong>Output:</strong> </p> <pre> Values of a and b are : 5,6.5 </pre> <h3>Nontype Template Arguments</h3> <p>The template can contain multiple arguments, and we can also use the non-type arguments In addition to the type T argument, we can also use other types of arguments such as strings, function names, constant expression and built-in types. <strong>Let&apos; s see the following example:</strong> </p> <pre> template class array { T arr[size]; // automatic array initialization. }; </pre> <p>In the above case, the nontype template argument is size and therefore, template supplies the size of the array as an argument.</p> <p>Arguments are specified when the objects of a class are created:</p> <pre> array t1; // array of 15 integers. array t2; // array of 10 floats. array t3; // array of 4 chars. </pre> <p>Let&apos;s see a simple example of nontype template arguments.</p> <pre> #include using namespace std; template class A { public: T arr[size]; void insert() { int i =1; for (int j=0;j<size;j++) { arr[j]="i;" i++; } void display() for(int i="0;i&lt;size;i++)" std::cout << arr[i] ' '; }; int main() a t1; t1.insert(); t1.display(); return 0; < pre> <p> <strong>Output:</strong> </p> <pre> 1 2 3 4 5 6 7 8 9 10 </pre> <p>In the above example, the class template is created which contains the nontype template argument, i.e., size. It is specified when the object of class &apos;A&apos; is created.</p> <p> <strong>Points to Remember</strong> </p> <ul> <li>C++ supports a powerful feature known as a template to implement the concept of generic programming.</li> <li>A template allows us to create a family of classes or family of functions to handle different data types.</li> <li>Template classes and functions eliminate the code duplication of different data types and thus makes the development easier and faster.</li> <li>Multiple parameters can be used in both class and function template.</li> <li>Template functions can also be overloaded.</li> <li>We can also use nontype arguments such as built-in or derived data types as template arguments.</li> </ul> <br></size;j++)></pre></'></pre></std::endl;></pre></'value></pre></a<<></pre></a<<></pre></'addition>

Aukščiau pateiktame pavyzdyje sukuriame funkcijos šabloną, kuris gali atlikti bet kokio tipo pridėjimo operaciją: sveikasis skaičius, plūduriuojantis arba dvigubas.

Funkcijų šablonai su keliais parametrais

Šablono funkcijoje galime naudoti daugiau nei vieną bendrąjį tipą, naudodami kablelį, kad atskirtume sąrašą.

Sintaksė

 template return_type function_name (arguments of type T1, T2....) { // body of function. } 

Aukščiau pateiktoje sintaksėje matėme, kad šablono funkcija gali priimti bet kokį skaičių skirtingo tipo argumentų.

Pažiūrėkime paprastą pavyzdį:

 #include using namespace std; template void fun(X a,Y b) { std::cout &lt;&lt; &apos;Value of a is : &apos; &lt; <a<< std::endl; std::cout << \'value of b is : \' < <b<< } int main() { fun(15,12.3); return 0; pre> <p> <strong>Output:</strong> </p> <pre> Value of a is : 15 Value of b is : 12.3 </pre> <p>In the above example, we use two generic types in the template function, i.e., X and Y.</p> <h3>Overloading a Function Template</h3> <p>We can overload the generic function means that the overloaded template functions can differ in the parameter list.</p> <p> <strong>Let&apos;s understand this through a simple example:</strong> </p> <pre> #include using namespace std; template void fun(X a) { std::cout &lt;&lt; &apos;Value of a is : &apos; &lt; <a<< std::endl; } template void fun(x b ,y c) { std::cout << \'value of is : \' < <b<< c <<c<< int main() fun(10); fun(20,30.5); return 0; pre> <p> <strong>Output:</strong> </p> <pre> Value of a is : 10 Value of b is : 20 Value of c is : 30.5 </pre> <p>In the above example, template of fun() function is overloaded.</p> <h3>Restrictions of Generic Functions</h3> <p>Generic functions perform the same operation for all the versions of a function except the data type differs. Let&apos;s see a simple example of an overloaded function which cannot be replaced by the generic function as both the functions have different functionalities.</p> <p> <strong>Let&apos;s understand this through a simple example:</strong> </p> <pre> #include using namespace std; void fun(double a) { cout&lt;<\'value of a is : \'< <a<<\'
\'; } void fun(int b) { if(b%2="=0)" cout<<\'number even\'; else odd\'; int main() fun(4.6); fun(6); return 0; < pre> <p> <strong>Output:</strong> </p> <pre> value of a is : 4.6 Number is even </pre> <p>In the above example, we overload the ordinary functions. We cannot overload the generic functions as both the functions have different functionalities. First one is displaying the value and the second one determines whether the number is even or not.</p> <hr> <h2>CLASS TEMPLATE</h2> <p> <strong>Class Template</strong> can also be defined similarly to the Function Template. When a class uses the concept of Template, then the class is known as generic class.</p> <h2>Syntax</h2> <pre> template class class_name { . . } </pre> <p> <strong>Ttype</strong> is a placeholder name which will be determined when the class is instantiated. We can define more than one generic data type using a comma-separated list. The Ttype can be used inside the class body.</p> <p>Now, we create an instance of a class</p> <pre> class_name ob; </pre> <p> <strong>where class_name</strong> : It is the name of the class.</p> <p> <strong>type</strong> : It is the type of the data that the class is operating on.</p> <p> <strong>ob</strong> : It is the name of the object.</p> <p> <strong>Let&apos;s see a simple example:</strong> </p> <pre> #include using namespace std; template class A { public: T num1 = 5; T num2 = 6; void add() { std::cout &lt;&lt; &apos;Addition of num1 and num2 : &apos; &lt;&lt; num1+num2&lt;<std::endl; } }; int main() { a d; d.add(); return 0; < pre> <p> <strong>Output:</strong> </p> <pre> Addition of num1 and num2 : 11 </pre> <p>In the above example, we create a template for class A. Inside the main() method, we create the instance of class A named as, &apos;d&apos;.</p> <h3>CLASS TEMPLATE WITH MULTIPLE PARAMETERS</h3> <p>We can use more than one generic data type in a class template, and each generic data type is separated by the comma.</p> <h2>Syntax</h2> <pre> template class class_name { // Body of the class. } </pre> <p> <strong>Let&apos;s see a simple example when class template contains two generic data types.</strong> </p> <pre> #include using namespace std; template class A { T1 a; T2 b; public: A(T1 x,T2 y) { a = x; b = y; } void display() { std::cout &lt;&lt; &apos;Values of a and b are : &apos; &lt;&lt; a&lt;<\' ,\'< <b<<std::endl; } }; int main() { a d(5,6.5); d.display(); return 0; < pre> <p> <strong>Output:</strong> </p> <pre> Values of a and b are : 5,6.5 </pre> <h3>Nontype Template Arguments</h3> <p>The template can contain multiple arguments, and we can also use the non-type arguments In addition to the type T argument, we can also use other types of arguments such as strings, function names, constant expression and built-in types. <strong>Let&apos; s see the following example:</strong> </p> <pre> template class array { T arr[size]; // automatic array initialization. }; </pre> <p>In the above case, the nontype template argument is size and therefore, template supplies the size of the array as an argument.</p> <p>Arguments are specified when the objects of a class are created:</p> <pre> array t1; // array of 15 integers. array t2; // array of 10 floats. array t3; // array of 4 chars. </pre> <p>Let&apos;s see a simple example of nontype template arguments.</p> <pre> #include using namespace std; template class A { public: T arr[size]; void insert() { int i =1; for (int j=0;j<size;j++) { arr[j]="i;" i++; } void display() for(int i="0;i&lt;size;i++)" std::cout << arr[i] \' \'; }; int main() a t1; t1.insert(); t1.display(); return 0; < pre> <p> <strong>Output:</strong> </p> <pre> 1 2 3 4 5 6 7 8 9 10 </pre> <p>In the above example, the class template is created which contains the nontype template argument, i.e., size. It is specified when the object of class &apos;A&apos; is created.</p> <p> <strong>Points to Remember</strong> </p> <ul> <li>C++ supports a powerful feature known as a template to implement the concept of generic programming.</li> <li>A template allows us to create a family of classes or family of functions to handle different data types.</li> <li>Template classes and functions eliminate the code duplication of different data types and thus makes the development easier and faster.</li> <li>Multiple parameters can be used in both class and function template.</li> <li>Template functions can also be overloaded.</li> <li>We can also use nontype arguments such as built-in or derived data types as template arguments.</li> </ul> <br></size;j++)></pre></\'></pre></std::endl;></pre></\'value></pre></a<<></pre></a<<>

Anksčiau pateiktame pavyzdyje šablono funkcijoje naudojame du bendruosius tipus, ty X ir Y.

Funkcijų šablono perkrovimas

Galime perkrauti bendrąją funkciją reiškia, kad perkrautos šablono funkcijos parametrų sąraše gali skirtis.

Supraskime tai per paprastą pavyzdį:

 #include using namespace std; template void fun(X a) { std::cout &lt;&lt; &apos;Value of a is : &apos; &lt; <a<< std::endl; } template void fun(x b ,y c) { std::cout << \'value of is : \' < <b<< c <<c<< int main() fun(10); fun(20,30.5); return 0; pre> <p> <strong>Output:</strong> </p> <pre> Value of a is : 10 Value of b is : 20 Value of c is : 30.5 </pre> <p>In the above example, template of fun() function is overloaded.</p> <h3>Restrictions of Generic Functions</h3> <p>Generic functions perform the same operation for all the versions of a function except the data type differs. Let&apos;s see a simple example of an overloaded function which cannot be replaced by the generic function as both the functions have different functionalities.</p> <p> <strong>Let&apos;s understand this through a simple example:</strong> </p> <pre> #include using namespace std; void fun(double a) { cout&lt;<\'value of a is : \'< <a<<\'
\'; } void fun(int b) { if(b%2="=0)" cout<<\'number even\'; else odd\'; int main() fun(4.6); fun(6); return 0; < pre> <p> <strong>Output:</strong> </p> <pre> value of a is : 4.6 Number is even </pre> <p>In the above example, we overload the ordinary functions. We cannot overload the generic functions as both the functions have different functionalities. First one is displaying the value and the second one determines whether the number is even or not.</p> <hr> <h2>CLASS TEMPLATE</h2> <p> <strong>Class Template</strong> can also be defined similarly to the Function Template. When a class uses the concept of Template, then the class is known as generic class.</p> <h2>Syntax</h2> <pre> template class class_name { . . } </pre> <p> <strong>Ttype</strong> is a placeholder name which will be determined when the class is instantiated. We can define more than one generic data type using a comma-separated list. The Ttype can be used inside the class body.</p> <p>Now, we create an instance of a class</p> <pre> class_name ob; </pre> <p> <strong>where class_name</strong> : It is the name of the class.</p> <p> <strong>type</strong> : It is the type of the data that the class is operating on.</p> <p> <strong>ob</strong> : It is the name of the object.</p> <p> <strong>Let&apos;s see a simple example:</strong> </p> <pre> #include using namespace std; template class A { public: T num1 = 5; T num2 = 6; void add() { std::cout &lt;&lt; &apos;Addition of num1 and num2 : &apos; &lt;&lt; num1+num2&lt;<std::endl; } }; int main() { a d; d.add(); return 0; < pre> <p> <strong>Output:</strong> </p> <pre> Addition of num1 and num2 : 11 </pre> <p>In the above example, we create a template for class A. Inside the main() method, we create the instance of class A named as, &apos;d&apos;.</p> <h3>CLASS TEMPLATE WITH MULTIPLE PARAMETERS</h3> <p>We can use more than one generic data type in a class template, and each generic data type is separated by the comma.</p> <h2>Syntax</h2> <pre> template class class_name { // Body of the class. } </pre> <p> <strong>Let&apos;s see a simple example when class template contains two generic data types.</strong> </p> <pre> #include using namespace std; template class A { T1 a; T2 b; public: A(T1 x,T2 y) { a = x; b = y; } void display() { std::cout &lt;&lt; &apos;Values of a and b are : &apos; &lt;&lt; a&lt;<\' ,\'< <b<<std::endl; } }; int main() { a d(5,6.5); d.display(); return 0; < pre> <p> <strong>Output:</strong> </p> <pre> Values of a and b are : 5,6.5 </pre> <h3>Nontype Template Arguments</h3> <p>The template can contain multiple arguments, and we can also use the non-type arguments In addition to the type T argument, we can also use other types of arguments such as strings, function names, constant expression and built-in types. <strong>Let&apos; s see the following example:</strong> </p> <pre> template class array { T arr[size]; // automatic array initialization. }; </pre> <p>In the above case, the nontype template argument is size and therefore, template supplies the size of the array as an argument.</p> <p>Arguments are specified when the objects of a class are created:</p> <pre> array t1; // array of 15 integers. array t2; // array of 10 floats. array t3; // array of 4 chars. </pre> <p>Let&apos;s see a simple example of nontype template arguments.</p> <pre> #include using namespace std; template class A { public: T arr[size]; void insert() { int i =1; for (int j=0;j<size;j++) { arr[j]="i;" i++; } void display() for(int i="0;i&lt;size;i++)" std::cout << arr[i] \' \'; }; int main() a t1; t1.insert(); t1.display(); return 0; < pre> <p> <strong>Output:</strong> </p> <pre> 1 2 3 4 5 6 7 8 9 10 </pre> <p>In the above example, the class template is created which contains the nontype template argument, i.e., size. It is specified when the object of class &apos;A&apos; is created.</p> <p> <strong>Points to Remember</strong> </p> <ul> <li>C++ supports a powerful feature known as a template to implement the concept of generic programming.</li> <li>A template allows us to create a family of classes or family of functions to handle different data types.</li> <li>Template classes and functions eliminate the code duplication of different data types and thus makes the development easier and faster.</li> <li>Multiple parameters can be used in both class and function template.</li> <li>Template functions can also be overloaded.</li> <li>We can also use nontype arguments such as built-in or derived data types as template arguments.</li> </ul> <br></size;j++)></pre></\'></pre></std::endl;></pre></\'value></pre></a<<>

Aukščiau pateiktame pavyzdyje funkcijos fun() šablonas yra perkrautas.

Bendrųjų funkcijų apribojimai

Bendrosios funkcijos atlieka tą pačią operaciją visoms funkcijos versijoms, išskyrus duomenų tipą. Pažiūrėkime paprastą perkrautos funkcijos pavyzdį, kurio negalima pakeisti bendra funkcija, nes abi funkcijos turi skirtingas funkcijas.

Supraskime tai per paprastą pavyzdį:

 #include using namespace std; void fun(double a) { cout&lt;<\'value of a is : \'< <a<<\'
\'; } void fun(int b) { if(b%2="=0)" cout<<\'number even\'; else odd\'; int main() fun(4.6); fun(6); return 0; < pre> <p> <strong>Output:</strong> </p> <pre> value of a is : 4.6 Number is even </pre> <p>In the above example, we overload the ordinary functions. We cannot overload the generic functions as both the functions have different functionalities. First one is displaying the value and the second one determines whether the number is even or not.</p> <hr> <h2>CLASS TEMPLATE</h2> <p> <strong>Class Template</strong> can also be defined similarly to the Function Template. When a class uses the concept of Template, then the class is known as generic class.</p> <h2>Syntax</h2> <pre> template class class_name { . . } </pre> <p> <strong>Ttype</strong> is a placeholder name which will be determined when the class is instantiated. We can define more than one generic data type using a comma-separated list. The Ttype can be used inside the class body.</p> <p>Now, we create an instance of a class</p> <pre> class_name ob; </pre> <p> <strong>where class_name</strong> : It is the name of the class.</p> <p> <strong>type</strong> : It is the type of the data that the class is operating on.</p> <p> <strong>ob</strong> : It is the name of the object.</p> <p> <strong>Let&apos;s see a simple example:</strong> </p> <pre> #include using namespace std; template class A { public: T num1 = 5; T num2 = 6; void add() { std::cout &lt;&lt; &apos;Addition of num1 and num2 : &apos; &lt;&lt; num1+num2&lt;<std::endl; } }; int main() { a d; d.add(); return 0; < pre> <p> <strong>Output:</strong> </p> <pre> Addition of num1 and num2 : 11 </pre> <p>In the above example, we create a template for class A. Inside the main() method, we create the instance of class A named as, &apos;d&apos;.</p> <h3>CLASS TEMPLATE WITH MULTIPLE PARAMETERS</h3> <p>We can use more than one generic data type in a class template, and each generic data type is separated by the comma.</p> <h2>Syntax</h2> <pre> template class class_name { // Body of the class. } </pre> <p> <strong>Let&apos;s see a simple example when class template contains two generic data types.</strong> </p> <pre> #include using namespace std; template class A { T1 a; T2 b; public: A(T1 x,T2 y) { a = x; b = y; } void display() { std::cout &lt;&lt; &apos;Values of a and b are : &apos; &lt;&lt; a&lt;<\' ,\'< <b<<std::endl; } }; int main() { a d(5,6.5); d.display(); return 0; < pre> <p> <strong>Output:</strong> </p> <pre> Values of a and b are : 5,6.5 </pre> <h3>Nontype Template Arguments</h3> <p>The template can contain multiple arguments, and we can also use the non-type arguments In addition to the type T argument, we can also use other types of arguments such as strings, function names, constant expression and built-in types. <strong>Let&apos; s see the following example:</strong> </p> <pre> template class array { T arr[size]; // automatic array initialization. }; </pre> <p>In the above case, the nontype template argument is size and therefore, template supplies the size of the array as an argument.</p> <p>Arguments are specified when the objects of a class are created:</p> <pre> array t1; // array of 15 integers. array t2; // array of 10 floats. array t3; // array of 4 chars. </pre> <p>Let&apos;s see a simple example of nontype template arguments.</p> <pre> #include using namespace std; template class A { public: T arr[size]; void insert() { int i =1; for (int j=0;j<size;j++) { arr[j]="i;" i++; } void display() for(int i="0;i&lt;size;i++)" std::cout << arr[i] \' \'; }; int main() a t1; t1.insert(); t1.display(); return 0; < pre> <p> <strong>Output:</strong> </p> <pre> 1 2 3 4 5 6 7 8 9 10 </pre> <p>In the above example, the class template is created which contains the nontype template argument, i.e., size. It is specified when the object of class &apos;A&apos; is created.</p> <p> <strong>Points to Remember</strong> </p> <ul> <li>C++ supports a powerful feature known as a template to implement the concept of generic programming.</li> <li>A template allows us to create a family of classes or family of functions to handle different data types.</li> <li>Template classes and functions eliminate the code duplication of different data types and thus makes the development easier and faster.</li> <li>Multiple parameters can be used in both class and function template.</li> <li>Template functions can also be overloaded.</li> <li>We can also use nontype arguments such as built-in or derived data types as template arguments.</li> </ul> <br></size;j++)></pre></\'></pre></std::endl;></pre></\'value>

Aukščiau pateiktame pavyzdyje mes perkrauname įprastas funkcijas. Negalime perkrauti bendrųjų funkcijų, nes abi funkcijos turi skirtingas funkcijas. Pirmasis rodo reikšmę, o antrasis nustato, ar skaičius yra lyginis, ar ne.


KLASĖS ŠABLONAS

Klasės šablonas taip pat gali būti apibrėžtas panašiai kaip funkcijos šablonas. Kai klasėje naudojama šablono sąvoka, klasė yra žinoma kaip bendroji klasė.

Sintaksė

 template class class_name { . . } 

Ttipas yra rezervuotos vietos pavadinimas, kuris bus nustatytas sukūrus klasę. Naudodami kableliais atskirtą sąrašą galime apibrėžti daugiau nei vieną bendrąjį duomenų tipą. Ttype galima naudoti klasės korpuse.

Dabar sukuriame klasės egzempliorių

 class_name ob; 

kur klasės_pavadinimas : Tai klasės pavadinimas.

tipo : Tai duomenų tipas, kurį naudoja klasė.

adresu : Tai objekto pavadinimas.

Pažiūrėkime paprastą pavyzdį:

 #include using namespace std; template class A { public: T num1 = 5; T num2 = 6; void add() { std::cout &lt;&lt; &apos;Addition of num1 and num2 : &apos; &lt;&lt; num1+num2&lt;<std::endl; } }; int main() { a d; d.add(); return 0; < pre> <p> <strong>Output:</strong> </p> <pre> Addition of num1 and num2 : 11 </pre> <p>In the above example, we create a template for class A. Inside the main() method, we create the instance of class A named as, &apos;d&apos;.</p> <h3>CLASS TEMPLATE WITH MULTIPLE PARAMETERS</h3> <p>We can use more than one generic data type in a class template, and each generic data type is separated by the comma.</p> <h2>Syntax</h2> <pre> template class class_name { // Body of the class. } </pre> <p> <strong>Let&apos;s see a simple example when class template contains two generic data types.</strong> </p> <pre> #include using namespace std; template class A { T1 a; T2 b; public: A(T1 x,T2 y) { a = x; b = y; } void display() { std::cout &lt;&lt; &apos;Values of a and b are : &apos; &lt;&lt; a&lt;<\' ,\'< <b<<std::endl; } }; int main() { a d(5,6.5); d.display(); return 0; < pre> <p> <strong>Output:</strong> </p> <pre> Values of a and b are : 5,6.5 </pre> <h3>Nontype Template Arguments</h3> <p>The template can contain multiple arguments, and we can also use the non-type arguments In addition to the type T argument, we can also use other types of arguments such as strings, function names, constant expression and built-in types. <strong>Let&apos; s see the following example:</strong> </p> <pre> template class array { T arr[size]; // automatic array initialization. }; </pre> <p>In the above case, the nontype template argument is size and therefore, template supplies the size of the array as an argument.</p> <p>Arguments are specified when the objects of a class are created:</p> <pre> array t1; // array of 15 integers. array t2; // array of 10 floats. array t3; // array of 4 chars. </pre> <p>Let&apos;s see a simple example of nontype template arguments.</p> <pre> #include using namespace std; template class A { public: T arr[size]; void insert() { int i =1; for (int j=0;j<size;j++) { arr[j]="i;" i++; } void display() for(int i="0;i&lt;size;i++)" std::cout << arr[i] \' \'; }; int main() a t1; t1.insert(); t1.display(); return 0; < pre> <p> <strong>Output:</strong> </p> <pre> 1 2 3 4 5 6 7 8 9 10 </pre> <p>In the above example, the class template is created which contains the nontype template argument, i.e., size. It is specified when the object of class &apos;A&apos; is created.</p> <p> <strong>Points to Remember</strong> </p> <ul> <li>C++ supports a powerful feature known as a template to implement the concept of generic programming.</li> <li>A template allows us to create a family of classes or family of functions to handle different data types.</li> <li>Template classes and functions eliminate the code duplication of different data types and thus makes the development easier and faster.</li> <li>Multiple parameters can be used in both class and function template.</li> <li>Template functions can also be overloaded.</li> <li>We can also use nontype arguments such as built-in or derived data types as template arguments.</li> </ul> <br></size;j++)></pre></\'></pre></std::endl;>

Aukščiau pateiktame pavyzdyje sukuriame A klasės šabloną. Metodo main() viduje sukuriame A klasės egzempliorių, pavadintą „d“.

KLASĖS ŠABLONAS SU KELIAIS PARAMETRAIS

Klasės šablone galime naudoti daugiau nei vieną bendrąjį duomenų tipą, o kiekvienas bendrasis duomenų tipas atskiriamas kableliu.

Sintaksė

 template class class_name { // Body of the class. } 

Pažiūrėkime paprastą pavyzdį, kai klasės šablone yra du bendrieji duomenų tipai.

 #include using namespace std; template class A { T1 a; T2 b; public: A(T1 x,T2 y) { a = x; b = y; } void display() { std::cout &lt;&lt; &apos;Values of a and b are : &apos; &lt;&lt; a&lt;<\\' ,\\'< <b<<std::endl; } }; int main() { a d(5,6.5); d.display(); return 0; < pre> <p> <strong>Output:</strong> </p> <pre> Values of a and b are : 5,6.5 </pre> <h3>Nontype Template Arguments</h3> <p>The template can contain multiple arguments, and we can also use the non-type arguments In addition to the type T argument, we can also use other types of arguments such as strings, function names, constant expression and built-in types. <strong>Let&apos; s see the following example:</strong> </p> <pre> template class array { T arr[size]; // automatic array initialization. }; </pre> <p>In the above case, the nontype template argument is size and therefore, template supplies the size of the array as an argument.</p> <p>Arguments are specified when the objects of a class are created:</p> <pre> array t1; // array of 15 integers. array t2; // array of 10 floats. array t3; // array of 4 chars. </pre> <p>Let&apos;s see a simple example of nontype template arguments.</p> <pre> #include using namespace std; template class A { public: T arr[size]; void insert() { int i =1; for (int j=0;j<size;j++) { arr[j]="i;" i++; } void display() for(int i="0;i&lt;size;i++)" std::cout << arr[i] \\' \\'; }; int main() a t1; t1.insert(); t1.display(); return 0; < pre> <p> <strong>Output:</strong> </p> <pre> 1 2 3 4 5 6 7 8 9 10 </pre> <p>In the above example, the class template is created which contains the nontype template argument, i.e., size. It is specified when the object of class &apos;A&apos; is created.</p> <p> <strong>Points to Remember</strong> </p> <ul> <li>C++ supports a powerful feature known as a template to implement the concept of generic programming.</li> <li>A template allows us to create a family of classes or family of functions to handle different data types.</li> <li>Template classes and functions eliminate the code duplication of different data types and thus makes the development easier and faster.</li> <li>Multiple parameters can be used in both class and function template.</li> <li>Template functions can also be overloaded.</li> <li>We can also use nontype arguments such as built-in or derived data types as template arguments.</li> </ul> <br></size;j++)></pre></\\'>

Netipiniai šablono argumentai

Šablone gali būti keli argumentai, taip pat galime naudoti ne tipo argumentus. Be T tipo argumento, galime naudoti ir kitų tipų argumentus, tokius kaip eilutės, funkcijų pavadinimai, pastovi išraiška ir įtaisytieji tipai. Pažiūrėkime šį pavyzdį:

 template class array { T arr[size]; // automatic array initialization. }; 

Pirmiau nurodytu atveju ne tipo šablono argumentas yra dydis, todėl šablonas kaip argumentą pateikia masyvo dydį.

Argumentai nurodomi, kai sukuriami klasės objektai:

 array t1; // array of 15 integers. array t2; // array of 10 floats. array t3; // array of 4 chars. 

Pažiūrėkime paprastą nontype šablono argumentų pavyzdį.

 #include using namespace std; template class A { public: T arr[size]; void insert() { int i =1; for (int j=0;j<size;j++) { arr[j]="i;" i++; } void display() for(int i="0;i&lt;size;i++)" std::cout << arr[i] \\' \\'; }; int main() a t1; t1.insert(); t1.display(); return 0; < pre> <p> <strong>Output:</strong> </p> <pre> 1 2 3 4 5 6 7 8 9 10 </pre> <p>In the above example, the class template is created which contains the nontype template argument, i.e., size. It is specified when the object of class &apos;A&apos; is created.</p> <p> <strong>Points to Remember</strong> </p> <ul> <li>C++ supports a powerful feature known as a template to implement the concept of generic programming.</li> <li>A template allows us to create a family of classes or family of functions to handle different data types.</li> <li>Template classes and functions eliminate the code duplication of different data types and thus makes the development easier and faster.</li> <li>Multiple parameters can be used in both class and function template.</li> <li>Template functions can also be overloaded.</li> <li>We can also use nontype arguments such as built-in or derived data types as template arguments.</li> </ul> <br></size;j++)>

Aukščiau pateiktame pavyzdyje sukuriamas klasės šablonas, kuriame yra ne tipo šablono argumentas, ty dydis. Jis nurodomas, kai sukuriamas „A“ klasės objektas.

Taškai, kuriuos reikia prisiminti

  • C++ palaiko galingą funkciją, žinomą kaip šablonas, skirtas įgyvendinti bendrojo programavimo koncepciją.
  • Šablonas leidžia mums sukurti klasių ar funkcijų šeimą, kad galėtume tvarkyti skirtingus duomenų tipus.
  • Šablonų klasės ir funkcijos pašalina skirtingų duomenų tipų kodų dubliavimą ir taip palengvina ir pagreitina kūrimą.
  • Klasėje ir funkcijos šablone gali būti naudojami keli parametrai.
  • Šablonų funkcijos taip pat gali būti perkrautos.
  • Taip pat galime naudoti netipinius argumentus, pvz., integruotus arba išvestinius duomenų tipus kaip šablono argumentus.