Inheritance in C++
Imagine you’re designing ID cards for your school. Every student and every teacher needs certain common fields: name, age, and ID number.
But teachers also need “subject” and “department,” while students need “class” and “section.”
Instead of rewriting shared information twice, you put common details in one place and extend it for teachers or students.
C++ does this using inheritance, allowing one class to reuse and expand another class’s code, just like building on a foundation instead of starting from scratch.
Student and Teacher Profiles Using Inheritance
You will create:
- A Base class: Person (name, age)
- A Derived class: Student (inherits name & age and adds class level)
- A Derived class: Teacher (inherits name & age and adds subject)
Expected output:
1 2 3 4 5 6 7 8 9 10 11 12 13======================== STUDENT INFO Name: Ali Age: 16 Class: 10 ======================== ======================== TEACHER INFO Name: Miss Sara Age: 30 Subject: Math ========================
This shows how a single base class can be reused for multiple related objects.
What Is Inheritance?
Inheritance allows one class (child) to acquire the properties and functions of another class (parent). It promotes code reuse and reduces duplication. It models real-world relationships like Student → Person.
Base Class (Parent Class)
A base class contains common or shared attributes and behaviors. Other classes inherit these features from it. It acts as the foundation for related classes.
Example
1class Person { public: string name; int age; };
Derived Class (Child Class)
A derived class extends the base class by adding new features.
It gains all accessible members of the parent automatically.
This lets you write less code and maintain shared logic easily.
Example
1class Student : public Person { public: int classNo; };
Public Inheritance
The most common form: the public members of the base remain public in the derived class. This models “is-a” relationships such as: A Student is a Person.
It allows clear and structured class hierarchies.
Inheritance removes the need to repeat shared fields like name and age.
You write the base class once and reuse it everywhere.
This makes programs cleaner and easier to maintain.
Function Inheritance
Derived classes inherit functions too, not just variables.
You can directly use or override them.
This keeps behavior consistent across related classes.
Build the Person–Student–Teacher Program
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56#include <iostream> using namespace std; // Base Class class Person { public: string name; int age; }; // Derived Class - Student class Student : public Person { public: int classNo; void display() { cout << "========================" << endl; cout << " STUDENT INFO" << endl; cout << " Name: " << name << endl; cout << " Age: " << age << endl; cout << " Class: " << classNo << endl; cout << "========================" << endl; } }; // Derived Class - Teacher class Teacher : public Person { public: string subject; void display() { cout << "========================" << endl; cout << " TEACHER INFO" << endl; cout << " Name: " << name << endl; cout << " Age: " << age << endl; cout << " Subject: " << subject << endl; cout << "========================" << endl; } }; int main() { Student s; s.name = "Ali"; s.age = 16; s.classNo = 10; Teacher t; t.name = "Miss Sara"; t.age = 30; t.subject = "Math"; s.display(); t.display(); return 0; }
How It Works
- Student and Teacher automatically inherit name and age from Person.
- Each derived class adds unique properties (classNo, subject).
- Both can use the base class fields without redefining them.
Try it instantly in the DevsCall AI Code Runner.
Learn Together with AI
Try prompts like:
- “Explain inheritance using everyday examples.”
- “Create a base class Animal and derived classes Dog and Cat.”
- “Show how overriding works in C++ with a simple example.”
- “When should I use public inheritance vs private?”
AI can generate variations, correct errors, and help you build intuition.
Practice Time
Write a C++ program that:
- Creates a base class Vehicle with fields: brand, year
- Creates a derived class Car with field: seats
- Creates a derived class Bike with field: type (e.g., Sports, Regular)
- Prints output like:
1 2 3 4 5 6 7 8 9 10 11 12 13======================== CAR DETAILS Brand: Honda Year: 2019 Seats: 4 ======================== ======================== BIKE DETAILS Brand: Yamaha Year: 2021 Type: Sports ========================
Ask AI for help:
“How do I create multiple derived classes from the same base in C++?”
Frequently Asked Questions
Inheritance allows one class (child) to reuse and extend the properties and functions of another class (parent), reducing repetition and improving structure.
A base class contains shared attributes and behaviors. Other classes inherit these common features from it.
A derived class extends the base class by adding new variables or functions. It inherits all accessible members of the parent.
It helps avoid repeating code, keeps programs organized, allows consistency across related classes, and models real-world relationships naturally.
Yes. Derived classes can redefine functions inherited from the base class to provide specialized behavior.
Absolutely. A single base class can be extended into many specialized classes, like Person → Student, Person → Teacher.
No. You can run all inheritance-based examples in the DevsCall AI Code Runner without installing anything.
Still have questions?Contact our support team