Factory Pattern with Smart Pointers

Medium Smart Pointers
Solve in Playground →

Problem Statement

Write a C++ program implementing a factory method returning `std::unique_ptr` to instantiate graphic shapes dynamically.

Input Format

A choice character ('C' for Circle, 'R' for Rectangle).

Output Format

Print shape name created by factory.

Constraints

Choice is C or R

Sample Input

C

Sample Output

Created Circle

Explanation

Factory functions combined with smart pointers enforce safe object creation and ownership transfer.

Starter Code

#include <iostream>
#include <memory>

class Shape {
public:
    virtual void draw() = 0;
    virtual ~Shape() {}
};

class Circle : public Shape {
public:
    void draw() override { std::cout << "Created Circlen"; }
};

class Rectangle : public Shape {
public:
    void draw() override { std::cout << "Created Rectanglen"; }
};

std::unique_ptr<Shape> createShape(char type) {
    // Implement factory function logic here
}

int main() {
    char type;
    if (std::cin >> type) {
        auto shape = createShape(type);
        shape->draw();
    }
    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