Index
Introduction
Inheritance is a fundamental concept in object-oriented programming (OOP) that allows one class to inherit properties and behaviors (fields and methods) from another class. It enables code reusability and establishes a relationship between classes.
Basic Concepts
- Base Class (Parent Class): The class whose properties and methods are inherited by another class.
- Derived Class (Child Class): The class that inherits properties and methods from the base class.
Why Use Inheritance?
Inheritance allows you to create a new class based on an existing class, reusing code and creating a hierarchy of classes that share common attributes and behaviors.
Code
#include <iostream>
using namespace std;
// Base Class
class Person {
public:
string name;
int age;
void displayInfo() {
cout << "Name: " << name << endl;
cout << "Age: " << age << endl;
}
};
// Derived Class
class Student : public Person {
public:
int studentID;
void displayStudentInfo() {
// displayInfo() is inherited from the Person class
displayInfo();
cout << "Student ID: " << studentID << endl;
}
};
int main() {
Student s1;
s1.name = "John Doe"; // Inherited from Person
s1.age = 20; // Inherited from Person
s1.studentID = 12345; // Specific to Student
s1.displayStudentInfo(); // Calls the method from the Student class
return 0;
}
Output:
Name: John Doe
Age: 20
Student ID: 12345
Explanation:
Step 1: Including the Required Library
#include <iostream>
using namespace std;
#include <iostream>: This line includes the input-output stream library, which allows you to usecoutandcinfor printing and taking input.using namespace std;: This line lets you use standard library features (likecoutandcin) without prefixing them withstd::.
Step 2: Defining the Base Class (Person)
class Person {
public:
string name;
int age;
void displayInfo() {
cout << "Name: " << name << endl;
cout << "Age: " << age << endl;
}
};
- Class Definition: The class
Personis defined with the keywordclass. - Public Members:
string name;andint age;: These are public data members that store the person’s name and age.void displayInfo(): This is a public member function that prints the name and age of the person.
Step 3: Defining the Derived Class (Student)
class Student : public Person {
public:
int studentID;
void displayStudentInfo() {
displayInfo();
cout << "Student ID: " << studentID << endl;
}
};
- Inheritance: The
Studentclass is derived from thePersonclass using the syntaxclass Student : public Person. This meansStudentinherits all public and protected members ofPerson. - New Member:
int studentID;: This is a new data member specific to theStudentclass, storing the student’s ID.
- New Method:
void displayStudentInfo(): This method is specific to theStudentclass. It calls the inheriteddisplayInfo()method to print the name and age, and then it prints thestudentID.
Step 4: The ‘main’ Function
int main() {
Student s1;
s1.name = "John Doe"; // Inherited from Person
s1.age = 20; // Inherited from Person
s1.studentID = 12345; // Specific to Student
s1.displayStudentInfo(); // Calls the method from the Student class
return 0;
}
Creating an Object:
Student s1;: This line creates an objects1of theStudentclass.
Setting Attributes:
s1.name = "John Doe";ands1.age = 20;: These lines set thenameandageattributes, whichs1inherits from thePersonclass.s1.studentID = 12345;: This line sets thestudentID, which is specific to theStudentclass.
Calling the Method:
s1.displayStudentInfo();: This line calls thedisplayStudentInfo()method on thes1object. This method prints the name, age (from thePersonclass), and student ID (from theStudentclass).

