Concepts of inheritance and types of inheritance

Inheritance lets a new class reuse and extend an existing one (class Student : public Person), and the five shapes it can take are single, multilevel, multiple, hierarchical and hybrid.

10 min read · 10 cards · 2 checks

Read in: English · हिन्दी · ગુજરાતી


Theory

Writing the same class twice

The canteen launches membership cards. You write a Student class: name, ID, show(), plus a 10% student discount. Then staff want cards, so you copy the file, rename to Staff, change the discount to 20%.

Next week the college asks you to add a phone number. You now edit two files, and forget one. Two months later there are five card types and every change means five edits.

Copy-paste is not reuse. C++ has actual reuse: inheritance.

Theory

Family property

A child does not rebuild the family house; they inherit it and add a floor.

In C++, a derived class inherits everything the base class owns (its members) and then adds or adjusts what makes it special. Student and Staff both inherit name, ID and show() from Person; each adds only its own discount rule. One house, many floors, no duplicate bricks.

Theory

Inheritance, formally

Inheritance is the mechanism by which one class (the derived or child class) acquires the properties and behaviours of another (the base or parent class).

Syntax:

class Student : public Person { ... };

Read the colon as "is a kind of". The public there is the inheritance mode: with public inheritance, the base's public members stay public in the child.

Two wins: reusability (write once, use in every child) and hierarchy (the code mirrors real-world is-a relationships).

Practical

Person once, cards forever

#include <iostream>
#include <cstring>
using namespace std;

class Person {                 // base class
protected:
    char name[30];
public:
    void setName(const char n[]) { strcpy(name, n); }
    void show() { cout << "Card holder: " << name << endl; }
};

class Student : public Person { // derived: single inheritance
public:
    double discount() { return 0.10; }
};

class Staff : public Person {   // Person now has two children
public:
    double discount() { return 0.20; }
};

int main() {
    Student s;
    s.setName("Riya");   // inherited from Person
    s.show();            // inherited from Person
    cout << s.discount();// Student's own addition
    return 0;
}

At a glance

The five types of inheritance

TypeShapeExample
SingleA → BPerson → Student
MultilevelA → B → C (chain)Person → Student → CommitteeStudent
MultipleA + B → C (two parents)Student + SportsMember → Captain
HierarchicalA → B, A → C (one parent, many children)Person → Student, Staff, Guest
HybridMix of the aboveHierarchical + multiple combined

Think first

Name that shape

The canteen's classes grow: Person → Student, Person → Staff, and Student → CommitteeStudent. Before tapping: which TWO inheritance types does this diagram contain?

Show the answer

Hierarchical (Person has two children, Student and Staff) and multilevel (the chain Person → Student → CommitteeStudent). Since two pure types are combined in one structure, the whole picture qualifies as hybrid. Exams often draw exactly such a figure and ask you to name every type present, so scan for chains AND fan-outs.

Quiz

class Person has private: char name[30]. class Student : public Person tries to use name directly inside its own member function. What happens?

  1. Compile error: private members are inherited but not accessible in the derived class
  2. It works: children can access everything of the parent
  3. Runtime error: name exists but contains garbage
  4. It works only if Student is in the same file as Person
Show the answer

Compile error: private members are inherited but not accessible in the derived class

Private means private even to children: the member exists inside the Student object but the compiler refuses direct access from Student's code. The standard fix is exactly why protected exists (accessible to the class and its derived classes, still hidden from outside). Option B is the misconception this question hunts; file placement (option D) plays no role in access control.

Watch out

Two traps worth marks

Private is not passed on for use: write "private members are inherited but not accessible in the derived class; protected solves this" and you collect the point exams look for.

Multiple vs multilevel confusion: multiple = two parents at once (allowed in C++, famously not in Java); multilevel = grandparent chain. Hybrid inheritance can create the diamond problem (one grandparent reached by two paths); naming that risk is bonus-mark territory.

Theory

Why the next lessons need this

Inheritance is the stage on which the rest of Unit 2 and all of Unit 3 performs: constructors run base-first when a child is born, and polymorphism (one discount() call behaving differently for Student and Staff) only exists because both children share the Person base. Keep the card family in mind; it returns in every remaining OOP lesson.

Summary

Key takeaways

  • Inheritance: a derived class acquires the members of a base class and extends them.
  • Syntax: class Child : public Parent { }; public inheritance keeps public members public.
  • Five types: single, multilevel (chain), multiple (two parents), hierarchical (fan-out), hybrid (mix).
  • Private members are inherited but inaccessible to the child; protected is the fix.
  • Multiple inheritance is legal in C++ (not Java); hybrid can cause the diamond problem.
  • Benefits: reusability and real-world is-a hierarchies.
  • Memory hook: inherit the house, add a floor.

Study this properly

This page is the lesson to read. In Gri-Learn the same topic is a graded deck: the self-checks are scored and your weak topics are tracked. Free to start.

Start this topic

Already have an account? Sign in

More from Data Encapsulation and inheritance

Gri-Learn · syllabus-mapped B.C.A. lessons in English, Hindi and Gujarati

Concepts of inheritance and types of inheritance · Object Oriented Programming and Data Structures (OOPs & D.S.) · Gri-Learn