logo

Modèles C++

Un modèle C++ est une fonctionnalité puissante ajoutée au C++. Il permet de définir les classes génériques et les fonctions génériques et prend ainsi en charge la programmation générique. La programmation générique est une technique dans laquelle des types génériques sont utilisés comme paramètres dans des algorithmes afin qu'ils puissent fonctionner avec une variété de types de données.

Les modèles peuvent être représentés de deux manières :

chaîne trouver c++
  • Modèles de fonctions
  • Modèles de cours
Modèles C++

Modèles de fonctions :

Nous pouvons définir un modèle pour une fonction. Par exemple, si nous avons une fonction add(), nous pouvons créer des versions de la fonction add pour ajouter les valeurs de type int, float ou double.

Modèle de classe :

Nous pouvons définir un modèle pour une classe. Par exemple, un modèle de classe peut être créé pour la classe array qui peut accepter le tableau de différents types tels qu'un tableau int, un tableau flottant ou un tableau double.


Modèle de fonction

  • Les fonctions génériques utilisent le concept de modèle de fonction. Les fonctions génériques définissent un ensemble d'opérations pouvant être appliquées aux différents types de données.
  • Le type de données sur lesquelles la fonction fonctionnera dépend du type de données passées en paramètre.
  • Par exemple, l'algorithme de tri rapide est implémenté à l'aide d'une fonction générique, il peut être implémenté sur un tableau d'entiers ou un tableau de flottants.
  • Une fonction générique est créée à l'aide du modèle de mot-clé. Le modèle définit ce que la fonction fera.

Syntaxe du modèle de fonction

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

Type T : Il s'agit d'un nom d'espace réservé pour un type de données utilisé par la fonction. Il est utilisé dans la définition de la fonction. Il s'agit uniquement d'un espace réservé que le compilateur remplacera automatiquement cet espace réservé par le type de données réel.

classe : Un mot-clé class est utilisé pour spécifier un type générique dans une déclaration de modèle.

Voyons un exemple simple de modèle de fonction :

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

Dans l'exemple ci-dessus, nous créons le modèle de fonction qui peut effectuer l'opération d'addition sur n'importe quel type, qu'il soit entier, flottant ou double.

Modèles de fonctions avec plusieurs paramètres

Nous pouvons utiliser plusieurs types génériques dans la fonction modèle en utilisant la virgule pour séparer la liste.

Syntaxe

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

Dans la syntaxe ci-dessus, nous avons vu que la fonction modèle peut accepter n'importe quel nombre d'arguments d'un type différent.

1 milliard en millions

Voyons un exemple simple :

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

Dans l'exemple ci-dessus, nous utilisons deux types génériques dans la fonction modèle, c'est-à-dire X et Y.

Surcharge d'un modèle de fonction

Nous pouvons surcharger la fonction générique signifie que les fonctions de modèle surchargées peuvent différer dans la liste des paramètres.

Comprenons cela à travers un exemple simple :

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

Dans l'exemple ci-dessus, le modèle de la fonction fun() est surchargé.

Architecture de réseau

Restrictions des fonctions génériques

Les fonctions génériques effectuent la même opération pour toutes les versions d'une fonction, sauf que le type de données diffère. Voyons un exemple simple d'une fonction surchargée qui ne peut pas être remplacée par la fonction générique car les deux fonctions ont des fonctionnalités différentes.

Comprenons cela à travers un exemple simple :

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

Dans l’exemple ci-dessus, nous surchargeons les fonctions ordinaires. Nous ne pouvons pas surcharger les fonctions génériques car les deux fonctions ont des fonctionnalités différentes. Le premier affiche la valeur et le second détermine si le nombre est pair ou non.


MODÈLE DE CLASSE

Modèle de classe peut également être défini de la même manière que le modèle de fonction. Lorsqu'une classe utilise le concept de modèle, elle est alors appelée classe générique.

Syntaxe

 template class class_name { . . } 

Type T est un nom d'espace réservé qui sera déterminé lors de l'instanciation de la classe. Nous pouvons définir plusieurs types de données génériques à l'aide d'une liste séparée par des virgules. Le Ttype peut être utilisé dans le corps de la classe.

chaîne en json java

Maintenant, nous créons une instance d'une classe

 class_name ob; 

où nom_classe : C'est le nom de la classe.

taper : C'est le type de données sur lesquelles la classe opère.

à : C'est le nom de l'objet.

Voyons un exemple simple :

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

Dans l'exemple ci-dessus, nous créons un modèle pour la classe A. Dans la méthode main(), nous créons l'instance de la classe A nommée « d ».

MODÈLE DE CLASSE AVEC PLUSIEURS PARAMÈTRES

Nous pouvons utiliser plusieurs types de données génériques dans un modèle de classe, et chaque type de données génériques est séparé par une virgule.

Syntaxe

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

Voyons un exemple simple lorsque le modèle de classe contient deux types de données génériques.

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

Arguments de modèle non-type

Le modèle peut contenir plusieurs arguments et nous pouvons également utiliser des arguments non-types. En plus de l'argument de type T, nous pouvons également utiliser d'autres types d'arguments tels que des chaînes, des noms de fonctions, des expressions constantes et des types intégrés. Voyons l'exemple suivant :

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

Dans le cas ci-dessus, l'argument du modèle non type est size et, par conséquent, le modèle fournit la taille du tableau comme argument.

Les arguments sont spécifiés lors de la création des objets d'une classe :

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

Voyons un exemple simple d'arguments de modèle non-type.

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

Dans l'exemple ci-dessus, le modèle de classe est créé et contient l'argument de modèle non-type, c'est-à-dire la taille. Il est spécifié lors de la création de l'objet de classe 'A'.

Points à retenir

  • C++ prend en charge une fonctionnalité puissante appelée modèle pour implémenter le concept de programmation générique.
  • Un modèle nous permet de créer une famille de classes ou une famille de fonctions pour gérer différents types de données.
  • Les classes et fonctions de modèles éliminent la duplication de code de différents types de données et rendent ainsi le développement plus facile et plus rapide.
  • Plusieurs paramètres peuvent être utilisés dans le modèle de classe et de fonction.
  • Les fonctions des modèles peuvent également être surchargées.
  • Nous pouvons également utiliser des arguments non types tels que des types de données intégrés ou dérivés comme arguments de modèle.