Multilevel Inheritance

Multilevel Inheritance 
 

Introduction
Inheritance is a fundamental concept in object-oriented programming (OOP) that allows one class to inherit the properties and behavior of another class. The inheriting class, also known as the derived class or subclass, inherits all the fields and methods of the parent class, also known as the base class or superclass.


What is Multi-Level Inheritance?

Multi-level inheritance is a type of inheritance in which a derived class inherits properties and behavior from a base class, and the base class itself inherits properties and behavior from another base class. This creates a hierarchical relationship between classes, where a derived class can access and build upon the properties and behavior of multiple base classes.


Key features points:

1. *Hierarchical relationship*: A derived class inherits properties and behavior from a base class, which itself inherits from another base class.
2. *Multiple levels of inheritance*: A derived class can inherit from a base class that has already inherited from another base class.
3. *Access to multiple base classes*: A derived class can access and build upon the properties and behavior of multiple base classes.
4. *Inheritance chain*: The inheritance relationship forms a chain, with each derived class inheriting from the previous base class.
5. *Code reusability*: Multilevel inheritance promotes code reusability by allowing derived classes to inherit and build upon existing code.
6. *Complexity management*: Multilevel inheritance can help manage complexity by breaking down a complex system into smaller, more manageable pieces.
Multilevel diagram:
Bass class explanation:

A base class is a parent class from which other classes can inherit properties and behavior. It provides a common interface and implementation that can be shared by its derived classes. The base class is also known as the superclass or parent class.

Derived class explanation:

A derived class is a child class that inherits properties and behavior from a base class. It builds upon the foundation provided by the base class and can add new attributes and methods or override existing ones. The derived class is also known as the subclass or child class.

Program multilevel :

                             students record program 

#include <iostream>
using namespace std;

// Base class for person information
class Person {
protected:
    string name;
    int age;

public:
    // Constructor to initialize person details
    Person(string n, int a) : name(n), age(a) {}

    // Method to display person details
    void displayPerson() {
        cout << "Name: " << name << endl;
        cout << "Age: " << age << endl;
    }
};

// Derived class 1: Student class inherits from Person
class Student : public Person {
protected:
    int rollNumber;
public:
    // Constructor to initialize student-specific details
    Student(string n, int a, int roll) : Person(n, a), rollNumber(roll) {}

    // Method to display student details
    void displayStudent() {
        displayPerson(); // Call method from base class
        cout << "Roll Number: " << rollNumber << endl;
    }
};

// Derived class 2: AcademicRecord class inherits from Student
class AcademicRecord : public Student {
private:
    float grade;

public:
    // Constructor to initialize academic record details
    AcademicRecord(string n, int a, int roll, float g)
        : Student(n, a, roll), grade(g) {}

    // Method to display academic details
    void displayAcademicRecord() {
        displayStudent(); // Call method from Derived class 1
        cout << "Grade: " << grade << endl;
    }
};

int main() {
    // Create an object of AcademicRecord (Derived2 class)
    AcademicRecord student1("John Doe", 20, 101, 88.5);

    // Display the complete student record
    cout << "Student Record:" << endl;
    student1.displayAcademicRecord();

    return 0;
}

Output:

             Student Record:
             Name: John Doe
             Age: 20
             Roll Number: 101
             Grade: 88.5
Explanation of the Student Record Program (Multilevel Inheritance in C++)

The program demonstrates multilevel inheritance using classes that represent a student's personal details, student-specific details, and academic record. Let's break it down step by step:

1. Base Class: Person

This is the base class that holds basic personal information like name and age.

class Person {
protected:
    string name;
    int age;

public:
    // Constructor to initialize person details
    Person(string n, int a) : name(n), age(a) {}

    // Method to display person details
    void displayPerson() {
        cout << "Name: " << name << endl;
        cout << "Age: " << age << endl;
    }
};

Attributes:

name: Stores the name of the person.

age: Stores the age of the person.


Constructor:

Initializes the name and age attributes using the constructor.


Method (displayPerson):

Displays the name and age of the person.



2. Derived Class 1: Student

This class inherits from the Person class and adds student-specific attributes like rollNumber.

class Student : public Person {
protected:
    int rollNumber;

public:
    // Constructor to initialize student-specific details
    Student(string n, int a, int roll) : Person(n, a), rollNumber(roll) {}

    // Method to display student details
    void displayStudent() {
        displayPerson(); // Call method from base class
        cout << "Roll Number: " << rollNumber << endl;
    }
};

Attributes:

rollNumber: Stores the student's roll number.


Constructor:

Calls the Person constructor to initialize name and age (using the base class's constructor).

Initializes the rollNumber.


Method (displayStudent):


                                          Prepare by, Karthick.M
                                         jai krishna.M
                                 1,st B.SC computer science 

DATE:7/03/2025

Comments

Popular posts from this blog

ETHICAL HACKING - WHITE HAT HACKER

Radio Waves

Semantic Technology