Virtual Base Classes (virtual public)

Medium Multiple & Virtual Inheritance
Solve in Playground →

Problem Statement

Fix the diamond problem! Modify `Printer` and `Scanner` to inherit `virtual public` from `Device`. `Copier` inherits from both. Now `Copier` has only ONE shared instance of `power`. Read an integer, assign it directly to `Copier`’s `power` (no scope resolution needed), and print it.

Input Format

A single integer.

Output Format

Print 'Shared Power: [val]'.

Constraints

None

Sample Input

220

Sample Output

Shared Power: 220

Explanation

Virtual inheritance ensures only one copy of the base class is shared across the hierarchy, preventing ambiguity.

Starter Code

#include <iostream>

class Device {
public:
    int power;
};

// Use virtual public inheritance here
class Printer {};
class Scanner {};
class Copier : public Printer, public Scanner {};

int main() {
    int p;
    // Read p, assign directly, 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