The Protected Access Specifier

Medium Encapsulation, Access Specifiers & The this Pointer
Solve in Playground →

Problem Statement

Define a `Base` class with a `protected` integer attribute `secret`. Define a `Derived` class that inherits `public Base`. Inside `Derived`, create a public method `setSecret(int val)` to modify the inherited protected variable, and a `printSecret()` method to print it. In `main()`, use the `Derived` class to set and print the secret.

Input Format

A single integer.

Output Format

Print the formatted secret.

Constraints

None

Sample Input

42

Sample Output

Secret: 42

Explanation

Protected members act like private members to the outside world, but are accessible directly within derived child classes.

Starter Code

#include <iostream>

class Base {
    // Define protected secret here
};

class Derived : public Base {
public:
    // Implement setSecret() and printSecret()
};

int main() {
    // Instantiate Derived, read input, and execute
    return 0;
}

Limits

  • Time Limit: 1s
  • Memory Limit: 256MB

Embedded C Programming

Updated: March 15, 2026
Intermediate

Embedded systems rely on efficient low-level programming to interact directly with hardware. In this course, you will learn how to write practical Embedded C programs used in real microcontroller-based systems. Rather than focusing only on theory, this course follows a practice-driven approach. Each lesson includes hands-on coding exercises that simulate real firmware development tasks used