Composition: "Has-a" Relationship

Easy Composition & Aggregation
Solve in Playground →

Problem Statement

Implement a `Car` class that strictly contains an `Engine` class. The `Engine` class takes a `power` parameter. The `Car` class should instantiate the `Engine` inside its own constructor initialization list (strong lifecycle). Read an integer for power, instantiate the Car, and print the engine power.

Input Format

A single integer representing engine power.

Output Format

Print 'Engine: [power]HP' and on the next line 'Car created'.

Constraints

power > 0

Sample Input

400

Sample Output

Engine: 400HP
Car created

Explanation

Composition represents a strict 'has-a' relationship where the child object (Engine) cannot exist without the parent object (Car).

Starter Code

#include <iostream>

class Engine {
public:
    // Write your code here
};

class Car {
public:
    // Write your code here
};

int main() {
    // Read power, instantiate Car, and print details
    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