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 usecout
andcin
for printing and taking input.using namespace std;
: This line lets you use standard library features (likecout
andcin
) 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
Person
is 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
Student
class is derived from thePerson
class using the syntaxclass Student : public Person
. This meansStudent
inherits all public and protected members ofPerson
. - New Member:
int studentID;
: This is a new data member specific to theStudent
class, storing the student’s ID.
- New Method:
void displayStudentInfo()
: This method is specific to theStudent
class. 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 objects1
of theStudent
class.
Setting Attributes:
s1.name = "John Doe";
ands1.age = 20;
: These lines set thename
andage
attributes, whichs1
inherits from thePerson
class.s1.studentID = 12345;
: This line sets thestudentID
, which is specific to theStudent
class.
Calling the Method:
s1.displayStudentInfo();
: This line calls thedisplayStudentInfo()
method on thes1
object. This method prints the name, age (from thePerson
class), and student ID (from theStudent
class).