Programming Pandit

c/c++/c#/Javav/Python


Latest Update

Monday, August 17, 2026

To develop a C++ class hierarchy for different types of inheritance, including single, multilevel, hierarchical and multiple inheritance.

 

Experiment 5

Aim

To develop a C++ class hierarchy for different types of inheritance, including single, multilevel, hierarchical and multiple inheritance.

Objectives

  • To understand inheritance.
  • To implement different forms of inheritance.
  • To understand code reusability through inheritance.
  • To develop a class hierarchy.

Theory

Inheritance allows one class to acquire the properties and behaviour of another class.

Major types include:

  • Single inheritance
  • Multilevel inheritance
  • Multiple inheritance
  • Hierarchical inheritance

Program

#include <iostream>

using namespace std;

 

class Person {

public:

    void person() {

        cout << "Person\n";

    }

};

 

class Student : public Person {

public:

    void student() {

        cout << "Student\n";

    }

};

 

class Result : public Student {

public:

    void result() {

        cout << "Result\n";

    }

};

 

class Teacher : public Person {

public:

    void teacher() {

        cout << "Teacher\n";

    }

};

 

class Sports {

public:

    void sports() {

        cout << "Sports\n";

    }

};

 

class CollegeStudent : public Student, public Sports {

public:

    void collegeStudent() {

        cout << "College Student\n";

    }

};

 

int main() {

    cout << "Multilevel Inheritance:\n";

    Result r;

    r.person();

    r.student();

    r.result();

 

    cout << "\nHierarchical Inheritance:\n";

    Teacher t;

    t.person();

    t.teacher();

 

    cout << "\nMultiple Inheritance:\n";

    CollegeStudent c;

    c.student();

    c.sports();

    c.collegeStudent();

 

    return 0;

}

Result

Thus, a C++ class hierarchy was successfully developed to demonstrate different forms of inheritance.

No comments:

Post a Comment