3. Private Access

Your task is to define a class named Sensor with:

  • private variable value (type int).
  • public method setValue(int v) to set it.
  • public method getValue() to return it.

     

The program will:

  1. Create a Sensor object.
  2. Call setValue(75).
  3. Print Sensor value: 75.
     

Example

Output:

Sensor value: 75

 

Why this output?

The private variable is set through the setter method and read through the getter.

 

Question Significance

Demonstrates encapsulation — private data with public access methods.

Loading...

Input

Expected Output

Sensor value: 75