Object Accessing Inherited Variables

#include <iostream>
using namespace std;

class Device {
public:
    int id;
    Device(int id_val){
        id = id_val;
    }
};

class Sensor : public Device {
public:
    int value;
    Sensor(int val, int id) : Device(id), value(val){}
};

int main() {
    // your code here: declare Sensor object, assign id and value, and print them
    Sensor Sonar(75, 101);
    cout << "Device ID: " << Sonar.id 
         << ", Sensor Value: " << Sonar.value << endl;
    return 0;
}
Upvote
Downvote
Loading...

Input

Expected Output

Device ID: 101, Sensor Value: 75