Diamond Inheritance Duplication Fix

#include <iostream>
using namespace std;

// Shared base
class DeviceCore {
public:
    DeviceCore(int id) : deviceId(id) {
        cout << "Device core initialized" << endl;
    }

    int getId() const {
        return deviceId;
    }

private:
    int deviceId;
};

// Communication layer
class CommLayer :public virtual DeviceCore {
public:
    CommLayer(int id) : DeviceCore(id) {}
};

// Power layer
class PowerLayer :public virtual DeviceCore {
public:
    PowerLayer(int id) : DeviceCore(id) {}
};

// Diamond inheritance
class SensorDriver : public CommLayer, public PowerLayer {
public:
    SensorDriver(int id)
        : DeviceCore(id), CommLayer(id), PowerLayer(id) {}

    void printId() const {
        cout << "Device ID " << getId() << endl;
    }
};

int main() {
    int id;
    cin >> id;

    SensorDriver sensor(id);
    sensor.printId();

    return 0;
}

Solving Approach

 

 

 

 

Upvote
Downvote
Loading...

Input

0

Expected Output

Device core initialized Device ID 0