The Diamond Problem

Hard Multiple & Virtual Inheritance
Solve in Playground →

Problem Statement

Create a `Device` class with a public int `power`. `Printer` and `Scanner` inherit from `Device`. `Copier` inherits from both `Printer` and `Scanner`. In `main()`, instantiate `Copier`. To access `power` without using `virtual`, you must explicitly route through one of the bases. Read an integer, assign it to `Copier`’s `Printer` branch of `power`, and print it.

Input Format

A single integer.

Output Format

Print 'Power: [val]'.

Constraints

None

Sample Input

150

Sample Output

Power: 150

Explanation

Without virtual inheritance, two distinct copies of Device exist inside Copier. You must resolve ambiguity by explicitly stating which path to use.

Starter Code

#include <iostream>

class Device {
public:
    int power;
};

class Printer : public Device {};
class Scanner : public Device {};
class Copier : public Printer, public Scanner {};

int main() {
    int p;
    // Read p, assign to Copier's Printer branch, and print
    return 0;
}

Limits

  • Time Limit: 1s
  • Memory Limit: 256MB

Embedded C Programming

Updated: March 15, 2026
Intermediate

Embedded systems rely on efficient low-level programming to interact directly with hardware. In this course, you will learn how to write practical Embedded C programs used in real microcontroller-based systems. Rather than focusing only on theory, this course follows a practice-driven approach. Each lesson includes hands-on coding exercises that simulate real firmware development tasks used