{"id":6406,"date":"2025-03-05T09:00:00","date_gmt":"2025-03-05T04:00:00","guid":{"rendered":"https:\/\/afzalbadshah.com\/?p=6406"},"modified":"2026-03-18T17:42:29","modified_gmt":"2026-03-18T12:42:29","slug":"inheritance-in-c-for-beginners-complete-guide-with-examples-and-real-life-explanation","status":"publish","type":"post","link":"https:\/\/afzalbadshah.com\/index.php\/2025\/03\/05\/inheritance-in-c-for-beginners-complete-guide-with-examples-and-real-life-explanation\/","title":{"rendered":"Inheritance in C++ for Beginners: Complete Guide with Examples and Real-Life Explanation"},"content":{"rendered":"\n<p>In the real world, many objects share common characteristics but also have their own specialized features. For example, in a university environment, all people such as students, teachers, and administrative staff share common attributes like name, age, and identification number, yet each role also has its own specific responsibilities and properties. When designing software systems that represent such real world entities, it would be inefficient to repeatedly define the same common properties for every related class. Object Oriented Programming addresses this issue through a powerful mechanism known as <strong>inheritance<\/strong>, which allows a new class to reuse the existing features of another class while also extending it with additional capabilities. In this way, inheritance helps organize programs in a structured and logical manner while promoting code reuse and reducing redundancy.<\/p>\n\n\n\n<figure class=\"wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<iframe title=\"Introduction to Inheritance in C++ | OOP Tutorial for Beginners | Base &amp; Derived Class\" width=\"640\" height=\"360\" src=\"https:\/\/www.youtube.com\/embed\/FIdV0WEeH6U?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe>\n<\/div><\/figure>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p><strong>Definition:<\/strong><br>\u201cInheritance is the process by which one class acquires the properties (data) and behaviors (functions) of another class.\u201d<\/p>\n<\/blockquote>\n\n\n\n<p>In simple words, it means we can create a new class (child or derived class) based on an existing class (parent or base class) so that the new one can reuse and extend the existing functionality.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">2. Why Do We Use Inheritance?<\/h2>\n\n\n\n<p>Let\u2019s take a real-life situation:<\/p>\n\n\n\n<p>Suppose we are building a School Management System.<br>We start with a general class Employee that stores data like name, ID, and salary.<br>Now, we want to create a Teacher class. We could copy all variables again, but that\u2019s code repetition, not a good practice.<\/p>\n\n\n\n<p>Instead, we let Teacher inherit from Employee.<br>This way, all common properties (name, ID, salary) come automatically from Employee.<br>We just add new features like subject or department.<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p><strong>Purpose of inheritance:<\/strong><br>\u201cTo promote code reuse and avoid rewriting the same logic again and again.\u201d<\/p>\n<\/blockquote>\n\n\n\n<h2 class=\"wp-block-heading\">3. Basic Syntax of Inheritance<\/h2>\n\n\n\n<p>Inheritance is declared in C++ using a colon (:) after the class name, followed by an access specifier (usually public) and the base class name.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Base {\n    \/\/ members of base class\n};\n\nclass Derived : public Base {\n    \/\/ members of derived class\n};\n<\/code><\/pre>\n\n\n\n<p>Here,<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>Base<\/code> \u2192 Parent (or base) class<\/li>\n\n\n\n<li><code>Derived<\/code> \u2192 Child (or derived) class<\/li>\n\n\n\n<li><code>public<\/code> \u2192 Access specifier<\/li>\n<\/ul>\n\n\n\n<p>The <code>public<\/code> keyword means that public members of the base class remain public in the derived class.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">4. Example: Simple Inheritance<\/h2>\n\n\n\n<p>Let\u2019s write a simple program showing Teacher inheriting from Employee.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;iostream&gt;\nusing namespace std;\n\nclass Employee {\npublic:\n    string name;\n    int id;\n\n    void displayInfo() {\n        cout &lt;&lt; \"Name: \" &lt;&lt; name &lt;&lt; endl;\n        cout &lt;&lt; \"ID: \" &lt;&lt; id &lt;&lt; endl;\n    }\n};\n\nclass Teacher : public Employee {\npublic:\n    string subject;\n\n    void displayTeacher() {\n        displayInfo(); \/\/ calling base class function\n        cout &lt;&lt; \"Subject: \" &lt;&lt; subject &lt;&lt; endl;\n    }\n};\n\nint main() {\n    Teacher t;\n    t.name = \"Dr. Afzal\";\n    t.id = 101;\n    t.subject = \"Computer Science\";\n\n    t.displayTeacher();\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>Name: Dr. Afzal\nID: 101\nSubject: Computer Science\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Explanation:<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The Teacher class automatically gets the name and id fields and the function displayInfo() from Employee.<\/li>\n\n\n\n<li>It adds a new field subject and a new function displayTeacher().<\/li>\n\n\n\n<li>This shows how code reusability works in inheritance.<\/li>\n<\/ul>\n\n\n\n<figure class=\"wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<iframe title=\"Types of Inheritance in C++ | Multilevel, Hierarchical, Multiple Explained | OOP Tutorial\" width=\"640\" height=\"360\" src=\"https:\/\/www.youtube.com\/embed\/htD5E1FFs34?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe>\n<\/div><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">5. How Constructors Work in Inheritance<\/h2>\n\n\n\n<p>Constructors are special functions that run automatically when an object is created.<br>When a derived class is created, the base class constructor runs first, followed by the derived class constructor.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example:<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;iostream&gt;\nusing namespace std;\n\nclass Employee {\npublic:\n    Employee() {\n        cout &lt;&lt; \"Employee constructor called\" &lt;&lt; endl;\n    }\n};\n\nclass Teacher : public Employee {\npublic:\n    Teacher() {\n        cout &lt;&lt; \"Teacher constructor called\" &lt;&lt; endl;\n    }\n};\n\nint main() {\n    Teacher t;\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>Employee constructor called\nTeacher constructor called\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Explanation:<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>When we create an object of Teacher, C++ first calls the Employee constructor (the base part of the object).<\/li>\n\n\n\n<li>After that, the Teacher constructor is executed.<\/li>\n\n\n\n<li>This ensures that the base class part is properly set up before the derived class adds its own data.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">5.1 How Destructors Work in Inheritance<\/h2>\n\n\n\n<p>Destructors are special functions that are called automatically when an object goes out of scope or is deleted. They are used to release resources like memory, files, or network connections. In inheritance, destructors are called in the <strong>reverse order<\/strong> of constructors.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example:<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;iostream&gt;\nusing namespace std;\n\nclass Base {\npublic:\n    ~Base() {\n        cout &lt;&lt; \"Base Destructor called\" &lt;&lt; endl;\n    }\n};\n\nclass Derived : public Base {\npublic:\n    ~Derived() {\n        cout &lt;&lt; \"Derived Destructor called\" &lt;&lt; endl;\n    }\n};\n\nint main() {\n    Derived d;  \/\/ Constructor of Base then Derived runs automatically.\n    \/\/ When program ends, destructors run in reverse order.\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>Derived Destructor called\nBase Destructor called\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Explanation:<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>When the program finishes or the object goes out of scope, C++ first calls the destructor of the <strong>derived<\/strong> class, then the <strong>base<\/strong> class.<\/li>\n\n\n\n<li>This ensures that resources of the derived class are released first, followed by cleanup of the base class.<\/li>\n\n\n\n<li>This sequence is the opposite of constructor execution order.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Constructor and Destructor Behavior Summary Table<\/h3>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>Aspect<\/th><th>Constructor Behavior<\/th><th>Destructor Behavior<\/th><\/tr><\/thead><tbody><tr><td><strong>Execution Order<\/strong><\/td><td>Base \u2192 Derived<\/td><td>Derived \u2192 Base<\/td><\/tr><tr><td><strong>Purpose<\/strong><\/td><td>Initialize objects and allocate resources<\/td><td>Clean up and release resources<\/td><\/tr><tr><td><strong>Automatic Invocation<\/strong><\/td><td>Automatically called when an object is created<\/td><td>Automatically called when an object goes out of scope or is deleted<\/td><\/tr><tr><td><strong>Virtual in Base Class<\/strong><\/td><td>Constructors cannot be virtual<\/td><td>Destructors can be virtual (important in polymorphism)<\/td><\/tr><tr><td><strong>Memory Management Role<\/strong><\/td><td>Prepares object for use<\/td><td>Ensures proper deallocation<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>This table summarizes how constructors and destructors behave in inheritance, highlighting their sequence and key characteristics.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">5.2 Access Specifiers and Inheritance<\/h2>\n\n\n\n<p>C++ has three access specifiers:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>Specifier<\/th><th>Meaning<\/th><\/tr><\/thead><tbody><tr><td><strong>public<\/strong><\/td><td>Accessible everywhere<\/td><\/tr><tr><td><strong>protected<\/strong><\/td><td>Accessible in the same class and derived classes<\/td><\/tr><tr><td><strong>private<\/strong><\/td><td>Accessible only in the same class<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>When you use inheritance, the access level of base class members may change depending on the inheritance type.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>Access Specifier<\/th><th>Same Class<\/th><th>Derived Class<\/th><th>Outside World<\/th><\/tr><\/thead><tbody><tr><td><strong>public<\/strong><\/td><td>\u2705<\/td><td>\u2705<\/td><td>\u2705<\/td><\/tr><tr><td><strong>protected<\/strong><\/td><td>\u2705<\/td><td>\u2705<\/td><td>\u274c<\/td><\/tr><tr><td><strong>private<\/strong><\/td><td>\u2705<\/td><td>\u274c<\/td><td>\u274c<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>This table visually summarizes access levels and their visibility in different contexts.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">6. Types of Inheritance<\/h2>\n\n\n\n<p>C++ supports several forms of inheritance. Each form defines how classes relate to one another.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Single Inheritance<\/li>\n\n\n\n<li>Multilevel Inheritance<\/li>\n\n\n\n<li>Hierarchical Inheritance<\/li>\n\n\n\n<li>Multiple Inheritance<\/li>\n\n\n\n<li>Hybrid Inheritance<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">6.1 Single Inheritance<\/h3>\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\/07\/2.jpg?resize=640%2C360&#038;ssl=1\" alt=\"\" class=\"wp-image-10062\" srcset=\"https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2025\/07\/2.jpg?resize=1024%2C576&amp;ssl=1 1024w, https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2025\/07\/2.jpg?resize=300%2C169&amp;ssl=1 300w, https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2025\/07\/2.jpg?resize=768%2C432&amp;ssl=1 768w, https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2025\/07\/2.jpg?resize=1536%2C864&amp;ssl=1 1536w, https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2025\/07\/2.jpg?resize=480%2C270&amp;ssl=1 480w, https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2025\/07\/2.jpg?w=1920&amp;ssl=1 1920w, https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2025\/07\/2.jpg?w=1280&amp;ssl=1 1280w\" sizes=\"(max-width: 640px) 100vw, 640px\" \/><figcaption class=\"wp-element-caption\">Single Inheritance<\/figcaption><\/figure>\n\n\n\n<p>In single inheritance, a class is derived from one base class only.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;iostream&gt;\nusing namespace std;\n\nclass Animal {\npublic:\n    void eat() { cout &lt;&lt; \"Eating...\\n\"; }\n};\n\nclass Dog : public Animal {\npublic:\n    void bark() { cout &lt;&lt; \"Barking...\\n\"; }\n};\n\nint main() {\n    Dog d;\n    d.eat();\n    d.bark();\n}\n<\/code><\/pre>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Eating...\nBarking...\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">6.2 Multilevel Inheritance<\/h3>\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\/07\/4.jpg?resize=640%2C360&#038;ssl=1\" alt=\"\" class=\"wp-image-10063\" srcset=\"https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2025\/07\/4.jpg?resize=1024%2C576&amp;ssl=1 1024w, https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2025\/07\/4.jpg?resize=300%2C169&amp;ssl=1 300w, https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2025\/07\/4.jpg?resize=768%2C432&amp;ssl=1 768w, https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2025\/07\/4.jpg?resize=1536%2C864&amp;ssl=1 1536w, https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2025\/07\/4.jpg?resize=480%2C270&amp;ssl=1 480w, https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2025\/07\/4.jpg?w=1920&amp;ssl=1 1920w, https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2025\/07\/4.jpg?w=1280&amp;ssl=1 1280w\" sizes=\"(max-width: 640px) 100vw, 640px\" \/><figcaption class=\"wp-element-caption\">Multi-level Inhertance<\/figcaption><\/figure>\n\n\n\n<p>A derived class can act as a base class for another derived class.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;iostream&gt;\nusing namespace std;\n\nclass Person {\npublic:\n    string name;\n};\n\nclass Employee : public Person {\npublic:\n    int id;\n};\n\nclass Teacher : public Employee {\npublic:\n    string subject;\n    void show() {\n        cout &lt;&lt; \"Name: \" &lt;&lt; name &lt;&lt; \", ID: \" &lt;&lt; id &lt;&lt; \", Subject: \" &lt;&lt; subject &lt;&lt; endl;\n    }\n};\n\nint main() {\n    Teacher t;\n    t.name = \"Sara\";\n    t.id = 10;\n    t.subject = \"Mathematics\";\n    t.show();\n}\n<\/code><\/pre>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Name: Sara, ID: 10, Subject: Mathematics\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">6.3 Hierarchical Inheritance<\/h3>\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\/07\/5.jpg?resize=640%2C360&#038;ssl=1\" alt=\"\" class=\"wp-image-10065\" srcset=\"https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2025\/07\/5.jpg?resize=1024%2C576&amp;ssl=1 1024w, https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2025\/07\/5.jpg?resize=300%2C169&amp;ssl=1 300w, https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2025\/07\/5.jpg?resize=768%2C432&amp;ssl=1 768w, https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2025\/07\/5.jpg?resize=1536%2C864&amp;ssl=1 1536w, https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2025\/07\/5.jpg?resize=480%2C270&amp;ssl=1 480w, https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2025\/07\/5.jpg?w=1920&amp;ssl=1 1920w, https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2025\/07\/5.jpg?w=1280&amp;ssl=1 1280w\" sizes=\"(max-width: 640px) 100vw, 640px\" \/><figcaption class=\"wp-element-caption\">Hierarchical Inheritance<\/figcaption><\/figure>\n\n\n\n<p>One base class is inherited by multiple derived classes.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;iostream&gt;\nusing namespace std;\n\nclass Shape {\npublic:\n    void draw() { cout &lt;&lt; \"Drawing shape...\\n\"; }\n};\n\nclass Circle : public Shape {\npublic:\n    void area() { cout &lt;&lt; \"Calculating area of circle...\\n\"; }\n};\n\nclass Rectangle : public Shape {\npublic:\n    void area() { cout &lt;&lt; \"Calculating area of rectangle...\\n\"; }\n};\n\nint main() {\n    Circle c;\n    Rectangle r;\n    c.draw();\n    r.draw();\n    c.area();\n    r.area();\n}\n<\/code><\/pre>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Drawing shape...\nDrawing shape...\nCalculating area of circle...\nCalculating area of rectangle...\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">6.4 Multiple Inheritance<\/h3>\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\/07\/3.jpg?resize=640%2C360&#038;ssl=1\" alt=\"\" class=\"wp-image-10067\" srcset=\"https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2025\/07\/3.jpg?resize=1024%2C576&amp;ssl=1 1024w, https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2025\/07\/3.jpg?resize=300%2C169&amp;ssl=1 300w, https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2025\/07\/3.jpg?resize=768%2C432&amp;ssl=1 768w, https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2025\/07\/3.jpg?resize=1536%2C864&amp;ssl=1 1536w, https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2025\/07\/3.jpg?resize=480%2C270&amp;ssl=1 480w, https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2025\/07\/3.jpg?w=1920&amp;ssl=1 1920w, https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2025\/07\/3.jpg?w=1280&amp;ssl=1 1280w\" sizes=\"(max-width: 640px) 100vw, 640px\" \/><figcaption class=\"wp-element-caption\">Multiple Inheritance<\/figcaption><\/figure>\n\n\n\n<p>A class can inherit from more than one base class.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;iostream&gt;\nusing namespace std;\n\nclass Person {\npublic:\n    string name;\n    void setName(string n) { name = n; }\n};\n\nclass Worker {\npublic:\n    int hoursWorked;\n    void setHours(int h) { hoursWorked = h; }\n};\n\nclass Engineer : public Person, public Worker {\npublic:\n    void display() {\n        cout &lt;&lt; \"Name: \" &lt;&lt; name &lt;&lt; endl;\n        cout &lt;&lt; \"Hours Worked: \" &lt;&lt; hoursWorked &lt;&lt; endl;\n    }\n};\n\nint main() {\n    Engineer e;\n    e.setName(\"Ali\");\n    e.setHours(40);\n    e.display();\n}\n<\/code><\/pre>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Name: Ali\nHours Worked: 40\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">6.5 Hybrid Inheritance<\/h3>\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\/07\/6.jpg?resize=640%2C360&#038;ssl=1\" alt=\"\" class=\"wp-image-10068\" srcset=\"https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2025\/07\/6.jpg?resize=1024%2C576&amp;ssl=1 1024w, https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2025\/07\/6.jpg?resize=300%2C169&amp;ssl=1 300w, https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2025\/07\/6.jpg?resize=768%2C432&amp;ssl=1 768w, https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2025\/07\/6.jpg?resize=1536%2C864&amp;ssl=1 1536w, https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2025\/07\/6.jpg?resize=480%2C270&amp;ssl=1 480w, https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2025\/07\/6.jpg?w=1920&amp;ssl=1 1920w, https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2025\/07\/6.jpg?w=1280&amp;ssl=1 1280w\" sizes=\"(max-width: 640px) 100vw, 640px\" \/><figcaption class=\"wp-element-caption\">Hybrid Inheritance<\/figcaption><\/figure>\n\n\n\n<p>Hybrid inheritance is a combination of two or more types of inheritance. It often results in the Diamond Problem, which can be resolved using virtual base classes.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;iostream&gt;\nusing namespace std;\n\nclass A {\npublic:\n    void show() { cout &lt;&lt; \"Class A\\n\"; }\n};\n\nclass B : virtual public A {};\nclass C : virtual public A {};\nclass D : public B, public C {};\n\nint main() {\n    D obj;\n    obj.show();\n}\n<\/code><\/pre>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Class A\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Explanation:<\/h3>\n\n\n\n<p>Here, A is inherited virtually to avoid duplication. Hybrid inheritance combines features of multiple, multilevel, and hierarchical inheritance.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">8. Virtual Base Classes \u2013 Solving the Diamond Problem<\/h2>\n\n\n\n<p>The <strong>Diamond Problem<\/strong> in object oriented programming occurs in multiple inheritance when a class inherits from two classes that both originate from the same base class. In such a situation, the derived class receives two separate copies of the members of the base class through different inheritance paths, creating ambiguity when accessing those members. The compiler cannot determine which inherited version of the base class member should be used, which leads to confusion and potential errors in the program. This inheritance structure forms a diamond shaped hierarchy, where the base class appears at the top, two intermediate classes inherit from it, and a final class inherits from both of them.<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p><strong>Definition:<\/strong><br>\u201cA virtual base class ensures that only one shared copy of the base class is inherited, even when it appears multiple times in the inheritance hierarchy.\u201d<\/p>\n<\/blockquote>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;iostream&gt;\nusing namespace std;\n\nclass A {\npublic:\n    void show() { cout &lt;&lt; \"Class A\\n\"; }\n};\n\nclass B : virtual public A {};\nclass C : virtual public A {};\nclass D : public B, public C {};\n\nint main() {\n    D obj;\n    obj.show();\n}\n<\/code><\/pre>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Class A\n<\/code><\/pre>\n\n\n\n<p>Because A is inherited virtually, only one copy of A exists in D, even though B and C both derive from it. This removes ambiguity and saves memory.<\/p>\n\n\n\n<figure class=\"wp-block-embed is-type-rich is-provider-canva wp-block-embed-canva\"><div class=\"wp-block-embed__wrapper\">\n<iframe title=\"Inheritance\" src=\"https:\/\/www.canva.com\/design\/DAHERx0WD9o\/3nUCTPFFLyR6D3Ne-6Yn1g\/view?embed&amp;meta\" height=\"360\" width=\"640\" style=\"border: none; border-radius: 8px; width: 640px; height: 360px;\" allowfullscreen=\"allowfullscreen\" allow=\"fullscreen\"><\/iframe>\n<\/div><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>In the real world, many objects share common characteristics but also have their own specialized features. For example, in a university environment, all people such as students, teachers, and administrative staff share common attributes like name, age, and identification number, yet each role also has its own specific responsibilities and properties. When designing software systems that represent such real world entities, it would be inefficient to repeatedly define the same common properties for every related class. Object Oriented Programming addresses&#8230;<\/p>\n<p class=\"read-more\"><a class=\"btn btn-default\" href=\"https:\/\/afzalbadshah.com\/index.php\/2025\/03\/05\/inheritance-in-c-for-beginners-complete-guide-with-examples-and-real-life-explanation\/\"> Read More<span class=\"screen-reader-text\">  Read More<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":6408,"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":[704,607,603],"class_list":["post-6406","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oop-with-c","tag-c","tag-inheritance","tag-oop"],"aioseo_notices":[],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2025\/07\/Copy-of-OOP-1.jpg?fit=1920%2C1080&ssl=1","jetpack_sharing_enabled":true,"jetpack_likes_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/pf3emP-1Fk","jetpack-related-posts":[],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/afzalbadshah.com\/index.php\/wp-json\/wp\/v2\/posts\/6406","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=6406"}],"version-history":[{"count":15,"href":"https:\/\/afzalbadshah.com\/index.php\/wp-json\/wp\/v2\/posts\/6406\/revisions"}],"predecessor-version":[{"id":42871,"href":"https:\/\/afzalbadshah.com\/index.php\/wp-json\/wp\/v2\/posts\/6406\/revisions\/42871"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/afzalbadshah.com\/index.php\/wp-json\/wp\/v2\/media\/6408"}],"wp:attachment":[{"href":"https:\/\/afzalbadshah.com\/index.php\/wp-json\/wp\/v2\/media?parent=6406"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/afzalbadshah.com\/index.php\/wp-json\/wp\/v2\/categories?post=6406"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/afzalbadshah.com\/index.php\/wp-json\/wp\/v2\/tags?post=6406"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}