---
title: "Student, teaching and non-teaching staff from person"
subject: "Object Oriented Programming"
chapter: 6
key: "semester_3/object_oriented_programming:person-student-staff-program"
source: https://www.bctengineer.com/q/semester_3/object_oriented_programming/person-student-staff-program
site: BCT Engineering
---

# Student, teaching and non-teaching staff from person

Object Oriented Programming · Chapter 6: Inheritance

- Asked in: 2070 Asar, 6 marks
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_3/object_oriented_programming/person-student-staff-program
- Whole chapter: https://www.bctengineer.com/md/semester_3/object_oriented_programming/chapter-6

## Question

Write a program to create classes to represent student, teaching staffs and non-teaching staffs from the base class person. Use proper members in the classes to make your program meaningful.

## Short answer

The program uses hierarchical inheritance: a base class Person with name, age, address; derived Student (oll, program), TeachingStaff (epartment, subject), and NonTeachingStaff (epartment, post). Each defines getdata() and display() to input and output details, demonstrating reuse.

## Answer

Design: a base class `Person` holds common data (name, age, address). Three classes derive from it publicly: `Student` (roll number, programme), `TeachingStaff` (department, subject) and `NonTeachingStaff` (department, post). Each class has `getdata()` and `display()`.

```cpp
#include <iostream>
#include <string>
using namespace std;

class Person {
protected:
    string name, address;
    int age;
public:
    void getPerson() {
        cout << "Name: ";    cin >> name;
        cout << "Age: ";     cin >> age;
        cout << "Address: "; cin >> address;
    }
    void showPerson() {
        cout << "Name: " << name << ", Age: " << age
             << ", Address: " << address << endl;
    }
};

class Student : public Person {
    int roll;
    string program;
public:
    void getdata() {
        getPerson();
        cout << "Roll no: ";   cin >> roll;
        cout << "Programme: "; cin >> program;
    }
    void display() {
        cout << "--- Student ---" << endl;
        showPerson();
        cout << "Roll: " << roll << ", Programme: " << program << endl;
    }
};

class TeachingStaff : public Person {
    string department, subject;
public:
    void getdata() {
        getPerson();
        cout << "Department: "; cin >> department;
        cout << "Subject: ";    cin >> subject;
    }
    void display() {
        cout << "--- Teaching Staff ---" << endl;
        showPerson();
        cout << "Dept: " << department << ", Subject: " << subject << endl;
    }
};

class NonTeachingStaff : public Person {
    string department, post;
public:
    void getdata() {
        getPerson();
        cout << "Department: "; cin >> department;
        cout << "Post: ";       cin >> post;
    }
    void display() {
        cout << "--- Non-teaching Staff ---" << endl;
        showPerson();
        cout << "Dept: " << department << ", Post: " << post << endl;
    }
};

int main() {
    Student s;
    TeachingStaff t;
    NonTeachingStaff n;

    cout << "Enter student details" << endl;           s.getdata();
    cout << "Enter teaching staff details" << endl;    t.getdata();
    cout << "Enter non-teaching staff details" << endl; n.getdata();

    s.display();
    t.display();
    n.display();
    return 0;
}
```

This is **hierarchical inheritance**: one base class (`Person`) with three derived classes. The common data is written once in `Person` and reused, which shows the reusability benefit of inheritance. Members are `protected` so derived classes can access them but outside code cannot.

## Check yourself

The answer's key points as multiple-choice questions, with the correct option and why.

1. Which inheritance type is used for Person with Student, TeachingStaff and NonTeachingStaff?
   A. Multilevel
   B. Multiple
   C. Hierarchical
   D. Hybrid
   Answer: C. One base class has three derived classes, which is hierarchical. (Key point: Type of inheritance)

2. Why are the Person members declared protected?
   A. So they are hidden from derived classes too
   B. So derived classes can use them but outside code cannot
   C. So they become static
   D. So they are public
   Answer: B. Protected gives access to derived classes while hiding from outside. (Key point: Protected members)

3. What benefit does placing name, age and address in Person show?
   A. Faster execution
   B. Smaller object files
   C. Reusability: common data is written once
   D. Automatic input validation
   Answer: C. Common data is defined once and reused by all derived classes. (Key point: Reusability)

4. Which is a member of TeachingStaff, not of Person?
   A. name
   B. age
   C. address
   D. subject
   Answer: D. Subject and department are specific to teaching staff. (Key point: Derived members)

## Related in Object Oriented Programming

- [Abstract Student class program](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Astudent-abstract-program) — Object Oriented Programming, chapter 7
- [Manager derived from person and employee](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Amanager-person-employee) — Object Oriented Programming, chapter 6
- [Protected specifier; student/test/result program](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Aprotected-three-class-program) — Object Oriented Programming, chapter 6
- [File stream; student binary file program](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Afile-stream-student-binary) — Object Oriented Programming, chapter 8
- [Access overridden members from derived class](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Aaccess-overridden-members) — Object Oriented Programming, chapter 6, asked 2×
- [Effect of derivation types on class members](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Aderivation-types-effect) — Object Oriented Programming, chapter 6

## Related in other subjects

- [Create classes from design class diagrams](https://www.bctengineer.com/question.md?key=semester_6%2Fobject_oriented_analysis_and_design%3Aclasses-from-design-class-diagram) — Object Oriented Analysis and Design, chapter 4, asked 2×
- [Identify conceptual classes; create domain model](https://www.bctengineer.com/question.md?key=semester_6%2Fobject_oriented_analysis_and_design%3Aconceptual-classes-domain-model-steps) — Object Oriented Analysis and Design, chapter 2
- [Student course department class diagram](https://www.bctengineer.com/question.md?key=semester_6%2Fobject_oriented_analysis_and_design%3Astudent-course-diagram-type) — Object Oriented Analysis and Design, chapter 2
- [Interpret: teaching values to professionals](https://www.bctengineer.com/question.md?key=semester_5%2Fcommunication_english%3Ainterpret-teaching-moral-values-professionals) — Communication English, chapter 1, asked 2×
