logo

C++-sjablonen

Een C++-sjabloon is een krachtige functie die aan C++ is toegevoegd. Hiermee kunt u de generieke klassen en generieke functies definiëren en wordt zo ondersteuning geboden voor generieke programmering. Generiek programmeren is een techniek waarbij generieke typen als parameters in algoritmen worden gebruikt, zodat ze voor verschillende gegevenstypen kunnen werken.

Sjablonen kunnen op twee manieren worden weergegeven:

  • Functiesjablonen
  • Klasse-sjablonen
C++-sjablonen

Functiesjablonen:

We kunnen een sjabloon voor een functie definiëren. Als we bijvoorbeeld een add()-functie hebben, kunnen we versies van de add-functie maken voor het toevoegen van de int-, float- of double-typewaarden.

lezen uit een csv-bestand in Java

Klassensjabloon:

We kunnen een sjabloon voor een klasse definiëren. Er kan bijvoorbeeld een klassensjabloon worden gemaakt voor de array-klasse die de array van verschillende typen kan accepteren, zoals int array, float array of double array.


Functiesjabloon

  • Generieke functies gebruiken het concept van een functiesjabloon. Generieke functies definiëren een reeks bewerkingen die op de verschillende soorten gegevens kunnen worden toegepast.
  • Het type gegevens waarop de functie zal werken, hangt af van het type gegevens dat als parameter wordt doorgegeven.
  • Het Quick sorting-algoritme wordt bijvoorbeeld geïmplementeerd met behulp van een generieke functie. Het kan worden geïmplementeerd in een array van gehele getallen of een array van floats.
  • Er wordt een algemene functie gemaakt met behulp van de trefwoordsjabloon. De sjabloon definieert welke functie zal doen.

Syntaxis van functiesjabloon

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

Waar Ttype : Het is een tijdelijke aanduiding voor een gegevenstype dat door de functie wordt gebruikt. Het wordt gebruikt binnen de functiedefinitie. Het is slechts een tijdelijke aanduiding; de compiler zal deze tijdelijke aanduiding automatisch vervangen door het daadwerkelijke gegevenstype.

klas : Een class-trefwoord wordt gebruikt om een ​​algemeen type in een sjabloondeclaratie op te geven.

Laten we een eenvoudig voorbeeld van een functiesjabloon bekijken:

 #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>

In het bovenstaande voorbeeld maken we de functiesjabloon die de optelbewerking op elk type kan uitvoeren: geheel getal, zwevend of dubbel.

Functiesjablonen met meerdere parameters

We kunnen meer dan één algemeen type in de sjabloonfunctie gebruiken door de komma te gebruiken om de lijst te scheiden.

Syntaxis

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

In de bovenstaande syntaxis hebben we gezien dat de sjabloonfunctie een willekeurig aantal argumenten van een ander type kan accepteren.

Laten we een eenvoudig voorbeeld bekijken:

 #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<<>

In het bovenstaande voorbeeld gebruiken we twee algemene typen in de sjabloonfunctie, namelijk X en Y.

Overbelasting van een functiesjabloon

We kunnen de generieke functie overbelasten, wat betekent dat de overbelaste sjabloonfuncties kunnen verschillen in de parameterlijst.

Laten we dit begrijpen aan de hand van een eenvoudig voorbeeld:

 #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<<>

In het bovenstaande voorbeeld is de sjabloon van de functie fun() overbelast.

Beperkingen van generieke functies

Algemene functies voeren dezelfde bewerking uit voor alle versies van een functie, behalve dat het gegevenstype verschilt. Laten we een eenvoudig voorbeeld bekijken van een overbelaste functie die niet kan worden vervangen door de generieke functie, omdat beide functies verschillende functionaliteiten hebben.

Laten we dit begrijpen aan de hand van een eenvoudig voorbeeld:

 #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>

In het bovenstaande voorbeeld overbelasten we de gewone functies. We kunnen de generieke functies niet overbelasten, omdat beide functies verschillende functionaliteiten hebben. De eerste geeft de waarde weer en de tweede bepaalt of het getal even is of niet.


KLASSE SJABLOON

Klasse sjabloon kan ook op dezelfde manier worden gedefinieerd als het functiesjabloon. Wanneer een klasse het concept Sjabloon gebruikt, staat de klasse bekend als generieke klasse.

Syntaxis

 template class class_name { . . } 

Ttype is een tijdelijke aanduiding voor de naam die wordt bepaald wanneer de klasse wordt geïnstantieerd. We kunnen meer dan één algemeen gegevenstype definiëren met behulp van een door komma's gescheiden lijst. Het Ttype kan binnen de klassenbody worden gebruikt.

Nu maken we een instantie van een klasse

 class_name ob; 

waar klasse_naam : Het is de naam van de klasse.

type : Het is het type gegevens waarop de klasse werkt.

bij : Het is de naam van het object.

Laten we een eenvoudig voorbeeld bekijken:

 #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;>

In het bovenstaande voorbeeld maken we een sjabloon voor klasse A. Binnen de methode main() maken we de instantie van klasse A met de naam 'd'.

KLASSE SJABLOON MET MEERDERE PARAMETERS

We kunnen meer dan één algemeen gegevenstype in een klassensjabloon gebruiken, en elk algemeen gegevenstype wordt gescheiden door een komma.

Syntaxis

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

Laten we een eenvoudig voorbeeld bekijken waarin de klassensjabloon twee algemene gegevenstypen bevat.

 #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></\\'>

Nontype-sjabloonargumenten

De sjabloon kan meerdere argumenten bevatten, en we kunnen ook de niet-type argumenten gebruiken. Naast het type T-argument kunnen we ook andere typen argumenten gebruiken, zoals tekenreeksen, functienamen, constante expressie en ingebouwde typen. Laten we het volgende voorbeeld bekijken:

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

In het bovenstaande geval is het nontype-sjabloonargument grootte en daarom levert de sjabloon de grootte van de array als argument.

Argumenten worden gespecificeerd wanneer de objecten van een klasse worden gemaakt:

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

Laten we een eenvoudig voorbeeld bekijken van nontype-sjabloonargumenten.

 #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++)>

In het bovenstaande voorbeeld wordt de klassensjabloon gemaakt die het nontype-sjabloonargument bevat, d.w.z. grootte. Het wordt gespecificeerd wanneer het object van klasse 'A' wordt gemaakt.

Punten om te onthouden

  • C++ ondersteunt een krachtige functie die bekend staat als een sjabloon om het concept van generiek programmeren te implementeren.
  • Met een sjabloon kunnen we een familie van klassen of functies creëren om verschillende gegevenstypen te verwerken.
  • Sjabloonklassen en -functies elimineren de codeduplicatie van verschillende gegevenstypen en maken zo de ontwikkeling eenvoudiger en sneller.
  • Er kunnen meerdere parameters worden gebruikt in zowel de klasse- als de functiesjabloon.
  • Sjabloonfuncties kunnen ook overbelast raken.
  • We kunnen ook niet-type-argumenten, zoals ingebouwde of afgeleide gegevenstypen, als sjabloonargumenten gebruiken.