Object Accessing Inherited Variables

#include <iostream>
using namespace std;

class Device {
public:
    int id;
    void print() {
        std::cout << "Device ID: " << id << std::endl;
    }
};

class Sensor : public Device {
public:
    int value;
    void print() {
        std::cout << "Device ID: " << id  << ", Sensor Value: " << value << std::endl;
    }
};

int main() {
    // your code here: declare Sensor object, assign id and value, and print them
    Sensor sensei{};
    sensei.id = 101;
    sensei.value = 75;
    sensei.print();
    return 0;
}
Upvote
Downvote
Loading...

Input

Expected Output

Device ID: 101, Sensor Value: 75