Constructor Initialization Lists

Medium Object Lifecycle (Constructors & Destructors)
Solve in Playground →

Problem Statement

Create an `Item` class with a `const int id`. Because it’s `const`, you MUST use a constructor initialization list to assign its value. Print ‘ID: [id]’ inside the constructor. Read an integer and instantiate the object.

Input Format

A single integer.

Output Format

Print the formatted ID.

Constraints

None

Sample Input

205

Sample Output

ID: 205

Explanation

Initialization lists `Class(int a) : var(a) {}` are required when setting values for const attributes or reference types.

Starter Code

#include <iostream>

class Item {
public:
    const int id;
    // Write constructor using an initialization list here
};

int main() {
    int val;
    if (std::cin >> val) {
        // Instantiate Item
    }
    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