
Inheritance in C++ for Beginners: Complete Guide with Examples and Real-Life Explanation
Inheritance is one of the core ideas in object-oriented programming. It allows one class to use the properties and behavior of another class. In simple terms, inheritance helps us create a new class based on an existing class. The existing class is called the base class or parent class, and the new class is called the derived class or child class. Visit the detailed tutorial here.
What is the purpose of inheritance?
Suppose we are building a system for a school. We create a class called “Employee” that contains common information like name, ID, and salary. Now, we want to create a “Teacher” class. Instead of writing all the properties again, we let the “Teacher” class inherit from the “Employee” class. This way, Teacher automatically gets all the common features from Employee. We can then add teacher-specific features like subject or department. This is called code reusability. We do not repeat code unnecessarily, and we build a more organized structure where each class plays its specific role.
Basic syntax of inheritance
In C++, inheritance is declared using a colon and an access specifier (usually public). Here is the general format:
class Base {
// members of base class
};
class Derived : public Base {
// members of derived class
};
The word “public” in “: public Base” means that the public members of the base class will remain public in the derived class. Beginners should always start with public inheritance.
Simple example of inheritance
#include <iostream>
using namespace std;
class Employee {
public:
string name;
int id;
void displayInfo() {
cout << "Name: " << name << endl;
cout << "ID: " << id << endl;
}
};
class Teacher : public Employee {
public:
string subject;
void displayTeacher() {
displayInfo();
cout << "Subject: " << subject << endl;
}
};
int main() {
Teacher t;
t.name = "Dr. Afzal";
t.id = 101;
t.subject = "Computer Science";
t.displayTeacher();
return 0;
}
In this example, Teacher is inheriting from Employee. The Teacher class gets access to the name and id fields, and also to the displayInfo function. It also has its own field called subject and a function called displayTeacher. When you run the program, it prints the name, ID, and subject of the teacher. This is inheritance in action. The Teacher class is using both inherited and its own members.
How constructors behave in inheritance
Constructors are special functions that are automatically called when an object is created. In inheritance, when a derived class object is created, the base class constructor runs first.
Example:
class Employee {
public:
Employee() {
cout << "Employee constructor called" << endl;
}
};
class Teacher : public Employee {
public:
Teacher() {
cout << "Teacher constructor called" << endl;
}
};
int main() {
Teacher t;
return 0;
}
Output:
Employee constructor called
Teacher constructor called
Multiple Inheritance Example and Functionality
In multiple inheritance, a class derives from more than one base class. Each base class may have its own data and functions. The derived class can use the features of all base classes.
Example:
#include <iostream>
using namespace std;
class Person {
public:
string name;
void setName(string n) {
name = n;
}
};
class Worker {
public:
int hoursWorked;
void setHours(int h) {
hoursWorked = h;
}
};
class Engineer : public Person, public Worker {
public:
void display() {
cout << "Name: " << name << endl;
cout << "Hours Worked: " << hoursWorked << endl;
}
};
int main() {
Engineer e;
e.setName("Ali");
e.setHours(40);
e.display();
return 0;
}
In this program, the class Engineer
inherits from both Person
and Worker
. It gains the ability to store a name and number of hours worked, then display both using its own function. This is how multiple inheritance brings functionality from two or more base classes into one derived class.
Access specifiers and inheritance
C++ has three access specifiers: public, protected, and private.
- Public: accessible everywhere.
- Protected: accessible in the same class and derived class.
- Private: accessible only in the same class.
When you inherit a class publicly, the public members of the base class stay public in the derived class.
Real-life analogy to summarize
Think about vehicles. You have a base class called Vehicle. It has functions like start() and stop(). Now you create a derived class called Car. It automatically gets start() and stop(), but it may also have its own function openSunroof(). Similarly, a Truck class may have its own function loadCargo(). This is how we build specialized classes from a general one.
Another example: Person and Student
class Person {
public:
string name;
int age;
void showPerson() {
cout << "Name: " << name << ", Age: " << age << endl;
}
};
class Student : public Person {
public:
int rollNumber;
void showStudent() {
showPerson();
cout << "Roll Number: " << rollNumber << endl;
}
};
In this example, the Student is a Person and also has a roll number.
Inheritance allows you to create a new class that can reuse the features of an existing class. It helps in making your code more organized, more reusable, and easier to manage. You can use inheritance to model real-world relationships where one entity is a specialized version of another. If your students understand the concept of “is-a” relationship, they can use inheritance correctly. A Teacher is an Employee. A Student is a Person. An Engineer is both a Person and a Worker. These examples help them map the real world into code.