{"id":10481,"date":"2025-03-19T09:00:00","date_gmt":"2025-03-19T04:00:00","guid":{"rendered":"https:\/\/afzalbadshah.com\/?p=10481"},"modified":"2026-03-30T09:02:49","modified_gmt":"2026-03-30T04:02:49","slug":"polymorphism-in-c-oop-tutorial-with-real-life-examples","status":"publish","type":"post","link":"https:\/\/afzalbadshah.com\/index.php\/2025\/03\/19\/polymorphism-in-c-oop-tutorial-with-real-life-examples\/","title":{"rendered":"Polymorphism in C++ | OOP Tutorial with Real-Life Examples"},"content":{"rendered":"\n<p>Polymorphism is one of the four core concepts of Object-Oriented Programming (OOP), along with encapsulation, inheritance, and abstraction. The term polymorphism is derived from two Greek words; poly (many) and morph (forms). In programming, it means one name, many forms. In simple words, polymorphism allows a single function, operator, or object to behave differently based on the context. This makes our code flexible, reusable, and easy to extend.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Understanding Polymorphism<\/h2>\n\n\n\n<p>Think about the word <strong>\u201cdrive\u201d<\/strong>.<br>A <strong>car<\/strong> drives, a <strong>bike<\/strong> drives, and a <strong>bus<\/strong> drives. The action \u201cdrive\u201d is the same, but the behavior is different depending on the object. This is polymorphism; the same operation acting differently for different entities.<\/p>\n\n\n\n<p>Similarly, in a company, the manager, designer, and developer all respond to the command <strong>\u201cWork!\u201d<\/strong>, but each performs a different task. This is how polymorphism works in real life and also in programming.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What Is Polymorphism in C++<\/h2>\n\n\n\n<p>Polymorphism in C++ is the ability of a function, operator, or object to take many forms. It allows the same function name to perform different tasks depending on the type of object or the number of parameters. Polymorphism provides the foundation for dynamic behavior in software, allowing us to write programs that can adapt at runtime.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img data-recalc-dims=\"1\" decoding=\"async\" width=\"640\" height=\"360\" src=\"https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2025\/03\/Polymorphism-1.jpg?resize=640%2C360&#038;ssl=1\" alt=\"\" class=\"wp-image-43612\" srcset=\"https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2025\/03\/Polymorphism-1.jpg?resize=1024%2C576&amp;ssl=1 1024w, https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2025\/03\/Polymorphism-1.jpg?resize=300%2C169&amp;ssl=1 300w, https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2025\/03\/Polymorphism-1.jpg?resize=768%2C432&amp;ssl=1 768w, https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2025\/03\/Polymorphism-1.jpg?resize=1536%2C864&amp;ssl=1 1536w, https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2025\/03\/Polymorphism-1.jpg?resize=480%2C270&amp;ssl=1 480w, https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2025\/03\/Polymorphism-1.jpg?w=1920&amp;ssl=1 1920w, https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2025\/03\/Polymorphism-1.jpg?w=1280&amp;ssl=1 1280w\" sizes=\"(max-width: 640px) 100vw, 640px\" \/><figcaption class=\"wp-element-caption\">Types of Polymorphism<\/figcaption><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Types of Polymorphism in C++<\/h2>\n\n\n\n<p>C++ supports two main types of polymorphism:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>Type<\/th><th>Description<\/th><th>Example<\/th><\/tr><\/thead><tbody><tr><td><strong>Compile-Time Polymorphism (Static)<\/strong><\/td><td>Decided by the compiler during compilation.<\/td><td>Function Overloading, Operator Overloading<\/td><\/tr><tr><td><strong>Run-Time Polymorphism (Dynamic)<\/strong><\/td><td>Decided during program execution.<\/td><td>Virtual Functions \/ Function Overriding<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Compile-Time (Static) Polymorphism<\/h2>\n\n\n\n<p>Compile-time polymorphism means the decision about which function to call is made before the program runs. This is achieved using function overloading and operator overloading.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img data-recalc-dims=\"1\" decoding=\"async\" width=\"640\" height=\"376\" src=\"https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2025\/03\/function-overloading.png?resize=640%2C376&#038;ssl=1\" alt=\"\" class=\"wp-image-43608\" srcset=\"https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2025\/03\/function-overloading.png?resize=1024%2C602&amp;ssl=1 1024w, https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2025\/03\/function-overloading.png?resize=300%2C176&amp;ssl=1 300w, https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2025\/03\/function-overloading.png?resize=768%2C451&amp;ssl=1 768w, https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2025\/03\/function-overloading.png?resize=459%2C270&amp;ssl=1 459w, https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2025\/03\/function-overloading.png?w=1218&amp;ssl=1 1218w\" sizes=\"(max-width: 640px) 100vw, 640px\" \/><figcaption class=\"wp-element-caption\">Function overloading<\/figcaption><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">Function Overloading Example<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;iostream&gt;\nusing namespace std;\n\nclass Math {\npublic:\n    int add(int a, int b) {\n        return a + b;\n    }\n\n    double add(double a, double b) {\n        return a + b;\n    }\n\n    int add(int a, int b, int c) {\n        return a + b + c;\n    }\n};\n\nint main() {\n    Math m;\n    cout &lt;&lt; \"Sum of two integers: \" &lt;&lt; m.add(2, 3) &lt;&lt; endl;\n    cout &lt;&lt; \"Sum of two doubles: \" &lt;&lt; m.add(2.5, 3.8) &lt;&lt; endl;\n    cout &lt;&lt; \"Sum of three integers: \" &lt;&lt; m.add(1, 2, 3) &lt;&lt; endl;\n    return 0;\n}\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Operator Overloading Example<\/h3>\n\n\n\n<figure class=\"wp-block-image size-large\"><img data-recalc-dims=\"1\" decoding=\"async\" width=\"640\" height=\"394\" src=\"https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2025\/03\/Polymorphism.jpg?resize=640%2C394&#038;ssl=1\" alt=\"\" class=\"wp-image-43609\" srcset=\"https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2025\/03\/Polymorphism.jpg?resize=1024%2C630&amp;ssl=1 1024w, https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2025\/03\/Polymorphism.jpg?resize=300%2C184&amp;ssl=1 300w, https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2025\/03\/Polymorphism.jpg?resize=768%2C472&amp;ssl=1 768w, https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2025\/03\/Polymorphism.jpg?resize=439%2C270&amp;ssl=1 439w, https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2025\/03\/Polymorphism.jpg?w=1041&amp;ssl=1 1041w\" sizes=\"(max-width: 640px) 100vw, 640px\" \/><figcaption class=\"wp-element-caption\">Operator Overloading<\/figcaption><\/figure>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;iostream&gt;\nusing namespace std;\n\nclass Plot {\n    int length, width;\n\npublic:\n    \/\/ Constructor\n    Plot(int l = 0, int w = 0) {\n        length = l;\n        width = w;\n    }\n\n    \/\/ Operator overloading (+)\n    Plot operator + (Plot p) {\n        Plot temp;\n        temp.length = length + p.length;\n        temp.width  = width + p.width;\n        return temp;\n    }\n\n    \/\/ Display function\n    void display() {\n        cout &lt;&lt; \"Plot: \" &lt;&lt; length &lt;&lt; \" x \" &lt;&lt; width &lt;&lt; endl;\n        cout &lt;&lt; \"Area: \" &lt;&lt; length * width &lt;&lt; endl;\n    }\n};\n\nint main() {\n    \/\/ Create two plots\n    Plot p1(10, 5);\n    Plot p2(6, 3);\n\n    \/\/ Add plots using overloaded operator\n    Plot p3 = p1 + p2;\n\n    \/\/ Display result\n    cout &lt;&lt; \"Combined Plot:\\n\";\n    p3.display();\n\n    return 0;\n}\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Run-Time (Dynamic) Polymorphism<\/h2>\n\n\n\n<p>Run-time polymorphism means the function that gets executed is determined at runtime, not compile time. This is achieved through function overriding using virtual functions and pointers\/references to base classes.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Function Overriding Example<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;iostream&gt;\nusing namespace std;\n\nclass Animal {\npublic:\n    virtual void sound() {\n        cout &lt;&lt; \"Animal makes a sound\" &lt;&lt; endl;\n    }\n};\n\nclass Dog : public Animal {\npublic:\n    void sound() {\n        cout &lt;&lt; \"Dog barks\" &lt;&lt; endl;\n    }\n};\n\nclass Cat : public Animal {\npublic:\n    void sound() {\n        cout &lt;&lt; \"Cat meows\" &lt;&lt; endl;\n    }\n};\n\nint main() {\n    Animal* a;   \/\/ Base class pointer\n    Dog d;\n    Cat c;\n\n    a = &amp;d;\n    a-&gt;sound();   \/\/ Calls Dog\u2019s sound()\n\n    a = &amp;c;\n    a-&gt;sound();   \/\/ Calls Cat\u2019s sound()\n\n    return 0;\n}\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Why Do We Use Pointers in Polymorphism?<\/h3>\n\n\n\n<p>When we call a virtual function using an object (e.g., <code>obj.sound()<\/code>), the compiler already knows its type, so it doesn\u2019t check at runtime. But when we call it through a base class pointer or reference, C++ checks which object the pointer actually refers to and calls the correct version. Hence, pointers or references are used to achieve real dynamic behavior.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Difference Between Function Overloading and Function Overriding<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>Feature<\/th><th>Function Overloading<\/th><th>Function Overriding<\/th><\/tr><\/thead><tbody><tr><td>Type<\/td><td>Compile-time polymorphism<\/td><td>Run-time polymorphism<\/td><\/tr><tr><td>Parameters<\/td><td>Must differ<\/td><td>Must be same<\/td><\/tr><tr><td>Inheritance<\/td><td>Not required<\/td><td>Required<\/td><\/tr><tr><td>Keyword<\/td><td>No keyword needed<\/td><td>Uses <code>virtual<\/code> keyword<\/td><\/tr><tr><td>Execution Time<\/td><td>Decided at compile time<\/td><td>Decided at runtime<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Real-World Example: Draw Function<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;iostream&gt;\nusing namespace std;\n\nclass Shape {\npublic:\n    virtual void draw() {\n        cout &lt;&lt; \"Drawing a shape\" &lt;&lt; endl;\n    }\n};\n\nclass Circle : public Shape {\npublic:\n    void draw() {\n        cout &lt;&lt; \"Drawing a circle\" &lt;&lt; endl;\n    }\n};\n\nclass Rectangle : public Shape {\npublic:\n    void draw() {\n        cout &lt;&lt; \"Drawing a rectangle\" &lt;&lt; endl;\n    }\n};\n\nint main() {\n    Shape* shape;\n    Circle c;\n    Rectangle r;\n\n    shape = &amp;c;\n    shape-&gt;draw();\n\n    shape = &amp;r;\n    shape-&gt;draw();\n\n    return 0;\n}\n<\/code><\/pre>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Drawing a circle\nDrawing a rectangle\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Advantages of Polymorphism<\/h2>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Code Reusability:<\/strong> Once a base class or function is written, it can be reused by derived classes without rewriting code. This reduces redundancy and saves development time.<\/li>\n\n\n\n<li><strong>Flexibility:<\/strong> Polymorphism allows one interface to handle multiple types of behavior. For example, a single pointer of a base class can call different functions depending on which derived object it points to.<\/li>\n\n\n\n<li><strong>Scalability:<\/strong> New classes and behaviors can be added easily without modifying existing code. This makes software easier to extend and maintain.<\/li>\n\n\n\n<li><strong>Readability:<\/strong> Code becomes more organized and easier to understand since similar actions are grouped under the same interface or function name.<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\">Key Points to Remember<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Polymorphism = One interface, many implementations.<\/li>\n\n\n\n<li>Function Overloading and Operator Overloading \u2192 Compile-time polymorphism.<\/li>\n\n\n\n<li>Function Overriding using <code>virtual<\/code> \u2192 Run-time polymorphism.<\/li>\n\n\n\n<li>Use base class pointers or references to achieve dynamic behavior.<\/li>\n\n\n\n<li>Destructors in base classes should be declared <code>virtual<\/code> to ensure proper cleanup.<\/li>\n<\/ul>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>Aspect<\/th><th>Compile-Time<\/th><th>Run-Time<\/th><\/tr><\/thead><tbody><tr><td>How it happens<\/td><td>Function\/Operator Overloading<\/td><td>Virtual Functions<\/td><\/tr><tr><td>Decision Time<\/td><td>Before program runs<\/td><td>During program execution<\/td><\/tr><tr><td>Keyword<\/td><td>None<\/td><td><code>virtual<\/code><\/td><\/tr><tr><td>Inheritance needed<\/td><td>No<\/td><td>Yes<\/td><\/tr><tr><td>Binding Type<\/td><td>Early binding<\/td><td>Late binding<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Polymorphism is one of the four core concepts of Object-Oriented Programming (OOP), along with encapsulation, inheritance, and abstraction. The term polymorphism is derived from two Greek words; poly (many) and morph (forms). In programming, it means one name, many forms. In simple words, polymorphism allows a single function, operator, or object to behave differently based on the context. This makes our code flexible, reusable, and easy to extend. Understanding Polymorphism Think about the word \u201cdrive\u201d.A car drives, a bike drives,&#8230;<\/p>\n<p class=\"read-more\"><a class=\"btn btn-default\" href=\"https:\/\/afzalbadshah.com\/index.php\/2025\/03\/19\/polymorphism-in-c-oop-tutorial-with-real-life-examples\/\"> Read More<span class=\"screen-reader-text\">  Read More<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":10482,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"enabled":false},"version":2}},"categories":[637],"tags":[724,722,623,608,723],"class_list":["post-10481","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oop-with-c","tag-compile-time-polymorphism","tag-fucntion-overriding","tag-function-overloading","tag-polymorphism","tag-run-time-polymorphism"],"aioseo_notices":[],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2025\/10\/Copy-of-Access-Modifers-in-OOP-Java.jpg?fit=1920%2C1080&ssl=1","jetpack_sharing_enabled":true,"jetpack_likes_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/pf3emP-2J3","jetpack-related-posts":[],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/afzalbadshah.com\/index.php\/wp-json\/wp\/v2\/posts\/10481","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/afzalbadshah.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/afzalbadshah.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/afzalbadshah.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/afzalbadshah.com\/index.php\/wp-json\/wp\/v2\/comments?post=10481"}],"version-history":[{"count":4,"href":"https:\/\/afzalbadshah.com\/index.php\/wp-json\/wp\/v2\/posts\/10481\/revisions"}],"predecessor-version":[{"id":43613,"href":"https:\/\/afzalbadshah.com\/index.php\/wp-json\/wp\/v2\/posts\/10481\/revisions\/43613"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/afzalbadshah.com\/index.php\/wp-json\/wp\/v2\/media\/10482"}],"wp:attachment":[{"href":"https:\/\/afzalbadshah.com\/index.php\/wp-json\/wp\/v2\/media?parent=10481"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/afzalbadshah.com\/index.php\/wp-json\/wp\/v2\/categories?post=10481"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/afzalbadshah.com\/index.php\/wp-json\/wp\/v2\/tags?post=10481"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}