{"id":164,"date":"2026-05-21T07:58:17","date_gmt":"2026-05-21T07:58:17","guid":{"rendered":"https:\/\/gigz.pk\/cpp\/?post_type=lesson&#038;p=164"},"modified":"2026-05-24T16:00:37","modified_gmt":"2026-05-24T16:00:37","slug":"templates","status":"publish","type":"lesson","link":"https:\/\/gigz.pk\/cpp\/?lesson=templates","title":{"rendered":"Templates"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Templates in C++ are a powerful feature that allows you to write generic code. This means you can create functions and classes that work with different data types without rewriting the same code multiple times.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What is a Template?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">A template is a blueprint that tells the compiler to generate functions or classes for any data type.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">It removes code repetition and improves flexibility.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Why Use Templates?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Templates are useful because they:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Reduce code duplication<\/li>\n\n\n\n<li>Make code reusable<\/li>\n\n\n\n<li>Work with multiple data types<\/li>\n\n\n\n<li>Improve code flexibility<\/li>\n\n\n\n<li>Are widely used in STL<\/li>\n<\/ul>\n\n\n\n<h1 class=\"wp-block-heading\">Types of Templates in C++<\/h1>\n\n\n\n<p class=\"wp-block-paragraph\">There are two main types:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Function Templates<\/li>\n\n\n\n<li>Class Templates<\/li>\n<\/ul>\n\n\n\n<h1 class=\"wp-block-heading\">Function Templates<\/h1>\n\n\n\n<p class=\"wp-block-paragraph\">A function template allows a single function to work with different data types.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Syntax<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>template &lt;typename T&gt;<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Example of Function Template<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;iostream&gt;<br>using namespace std;<br><br>template &lt;typename T&gt;<br>T add(T a, T b) {<br><br>    return a + b;<br>}<br><br>int main() {<br><br>    cout &lt;&lt; add(10, 20) &lt;&lt; endl;<br>    cout &lt;&lt; add(5.5, 2.5) &lt;&lt; endl;<br><br>    return 0;<br>}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Output<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>30<br>8<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">How It Works<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>T<\/code> is a placeholder for any data type<\/li>\n\n\n\n<li>Compiler automatically decides the type<\/li>\n\n\n\n<li>Same function works for int, float, double, etc.<\/li>\n<\/ul>\n\n\n\n<h1 class=\"wp-block-heading\">Class Templates<\/h1>\n\n\n\n<p class=\"wp-block-paragraph\">A class template allows a class to work with different data types.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Syntax<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>template &lt;typename T&gt;<br>class ClassName {<br>    T data;<br>};<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Example of Class Template<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;iostream&gt;<br>using namespace std;<br><br>template &lt;typename T&gt;<br>class Box {<br><br>public:<br><br>    T value;<br><br>    void setValue(T v) {<br>        value = v;<br>    }<br><br>    T getValue() {<br>        return value;<br>    }<br>};<br><br>int main() {<br><br>    Box&lt;int&gt; b1;<br>    b1.setValue(100);<br>    cout &lt;&lt; b1.getValue() &lt;&lt; endl;<br><br>    Box&lt;string&gt; b2;<br>    b2.setValue(\"Hello\");<br>    cout &lt;&lt; b2.getValue() &lt;&lt; endl;<br><br>    return 0;<br>}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Output<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>100<br>Hello<\/code><\/pre>\n\n\n\n<h1 class=\"wp-block-heading\">How Class Templates Work<\/h1>\n\n\n\n<ul class=\"wp-block-list\">\n<li>One class works for multiple data types<\/li>\n\n\n\n<li>Type is defined when creating an object<\/li>\n\n\n\n<li>Same logic is reused<\/li>\n<\/ul>\n\n\n\n<h1 class=\"wp-block-heading\">Real-Life Example<\/h1>\n\n\n\n<p class=\"wp-block-paragraph\">Think of a template like a form:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>One form can be used for students, employees, or customers<\/li>\n\n\n\n<li>Only the data changes, not the structure<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">This is how templates work in programming.<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">Advantages of Templates<\/h1>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Reduce repetitive code<\/li>\n\n\n\n<li>Improve reusability<\/li>\n\n\n\n<li>Support multiple data types<\/li>\n\n\n\n<li>Make programs scalable<\/li>\n\n\n\n<li>Used heavily in STL<\/li>\n<\/ul>\n\n\n\n<h1 class=\"wp-block-heading\">Common Mistakes with Templates<\/h1>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Forgetting template declaration<\/li>\n\n\n\n<li>Using wrong data types<\/li>\n\n\n\n<li>Not understanding type deduction<\/li>\n\n\n\n<li>Mixing incompatible operations<\/li>\n<\/ul>\n\n\n\n<h1 class=\"wp-block-heading\">Difference Between Function and Class Templates<\/h1>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>Function Template<\/th><th>Class Template<\/th><\/tr><\/thead><tbody><tr><td>Works with functions<\/td><td>Works with classes<\/td><\/tr><tr><td>Used for operations<\/td><td>Used for data structures<\/td><\/tr><tr><td>Simpler usage<\/td><td>More complex structure<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h1 class=\"wp-block-heading\">Applications of Templates<\/h1>\n\n\n\n<p class=\"wp-block-paragraph\">Templates are used in:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>STL containers (vector, map, list)<\/li>\n\n\n\n<li>Generic algorithms<\/li>\n\n\n\n<li>Data structure implementation<\/li>\n\n\n\n<li>Reusable libraries<\/li>\n\n\n\n<li>Large software systems<\/li>\n<\/ul>\n\n\n\n<h1 class=\"wp-block-heading\">Why Templates are Important<\/h1>\n\n\n\n<p class=\"wp-block-paragraph\">Templates are important because they:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Enable generic programming<\/li>\n\n\n\n<li>Reduce code repetition<\/li>\n\n\n\n<li>Improve performance and scalability<\/li>\n\n\n\n<li>Are a core part of modern C++<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Templates in C++ are a powerful feature that allows writing flexible and reusable code. By using function and class templates, developers can create programs that work with multiple data types efficiently without rewriting code.<\/p>\n\n\n<div class=\"yoast-breadcrumbs\"><span><span><a href=\"https:\/\/gigz.pk\/cpp\">Home<\/a><\/span> \u00bb <span class=\"breadcrumb_last\" aria-current=\"page\">Professional C++ > Advanced Concepts > Templates<\/span><\/span><\/div>\n\n\n<div class=\"schema-faq wp-block-yoast-faq-block\"><\/div>\n","protected":false},"menu_order":58,"template":"","class_list":["post-164","lesson","type-lesson","status-publish","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.7 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Templates - Learn C++Language with GiGz.PK<\/title>\n<meta name=\"description\" content=\"Learn C++ templates with function and class examples, generic programming concepts, syntax, and reusable code for multiple data types in STL.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/gigz.pk\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Templates - Learn C++Language with GiGz.PK\" \/>\n<meta property=\"og:description\" content=\"Learn C++ templates with function and class examples, generic programming concepts, syntax, and reusable code for multiple data types in STL.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/gigz.pk\/\" \/>\n<meta property=\"og:site_name\" content=\"Learn C++Language with GiGz.PK\" \/>\n<meta property=\"article:modified_time\" content=\"2026-05-24T16:00:37+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":[\"WebPage\",\"FAQPage\"],\"@id\":\"https:\\\/\\\/gigz.pk\\\/cpp\\\/?lesson=templates\",\"url\":\"https:\\\/\\\/gigz.pk\\\/\",\"name\":\"Templates - Learn C++Language with GiGz.PK\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/gigz.pk\\\/cpp\\\/#website\"},\"datePublished\":\"2026-05-21T07:58:17+00:00\",\"dateModified\":\"2026-05-24T16:00:37+00:00\",\"description\":\"Learn C++ templates with function and class examples, generic programming concepts, syntax, and reusable code for multiple data types in STL.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/gigz.pk\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/gigz.pk\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/gigz.pk\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/gigz.pk\\\/cpp\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Professional C++ > Advanced Concepts > Templates\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/gigz.pk\\\/cpp\\\/#website\",\"url\":\"https:\\\/\\\/gigz.pk\\\/cpp\\\/\",\"name\":\"Learn C++Language with GiGz.PK\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/gigz.pk\\\/cpp\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Templates - Learn C++Language with GiGz.PK","description":"Learn C++ templates with function and class examples, generic programming concepts, syntax, and reusable code for multiple data types in STL.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/gigz.pk\/","og_locale":"en_US","og_type":"article","og_title":"Templates - Learn C++Language with GiGz.PK","og_description":"Learn C++ templates with function and class examples, generic programming concepts, syntax, and reusable code for multiple data types in STL.","og_url":"https:\/\/gigz.pk\/","og_site_name":"Learn C++Language with GiGz.PK","article_modified_time":"2026-05-24T16:00:37+00:00","twitter_card":"summary_large_image","twitter_misc":{"Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":["WebPage","FAQPage"],"@id":"https:\/\/gigz.pk\/cpp\/?lesson=templates","url":"https:\/\/gigz.pk\/","name":"Templates - Learn C++Language with GiGz.PK","isPartOf":{"@id":"https:\/\/gigz.pk\/cpp\/#website"},"datePublished":"2026-05-21T07:58:17+00:00","dateModified":"2026-05-24T16:00:37+00:00","description":"Learn C++ templates with function and class examples, generic programming concepts, syntax, and reusable code for multiple data types in STL.","breadcrumb":{"@id":"https:\/\/gigz.pk\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/gigz.pk\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/gigz.pk\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/gigz.pk\/cpp"},{"@type":"ListItem","position":2,"name":"Professional C++ > Advanced Concepts > Templates"}]},{"@type":"WebSite","@id":"https:\/\/gigz.pk\/cpp\/#website","url":"https:\/\/gigz.pk\/cpp\/","name":"Learn C++Language with GiGz.PK","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/gigz.pk\/cpp\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"}]}},"_links":{"self":[{"href":"https:\/\/gigz.pk\/cpp\/index.php?rest_route=\/wp\/v2\/lesson\/164","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gigz.pk\/cpp\/index.php?rest_route=\/wp\/v2\/lesson"}],"about":[{"href":"https:\/\/gigz.pk\/cpp\/index.php?rest_route=\/wp\/v2\/types\/lesson"}],"wp:attachment":[{"href":"https:\/\/gigz.pk\/cpp\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=164"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}