C++ şablonu, C++'a eklenen güçlü bir özelliktir. Genel sınıfları ve genel işlevleri tanımlamanıza olanak tanır ve böylece genel programlama için destek sağlar. Genel programlama, çeşitli veri türleri için çalışabilmeleri için genel türlerin algoritmalarda parametre olarak kullanıldığı bir tekniktir.
Şablonlar iki şekilde temsil edilebilir:
- Fonksiyon şablonları
- Sınıf şablonları
İşlev Şablonları:
Bir fonksiyon için şablon tanımlayabiliriz. Örneğin, bir add() fonksiyonumuz varsa int, float veya double tipi değerleri eklemek için add fonksiyonunun versiyonlarını oluşturabiliriz.
Sınıf Şablonu:
Bir sınıf için şablon tanımlayabiliriz. Örneğin dizi sınıfı için int dizi, float dizi veya double dizi gibi çeşitli türlerdeki dizileri kabul edebilen bir sınıf şablonu oluşturulabilir.
İşlev Şablonu
- Genel işlevler, işlev şablonu kavramını kullanır. Genel işlevler, çeşitli veri türlerine uygulanabilecek bir dizi işlemi tanımlar.
- Fonksiyonun üzerinde çalışacağı veri türü, parametre olarak iletilen verinin türüne bağlıdır.
- Örneğin, Hızlı sıralama algoritması genel bir işlev kullanılarak uygulanır; bir tam sayı dizisine veya kayan nokta dizisine uygulanabilir.
- Anahtar kelime şablonu kullanılarak bir Genel işlev oluşturulur. Şablon, fonksiyonun ne yapacağını tanımlar.
İşlev Şablonunun Sözdizimi
template ret_type func_name(parameter_list) { // body of function. }
Nerede Tip türü : İşlev tarafından kullanılan veri türü için yer tutucu adıdır. Fonksiyon tanımı içerisinde kullanılır. Derleyicinin bu yer tutucuyu otomatik olarak gerçek veri türüyle değiştireceği yalnızca bir yer tutucudur.
sınıf : Bir şablon bildiriminde genel bir türü belirtmek için bir sınıf anahtar sözcüğü kullanılır.
Basit bir fonksiyon şablonu örneğini görelim:
#include using namespace std; template T add(T &a,T &b) { T result = a+b; return result; } int main() { int i =2; int j =3; float m = 2.3; float n = 1.2; cout<<'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's see a simple example:</strong> </p> <pre> #include using namespace std; template void fun(X a,Y b) { std::cout << 'Value of a is : ' < <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's understand this through a simple example:</strong> </p> <pre> #include using namespace std; template void fun(X a) { std::cout << 'Value of a is : ' < <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'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's understand this through a simple example:</strong> </p> <pre> #include using namespace std; void fun(double a) { cout<<'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'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 << 'Addition of num1 and num2 : ' << num1+num2<<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, 'd'.</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'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 << 'Values of a and b are : ' << a<<' ,'< <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' 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'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<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 'A' 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>
Yukarıdaki örnekte integer, float veya double olsun her tipte toplama işlemini gerçekleştirebilecek fonksiyon şablonunu oluşturuyoruz.
Çoklu Parametreli Fonksiyon Şablonları
Listeyi ayırmak için virgül kullanarak şablon fonksiyonunda birden fazla genel tür kullanabiliriz.
Sözdizimi
template return_type function_name (arguments of type T1, T2....) { // body of function. }
Yukarıdaki sözdiziminde şablon fonksiyonunun farklı türden herhangi bir sayıda argümanı kabul edebildiğini gördük.
Basit bir örnek görelim:
#include using namespace std; template void fun(X a,Y b) { std::cout << 'Value of a is : ' < <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's understand this through a simple example:</strong> </p> <pre> #include using namespace std; template void fun(X a) { std::cout << 'Value of a is : ' < <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'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's understand this through a simple example:</strong> </p> <pre> #include using namespace std; void fun(double a) { cout<<\'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'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 << 'Addition of num1 and num2 : ' << num1+num2<<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, 'd'.</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'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 << 'Values of a and b are : ' << a<<\' ,\'< <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' 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'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<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 'A' 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<<>
Yukarıdaki örnekte şablon işlevinde iki genel tür kullanıyoruz, yani X ve Y.
Bir İşlev Şablonunun Aşırı Yüklenmesi
Genel fonksiyonu aşırı yükleyebiliriz, bu da aşırı yüklenmiş şablon fonksiyonlarının parametre listesinde farklılık gösterebileceği anlamına gelir.
Bunu basit bir örnekle anlayalım:
#include using namespace std; template void fun(X a) { std::cout << 'Value of a is : ' < <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'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's understand this through a simple example:</strong> </p> <pre> #include using namespace std; void fun(double a) { cout<<\'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'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 << 'Addition of num1 and num2 : ' << num1+num2<<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, 'd'.</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'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 << 'Values of a and b are : ' << a<<\' ,\'< <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' 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'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<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 'A' 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<<>
Yukarıdaki örnekte fun() fonksiyonunun şablonu aşırı yüklenmiştir.
Genel İşlevlerin Kısıtlamaları
Genel işlevler, veri türünün farklı olması dışında bir işlevin tüm sürümleri için aynı işlemi gerçekleştirir. Her iki işlevin de farklı işlevleri olması nedeniyle genel işlevle değiştirilemeyen aşırı yüklenmiş bir işlevin basit bir örneğini görelim.
öküz vs boğa
Bunu basit bir örnekle anlayalım:
#include using namespace std; void fun(double a) { cout<<\'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'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 << 'Addition of num1 and num2 : ' << num1+num2<<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, 'd'.</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'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 << 'Values of a and b are : ' << a<<\' ,\'< <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' 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'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<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 'A' 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>
Yukarıdaki örnekte sıradan fonksiyonları aşırı yüklüyoruz. Her iki işlevin de farklı işlevleri olduğundan genel işlevleri aşırı yükleyemeyiz. Birincisi değeri gösteriyor, ikincisi ise sayının çift olup olmadığını belirliyor.
SINIF ŞABLONU
Sınıf Şablonu Fonksiyon Şablonuna benzer şekilde de tanımlanabilir. Bir sınıf Şablon kavramını kullandığında, sınıf genel sınıf olarak bilinir.
Sözdizimi
template class class_name { . . }
Tip türü sınıf başlatıldığında belirlenecek yer tutucu adıdır. Virgülle ayrılmış bir liste kullanarak birden fazla genel veri türü tanımlayabiliriz. Ttype sınıf gövdesinin içinde kullanılabilir.
Şimdi bir sınıfın örneğini oluşturuyoruz
class_name ob;
nerede sınıf_adı : Sınıfın adıdır.
bugüne kadar dönüştürücü dize
tip : Sınıfın üzerinde çalıştığı veri türüdür.
en : Nesnenin adıdır.
Basit bir örnek görelim:
#include using namespace std; template class A { public: T num1 = 5; T num2 = 6; void add() { std::cout << 'Addition of num1 and num2 : ' << num1+num2<<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, 'd'.</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'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 << 'Values of a and b are : ' << a<<\' ,\'< <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' 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'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<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 'A' 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;>
Yukarıdaki örnekte A sınıfı için bir şablon oluşturuyoruz. main() yönteminin içinde A sınıfının 'd' isimli örneğini oluşturuyoruz.
ÇOKLU PARAMETRELER İLE SINIF ŞABLONU
Bir sınıf şablonunda birden fazla genel veri türü kullanabiliriz ve her genel veri türü virgülle ayrılır.
Sözdizimi
template class class_name { // Body of the class. }
Sınıf şablonunun iki genel veri türü içerdiği basit bir örneği görelim.
#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 << 'Values of a and b are : ' << a<<\\' ,\\'< <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' 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'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<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 'A' 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></\\'>
Tür Dışı Şablon Bağımsız Değişkenleri
Şablon birden fazla bağımsız değişken içerebilir ve tür dışı bağımsız değişkenleri de kullanabiliriz. T türü bağımsız değişkenine ek olarak, dizeler, işlev adları, sabit ifadeler ve yerleşik türler gibi diğer bağımsız değişken türlerini de kullanabiliriz. Aşağıdaki örneği görelim:
template class array { T arr[size]; // automatic array initialization. };
Yukarıdaki durumda, tip dışı şablon argümanı boyuttur ve bu nedenle şablon, dizinin boyutunu argüman olarak sağlar.
Bir sınıfın nesneleri oluşturulduğunda argümanlar belirtilir:
array t1; // array of 15 integers. array t2; // array of 10 floats. array t3; // array of 4 chars.
Tip dışı şablon argümanlarının basit bir örneğini görelim.
#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<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 'A' 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++)>
Yukarıdaki örnekte, tip dışı şablon argümanını, yani boyutu içeren sınıf şablonu yaratılmıştır. 'A' sınıfının nesnesi oluşturulduğunda belirtilir.
Hatırlanacak noktalar
- C++, genel programlama kavramını uygulamaya yönelik şablon olarak bilinen güçlü bir özelliği destekler.
- Bir şablon, farklı veri türlerini işlemek için bir sınıf ailesi veya işlev ailesi oluşturmamıza olanak tanır.
- Şablon sınıfları ve işlevleri, farklı veri türlerinin kod çoğaltılmasını ortadan kaldırır ve böylece geliştirmeyi daha kolay ve hızlı hale getirir.
- Hem sınıf hem de fonksiyon şablonunda birden fazla parametre kullanılabilir.
- Şablon işlevleri de aşırı yüklenebilir.
- Yerleşik veya türetilmiş veri türleri gibi tür dışı bağımsız değişkenleri de şablon bağımsız değişkenleri olarak kullanabiliriz.
\\'>\'value>'addition>