17. Object Accessing Inherited Variables

We have already defined two classes:

  • Base class: Device
    • Public variable int id
  • Derived class: Sensor (inherits publicly from Device)
    • Public variable int value
       

Your task is to:

  1. Declare a Sensor object in main().
  2. Assign 101 to id (inherited from Device).
  3. Assign 75 to value (from Sensor).
  4. Print both values in the format:
    Device ID: 101, Sensor Value: 75

Example

Output:

Device ID: 101, Sensor Value: 75

 

Why this output?

The Sensor object contains both its own variable value and the inherited variable id from the Device class.

 

Question Significance

This problem demonstrates public inheritance and how derived class objects access base class members.

Loading...

Input

Expected Output

Device ID: 101, Sensor Value: 75