{"id":128,"date":"2026-05-21T04:47:09","date_gmt":"2026-05-21T04:47:09","guid":{"rendered":"https:\/\/gigz.pk\/cpp\/?post_type=lesson&#038;p=128"},"modified":"2026-05-22T11:17:27","modified_gmt":"2026-05-22T11:17:27","slug":"inheritance","status":"publish","type":"lesson","link":"https:\/\/gigz.pk\/cpp\/?lesson=inheritance","title":{"rendered":"Inheritance"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Inheritance is one of the most important concepts of Object-Oriented Programming (OOP) in C++. It allows one class to inherit properties and behaviors from another class.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What is Inheritance?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Inheritance is the process of creating a new class from an existing class. The new class automatically gets the data members and functions of the existing class.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Key Terms in Inheritance<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Base Class (Parent Class)<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The class whose properties are inherited.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Derived Class (Child Class)<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The class that inherits features from the base class.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Why Use Inheritance?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Inheritance is useful because it:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Promotes code reusability<\/li>\n\n\n\n<li>Reduces code duplication<\/li>\n\n\n\n<li>Improves program structure<\/li>\n\n\n\n<li>Makes code easier to maintain<\/li>\n\n\n\n<li>Supports real-world relationships<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Syntax of Inheritance<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>class BaseClass {<br><br>    \/\/ members<br>};<br><br>class DerivedClass : public BaseClass {<br><br>    \/\/ additional members<br>};<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Example of Inheritance<\/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 eat() {<br><br>        cout &lt;&lt; \"Eating...\" &lt;&lt; endl;<br>    }<br>};<br><br>class Dog : public Animal {<br><br>public:<br><br>    void bark() {<br><br>        cout &lt;&lt; \"Barking...\" &lt;&lt; endl;<br>    }<br>};<br><br>int main() {<br><br>    Dog d1;<br><br>    d1.eat();<br>    d1.bark();<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>Eating...<br>Barking...<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">How Inheritance Works<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Child class inherits parent class members<\/li>\n\n\n\n<li>Derived class can use parent functions<\/li>\n\n\n\n<li>Derived class can also add its own functions<\/li>\n\n\n\n<li>Code reuse becomes easier<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Types of Inheritance in C++<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">1. Single Inheritance<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">One derived class inherits from one base class.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class A {<br>};<br><br>class B : public A {<br>};<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">2. Multilevel Inheritance<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">A class is derived from another derived class.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Example of Multilevel Inheritance<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;iostream&gt;<br>using namespace std;<br><br>class A {<br><br>public:<br><br>    void showA() {<br><br>        cout &lt;&lt; \"Class A\" &lt;&lt; endl;<br>    }<br>};<br><br>class B : public A {<br><br>public:<br><br>    void showB() {<br><br>        cout &lt;&lt; \"Class B\" &lt;&lt; endl;<br>    }<br>};<br><br>class C : public B {<br><br>public:<br><br>    void showC() {<br><br>        cout &lt;&lt; \"Class C\" &lt;&lt; endl;<br>    }<br>};<br><br>int main() {<br><br>    C obj;<br><br>    obj.showA();<br>    obj.showB();<br>    obj.showC();<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>Class A<br>Class B<br>Class C<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">3. Multiple Inheritance<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">A class inherits from more than one base class.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class A {<br>};<br><br>class B {<br>};<br><br>class C : public A, public B {<br>};<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">4. Hierarchical Inheritance<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Multiple classes inherit from one base class.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class A {<br>};<br><br>class B : public A {<br>};<br><br>class C : public A {<br>};<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">5. Hybrid Inheritance<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Combination of different inheritance types.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Access Modes in Inheritance<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Inheritance can use:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Public inheritance<\/li>\n\n\n\n<li>Protected inheritance<\/li>\n\n\n\n<li>Private inheritance<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Public Inheritance<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Public members remain public in derived class.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class B : public A {<br>};<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Real-Life Example<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Think of vehicles:<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Parent Class<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Vehicle<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Engine<\/li>\n\n\n\n<li>Wheels<\/li>\n\n\n\n<li>Speed<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Child Class<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Car<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Inherits vehicle features<\/li>\n\n\n\n<li>Adds AC and music system<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Advantages of Inheritance<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Reusable code<\/li>\n\n\n\n<li>Better organization<\/li>\n\n\n\n<li>Easier maintenance<\/li>\n\n\n\n<li>Faster development<\/li>\n\n\n\n<li>Supports real-world modeling<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Why Inheritance is Important<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Inheritance is important because it:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Simplifies programming<\/li>\n\n\n\n<li>Reduces repetition<\/li>\n\n\n\n<li>Improves scalability<\/li>\n\n\n\n<li>Supports advanced OOP concepts<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Applications of Inheritance<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Inheritance is widely used in:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Game development<\/li>\n\n\n\n<li>Banking systems<\/li>\n\n\n\n<li>Software frameworks<\/li>\n\n\n\n<li>GUI applications<\/li>\n\n\n\n<li>Management systems<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Inheritance in C++ allows one class to reuse the features of another class, making programs more organized, reusable, and maintainable. It is a powerful OOP feature that helps model real-world relationships and build scalable applications.<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 > Inheritance<\/span><\/span><\/div>\n\n\n<div class=\"schema-faq wp-block-yoast-faq-block\"><\/div>\n","protected":false},"menu_order":40,"template":"","class_list":["post-128","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>Inheritance - Learn C++Language with GiGz.PK<\/title>\n<meta name=\"description\" content=\"Learn C++ inheritance with examples of base and derived classes, types of inheritance, and code reusability in OOP programming.\" \/>\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=\"Inheritance - Learn C++Language with GiGz.PK\" \/>\n<meta property=\"og:description\" content=\"Learn C++ inheritance with examples of base and derived classes, types of inheritance, and code reusability in OOP programming.\" \/>\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:17:27+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=inheritance\",\"url\":\"https:\\\/\\\/gigz.pk\\\/\",\"name\":\"Inheritance - Learn C++Language with GiGz.PK\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/gigz.pk\\\/cpp\\\/#website\"},\"datePublished\":\"2026-05-21T04:47:09+00:00\",\"dateModified\":\"2026-05-22T11:17:27+00:00\",\"description\":\"Learn C++ inheritance with examples of base and derived classes, types of inheritance, and code reusability in OOP programming.\",\"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 > Inheritance\"}]},{\"@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":"Inheritance - Learn C++Language with GiGz.PK","description":"Learn C++ inheritance with examples of base and derived classes, types of inheritance, and code reusability in OOP programming.","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":"Inheritance - Learn C++Language with GiGz.PK","og_description":"Learn C++ inheritance with examples of base and derived classes, types of inheritance, and code reusability in OOP programming.","og_url":"https:\/\/gigz.pk\/","og_site_name":"Learn C++Language with GiGz.PK","article_modified_time":"2026-05-22T11:17:27+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=inheritance","url":"https:\/\/gigz.pk\/","name":"Inheritance - Learn C++Language with GiGz.PK","isPartOf":{"@id":"https:\/\/gigz.pk\/cpp\/#website"},"datePublished":"2026-05-21T04:47:09+00:00","dateModified":"2026-05-22T11:17:27+00:00","description":"Learn C++ inheritance with examples of base and derived classes, types of inheritance, and code reusability in OOP programming.","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 > Inheritance"}]},{"@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\/128","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=128"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}