---
title: "Protected specifier; student/test/result program"
subject: "Object Oriented Programming"
chapter: 6
key: "semester_3/object_oriented_programming:protected-three-class-program"
source: https://www.bctengineer.com/q/semester_3/object_oriented_programming/protected-three-class-program
site: BCT Engineering
---

# Protected specifier; student/test/result program

Object Oriented Programming · Chapter 6: Inheritance

- Asked in: 2070 Chaitra, 3+7 marks
- Video: [Access Modifiers/ Access Specifiers in C++ | C++ Tutorials in हिंदी/English | Tutorial - 15](https://www.youtube.com/watch?v=R552gvEswHU) — The Study Corner
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_3/object_oriented_programming/protected-three-class-program
- Whole chapter: https://www.bctengineer.com/md/semester_3/object_oriented_programming/chapter-6

## Question

What is a protected access specifier? Write a program with three classes students, test and result by using multilevel inheritance. Assume necessary data members and functions yourself and program with input information, input data and calculate marks total and display result.

## Short answer

A protected member is accessible within its class and derived classes, hidden from outside. A multilevel program: Student holds roll/name, Test adds three subject marks, Result computes total and percentage, and displays pass/fail if all marks ≥40.

## Answer

#### Protected access specifier
A member declared `protected` is accessible within its own class and in classes derived from it, but not from outside code (e.g. `main()`). It lies between `private` (only the class) and `public` (everyone). It lets derived classes use base data while still hiding it from the outside.

#### Program: multilevel inheritance `Student -> Test -> Result`

- `Student` stores roll number and name.
- `Test` (derived from `Student`) stores marks in three subjects.
- `Result` (derived from `Test`) calculates total and percentage and displays the result.

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

class Student {
protected:
    int roll;
    string name;
public:
    void getInfo() {
        cout << "Enter roll number and name: ";
        cin >> roll >> name;
    }
};

class Test : public Student {
protected:
    float m1, m2, m3;
public:
    void getMarks() {
        cout << "Enter marks in 3 subjects: ";
        cin >> m1 >> m2 >> m3;
    }
};

class Result : public Test {
    float total, percent;
public:
    void calculate() {
        total = m1 + m2 + m3;
        percent = total / 3;
    }
    void display() {
        cout << "\nRoll: " << roll << "  Name: " << name << endl;
        cout << "Marks: " << m1 << ", " << m2 << ", " << m3 << endl;
        cout << "Total = " << total << ", Percentage = " << percent << "%" << endl;
        if (m1 >= 40 && m2 >= 40 && m3 >= 40)
            cout << "Result: Pass" << endl;
        else
            cout << "Result: Fail" << endl;
    }
};

int main() {
    Result r;
    r.getInfo();
    r.getMarks();
    r.calculate();
    r.display();
    return 0;
}
```

**Sample run:**

```text
Enter roll number and name: 7 Sita
Enter marks in 3 subjects: 78 65 82

Roll: 7  Name: Sita
Marks: 78, 65, 82
Total = 225, Percentage = 75%
Result: Pass
```

`Result` reads `roll`, `name`, `m1`, `m2` and `m3` directly because they are `protected` in their parent classes.

## Check yourself

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

1. Where can a protected member of a class be accessed?
   A. Only inside its own class
   B. Inside its own class and derived classes, but not from main()
   C. From anywhere in the program
   D. Only from derived classes, not in its own class
   Answer: B. Protected lies between private and public: the class and its derived classes can use it, outside code such as main() cannot. (Key point: Meaning of protected)

2. Why are roll and name declared protected in Student rather than private?
   A. So main() can read them directly
   B. So Test and Result can use them directly while outsiders cannot
   C. Because private members cannot be stored in objects
   D. Because protected members are initialised automatically
   Answer: B. Protected lets derived classes access base data directly while still hiding it from outside code. (Key point: Why Student data is protected)

3. In the program, what is the inheritance chain?
   A. Student -> Result -> Test
   B. Test -> Student -> Result
   C. Student -> Test -> Result
   D. Student, Test -> Result
   Answer: C. Test derives from Student and Result derives from Test, forming a multilevel chain, so Result inherits everything. (Key point: Multilevel chain)

4. For marks 78, 65, 82 in the sample run, what do total and percentage (total/3) come out as?
   A. 75 and 225
   B. 235 and 78.3
   C. 225 and 225
   D. 225 and 75
   Answer: D. 78+65+82 = 225, and 225/3 = 75. (Key point: Total and percentage)

## Related in Object Oriented Programming

- [Constructor and destructor order, multilevel inheritance](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Actor-order-multilevel) — Object Oriented Programming, chapter 6, asked 2×
- [Student file displayed in descending marks order](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Astudent-file-descending) — Object Oriented Programming, chapter 8
- [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 specifiers in inheritance](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Aaccess-specifiers-inheritance) — Object Oriented Programming, chapter 6
- [Abstract Student class program](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Astudent-abstract-program) — Object Oriented Programming, chapter 7
- [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×

## Related in other subjects

- [Pass student array; display passed students](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Apass-whole-array-student-info) — Computer Programming, chapter 7
- [Student records with two functions](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Astruct-student-read-display-functions) — Computer Programming, chapter 8
- [96 students record; pass marks](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Afile-96-students-ioe-txt) — Computer Programming, chapter 10
- [Normal: IOE marks of 5000 students](https://www.bctengineer.com/question.md?key=semester_5%2Fprobability_and_statistics%3Anormal-5000-students-marks) — Probability and Statistics, chapter 3
