{"id":130,"date":"2026-05-21T04:48:21","date_gmt":"2026-05-21T04:48:21","guid":{"rendered":"https:\/\/gigz.pk\/cpp\/?post_type=lesson&#038;p=130"},"modified":"2026-05-22T11:21:13","modified_gmt":"2026-05-22T11:21:13","slug":"polymorphism","status":"publish","type":"lesson","link":"https:\/\/gigz.pk\/cpp\/?lesson=polymorphism","title":{"rendered":"Polymorphism"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Polymorphism is one of the core concepts of Object-Oriented Programming (OOP) in C++. It allows the same function, method, or operator to perform different tasks depending on the situation.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What is Polymorphism?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The word polymorphism means \u201cmany forms\u201d.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In C++, polymorphism allows one interface to behave differently for different objects or data types.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Why Use Polymorphism?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Polymorphism is useful because it:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Increases code flexibility<\/li>\n\n\n\n<li>Reduces code complexity<\/li>\n\n\n\n<li>Improves code reusability<\/li>\n\n\n\n<li>Supports scalable applications<\/li>\n\n\n\n<li>Helps in real-world modeling<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Types of Polymorphism in C++<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">C++ mainly supports two types of polymorphism:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Compile-Time Polymorphism<\/li>\n\n\n\n<li>Run-Time Polymorphism<\/li>\n<\/ul>\n\n\n\n<h1 class=\"wp-block-heading\">1. Compile-Time Polymorphism<\/h1>\n\n\n\n<p class=\"wp-block-paragraph\">Compile-time polymorphism is achieved during compilation.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">It is also called:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Early Binding<\/li>\n\n\n\n<li>Static Polymorphism<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Achieved Using<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Function Overloading<\/li>\n\n\n\n<li>Operator Overloading<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Function Overloading Example<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;iostream&gt;<br>using namespace std;<br><br>class Math {<br><br>public:<br><br>    int add(int a, int b) {<br><br>        return a + b;<br>    }<br><br>    int add(int a, int b, int c) {<br><br>        return a + b + c;<br>    }<br>};<br><br>int main() {<br><br>    Math obj;<br><br>    cout &lt;&lt; obj.add(2, 3) &lt;&lt; endl;<br>    cout &lt;&lt; obj.add(2, 3, 4) &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>5<br>9<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">How Compile-Time Polymorphism Works<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Same function name<\/li>\n\n\n\n<li>Different parameters<\/li>\n\n\n\n<li>Compiler decides which function to call<\/li>\n<\/ul>\n\n\n\n<h1 class=\"wp-block-heading\">2. Run-Time Polymorphism<\/h1>\n\n\n\n<p class=\"wp-block-paragraph\">Run-time polymorphism is achieved during program execution.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">It is also called:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Late Binding<\/li>\n\n\n\n<li>Dynamic Polymorphism<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Achieved Using<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Function Overriding<\/li>\n\n\n\n<li>Virtual Functions<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Function Overriding Example<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;iostream&gt;<br>using namespace std;<br><br>class Animal {<br><br>public:<br><br>    void sound() {<br><br>        cout &lt;&lt; \"Animal sound\" &lt;&lt; endl;<br>    }<br>};<br><br>class Dog : public Animal {<br><br>public:<br><br>    void sound() {<br><br>        cout &lt;&lt; \"Dog barks\" &lt;&lt; endl;<br>    }<br>};<br><br>int main() {<br><br>    Dog d;<br><br>    d.sound();<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>Dog barks<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Virtual Function Example<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;iostream&gt;<br>using namespace std;<br><br>class Animal {<br><br>public:<br><br>    virtual void sound() {<br><br>        cout &lt;&lt; \"Animal sound\" &lt;&lt; endl;<br>    }<br>};<br><br>class Dog : public Animal {<br><br>public:<br><br>    void sound() override {<br><br>        cout &lt;&lt; \"Dog barks\" &lt;&lt; endl;<br>    }<br>};<br><br>int main() {<br><br>    Animal *a;<br><br>    Dog d;<br><br>    a = &amp;d;<br><br>    a-&gt;sound();<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>Dog barks<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">How Run-Time Polymorphism Works<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Base class pointer is used<\/li>\n\n\n\n<li>Virtual function enables dynamic binding<\/li>\n\n\n\n<li>Function call is decided at runtime<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Difference Between Compile-Time and Run-Time Polymorphism<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>Compile-Time Polymorphism<\/th><th>Run-Time Polymorphism<\/th><\/tr><\/thead><tbody><tr><td>Decided during compilation<\/td><td>Decided during execution<\/td><\/tr><tr><td>Faster execution<\/td><td>Slightly slower<\/td><\/tr><tr><td>Function overloading<\/td><td>Function overriding<\/td><\/tr><tr><td>Static binding<\/td><td>Dynamic binding<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Real-Life Example<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Think of a person:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Teacher at school<\/li>\n\n\n\n<li>Parent at home<\/li>\n\n\n\n<li>Employee at office<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Same person behaves differently in different situations.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Advantages of Polymorphism<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Flexible code<\/li>\n\n\n\n<li>Easier maintenance<\/li>\n\n\n\n<li>Better scalability<\/li>\n\n\n\n<li>Improved readability<\/li>\n\n\n\n<li>Supports reusable code<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Why Polymorphism is Important<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Polymorphism is important because it:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Simplifies complex systems<\/li>\n\n\n\n<li>Improves software design<\/li>\n\n\n\n<li>Supports dynamic behavior<\/li>\n\n\n\n<li>Enhances object-oriented programming<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Applications of Polymorphism<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Polymorphism is widely used in:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Game development<\/li>\n\n\n\n<li>GUI applications<\/li>\n\n\n\n<li>Software frameworks<\/li>\n\n\n\n<li>Banking systems<\/li>\n\n\n\n<li>Real-time applications<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Polymorphism in C++ allows the same interface to perform different actions based on context. Through function overloading, overriding, and virtual functions, polymorphism improves flexibility, scalability, and reusability in Object-Oriented Programming.<audio autoplay=\"\"><\/audio><\/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\">Advanced C++ > Object-Oriented Programming > Polymorphism<\/span><\/span><\/div>\n\n\n<div class=\"schema-faq wp-block-yoast-faq-block\"><\/div>\n","protected":false},"menu_order":41,"template":"","class_list":["post-130","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>Polymorphism - Learn C++Language with GiGz.PK<\/title>\n<meta name=\"description\" content=\"Learn C++ polymorphism with examples of function overloading, overriding, and virtual functions for flexible and reusable OOP code.\" \/>\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=\"Polymorphism - Learn C++Language with GiGz.PK\" \/>\n<meta property=\"og:description\" content=\"Learn C++ polymorphism with examples of function overloading, overriding, and virtual functions for flexible and reusable OOP code.\" \/>\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-22T11:21:13+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=polymorphism\",\"url\":\"https:\\\/\\\/gigz.pk\\\/\",\"name\":\"Polymorphism - Learn C++Language with GiGz.PK\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/gigz.pk\\\/cpp\\\/#website\"},\"datePublished\":\"2026-05-21T04:48:21+00:00\",\"dateModified\":\"2026-05-22T11:21:13+00:00\",\"description\":\"Learn C++ polymorphism with examples of function overloading, overriding, and virtual functions for flexible and reusable OOP code.\",\"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\":\"Advanced C++ > Object-Oriented Programming > Polymorphism\"}]},{\"@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":"Polymorphism - Learn C++Language with GiGz.PK","description":"Learn C++ polymorphism with examples of function overloading, overriding, and virtual functions for flexible and reusable OOP code.","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":"Polymorphism - Learn C++Language with GiGz.PK","og_description":"Learn C++ polymorphism with examples of function overloading, overriding, and virtual functions for flexible and reusable OOP code.","og_url":"https:\/\/gigz.pk\/","og_site_name":"Learn C++Language with GiGz.PK","article_modified_time":"2026-05-22T11:21:13+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=polymorphism","url":"https:\/\/gigz.pk\/","name":"Polymorphism - Learn C++Language with GiGz.PK","isPartOf":{"@id":"https:\/\/gigz.pk\/cpp\/#website"},"datePublished":"2026-05-21T04:48:21+00:00","dateModified":"2026-05-22T11:21:13+00:00","description":"Learn C++ polymorphism with examples of function overloading, overriding, and virtual functions for flexible and reusable OOP code.","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":"Advanced C++ > Object-Oriented Programming > Polymorphism"}]},{"@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\/130","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=130"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}