Extra 5% OFF Use Code: OL05
Free Shipping over ₹999

Inheritance

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 use cout and cin for printing and taking input.
  • using namespace std;: This line lets you use standard library features (like cout and cin) without prefixing them with std::.

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 keyword class.
  • Public Members:
    • string name; and int 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 the Person class using the syntax class Student : public Person. This means Student inherits all public and protected members of Person.
  • New Member:
    • int studentID;: This is a new data member specific to the Student class, storing the student’s ID.
  • New Method:
    • void displayStudentInfo(): This method is specific to the Student class. It calls the inherited displayInfo() method to print the name and age, and then it prints the studentID.

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 object s1 of the Student class.

Setting Attributes:

  • s1.name = "John Doe"; and s1.age = 20;: These lines set the name and age attributes, which s1 inherits from the Person class.
  • s1.studentID = 12345;: This line sets the studentID, which is specific to the Student class.

Calling the Method:

  • s1.displayStudentInfo();: This line calls the displayStudentInfo() method on the s1 object. This method prints the name, age (from the Person class), and student ID (from the Student class).

    Leave a Reply

    Your email address will not be published.

    Need Help?