Object Pointer Access

#include <iostream>
using namespace std;

class Sensor {
public:
    int value;
    void printValue() {
        // Fix the typo: 'Cout' should be 'cout'
        cout << "Sensor Value: " << value << "\n";
    }
};

int main() {
    Sensor x; // Declared the actual Sensor object 'x'
    
    // 1. Declare a Sensor object pointer and make it point to 'x'
    Sensor* pointer_x = &x; 

    // 2. Assign value using the arrow operator (->)
    // The arrow operator (->) is used to access members of an object through a pointer.
    pointer_x->value = 88;
    
    // 3. Call printValue() using the arrow operator (->)
    pointer_x->printValue(); 
    
    return 0;
}
Upvote
Downvote
Loading...

Input

Expected Output

Sensor Value: 88