logo

C++-Vorlagen

Eine C++-Vorlage ist eine leistungsstarke Funktion, die zu C++ hinzugefügt wurde. Es ermöglicht die Definition der generischen Klassen und generischen Funktionen und bietet somit Unterstützung für die generische Programmierung. Generische Programmierung ist eine Technik, bei der generische Typen als Parameter in Algorithmen verwendet werden, damit sie für eine Vielzahl von Datentypen funktionieren können.

np.einzigartig

Vorlagen können auf zwei Arten dargestellt werden:

  • Funktionsvorlagen
  • Klassenvorlagen
C++-Vorlagen

Funktionsvorlagen:

Wir können eine Vorlage für eine Funktion definieren. Wenn wir beispielsweise eine Funktion add() haben, können wir Versionen der Funktion add zum Hinzufügen der Werte vom Typ int, float oder double erstellen.

Klassenvorlage:

Wir können eine Vorlage für eine Klasse definieren. Beispielsweise kann eine Klassenvorlage für die Array-Klasse erstellt werden, die Arrays verschiedener Typen wie Int-Array, Float-Array oder Double-Array akzeptieren kann.


Funktionsvorlage

  • Generische Funktionen nutzen das Konzept einer Funktionsvorlage. Generische Funktionen definieren eine Reihe von Operationen, die auf die verschiedenen Datentypen angewendet werden können.
  • Die Art der Daten, mit denen die Funktion arbeitet, hängt von der Art der als Parameter übergebenen Daten ab.
  • Der Schnellsortierungsalgorithmus wird beispielsweise mithilfe einer generischen Funktion implementiert. Er kann in ein Array von Ganzzahlen oder ein Array von Gleitkommazahlen implementiert werden.
  • Mithilfe der Schlüsselwortvorlage wird eine generische Funktion erstellt. Die Vorlage definiert, welche Funktion ausgeführt wird.

Syntax der Funktionsvorlage

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

Wo Ttyp : Es handelt sich um einen Platzhalternamen für einen Datentyp, der von der Funktion verwendet wird. Es wird innerhalb der Funktionsdefinition verwendet. Es handelt sich lediglich um einen Platzhalter, den der Compiler automatisch durch den tatsächlichen Datentyp ersetzt.

Klasse : Ein Klassenschlüsselwort wird verwendet, um einen generischen Typ in einer Vorlagendeklaration anzugeben.

Sehen wir uns ein einfaches Beispiel einer Funktionsvorlage an:

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

Im obigen Beispiel erstellen wir die Funktionsvorlage, die die Additionsoperation für jeden Typ ausführen kann, sei es Integer, Float oder Double.

Funktionsvorlagen mit mehreren Parametern

Wir können mehr als einen generischen Typ in der Vorlagenfunktion verwenden, indem wir die Liste durch Kommas trennen.

Syntax

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

In der obigen Syntax haben wir gesehen, dass die Vorlagenfunktion eine beliebige Anzahl von Argumenten unterschiedlichen Typs akzeptieren kann.

Sehen wir uns ein einfaches Beispiel an:

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

Im obigen Beispiel verwenden wir zwei generische Typen in der Vorlagenfunktion, nämlich X und Y.

Überladen einer Funktionsvorlage

Wir können die generische Funktion überladen, was bedeutet, dass sich die überladenen Vorlagenfunktionen in der Parameterliste unterscheiden können.

Lassen Sie uns dies anhand eines einfachen Beispiels verstehen:

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

Im obigen Beispiel ist die Vorlage der Funktion fun() überladen.

Einschränkungen allgemeiner Funktionen

Generische Funktionen führen den gleichen Vorgang für alle Versionen einer Funktion aus, außer dass sich der Datentyp unterscheidet. Sehen wir uns ein einfaches Beispiel einer überladenen Funktion an, die nicht durch die generische Funktion ersetzt werden kann, da beide Funktionen unterschiedliche Funktionalitäten haben.

Lassen Sie uns dies anhand eines einfachen Beispiels verstehen:

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

Im obigen Beispiel überladen wir die gewöhnlichen Funktionen. Wir können die generischen Funktionen nicht überladen, da beide Funktionen unterschiedliche Funktionalitäten haben. Der erste zeigt den Wert an und der zweite bestimmt, ob die Zahl gerade ist oder nicht.


KLASSENVORLAGE

Klassenvorlage kann auch ähnlich wie die Funktionsvorlage definiert werden. Wenn eine Klasse das Konzept der Vorlage verwendet, wird die Klasse als generische Klasse bezeichnet.

Syntax

 template class class_name { . . } 

Ttyp ist ein Platzhaltername, der bei der Instanziierung der Klasse festgelegt wird. Mithilfe einer durch Kommas getrennten Liste können wir mehr als einen generischen Datentyp definieren. Der Ttype kann innerhalb des Klassenkörpers verwendet werden.

Jetzt erstellen wir eine Instanz einer Klasse

 class_name ob; 

wo Klassenname : Es ist der Name der Klasse.

Typ : Dies ist der Datentyp, mit dem die Klasse arbeitet.

bei : Es ist der Name des Objekts.

Chr-Funktion Python

Sehen wir uns ein einfaches Beispiel an:

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

Im obigen Beispiel erstellen wir eine Vorlage für Klasse A. Innerhalb der main()-Methode erstellen wir die Instanz von Klasse A mit dem Namen „d“.

KLASSENVORLAGE MIT MEHREREN PARAMETERN

Wir können in einer Klassenvorlage mehr als einen generischen Datentyp verwenden, und jeder generische Datentyp wird durch Komma getrennt.

Ex des Benutzernamens

Syntax

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

Sehen wir uns ein einfaches Beispiel an, bei dem die Klassenvorlage zwei generische Datentypen enthält.

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

Nichttyp-Vorlagenargumente

Die Vorlage kann mehrere Argumente enthalten, und wir können auch Nicht-Typ-Argumente verwenden. Zusätzlich zum Argument vom Typ T können wir auch andere Argumenttypen wie Zeichenfolgen, Funktionsnamen, Konstantenausdrücke und integrierte Typen verwenden. Sehen wir uns das folgende Beispiel an:

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

Im obigen Fall ist das Nichttyp-Vorlagenargument „size“ und daher liefert „template“ die Größe des Arrays als Argument.

Beim Erstellen der Objekte einer Klasse werden Argumente angegeben:

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

Sehen wir uns ein einfaches Beispiel für nicht typisierte Vorlagenargumente an.

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

Im obigen Beispiel wird die Klassenvorlage erstellt, die das Nichttyp-Vorlagenargument, d. h. Größe, enthält. Es wird angegeben, wenn das Objekt der Klasse „A“ erstellt wird.

Punkte, die man sich merken sollte

  • C++ unterstützt eine leistungsstarke Funktion, die als Vorlage bekannt ist, um das Konzept der generischen Programmierung umzusetzen.
  • Eine Vorlage ermöglicht es uns, eine Familie von Klassen oder Funktionen zu erstellen, um verschiedene Datentypen zu verarbeiten.
  • Vorlagenklassen und -funktionen eliminieren die Codeduplizierung verschiedener Datentypen und machen so die Entwicklung einfacher und schneller.
  • Sowohl in der Klassen- als auch in der Funktionsvorlage können mehrere Parameter verwendet werden.
  • Auch Template-Funktionen können überladen sein.
  • Wir können auch Nicht-Typ-Argumente wie integrierte oder abgeleitete Datentypen als Vorlagenargumente verwenden.