Rule of Five (Destructor, Copy/Move Ctor/Assign)

Hard Move Semantics
Solve in Playground →

Problem Statement

Write a C++ program implementing the Rule of Five for a custom resource-managing class.

Input Format

A single word string S.

Output Format

Print string from move-assigned instance.

Constraints

1 <= Length <= 50

Sample Input

RuleOfFive

Sample Output

RuleOfFive

Explanation

The Rule of Five dictates that if a class requires any of destructor, copy constructor, copy assignment, move constructor, or move assignment, it almost certainly requires all five.

Starter Code

#include <iostream>
#include <cstring>
#include <string>

class ResourcePool {
private:
    char *data;

public:
    ResourcePool(const char *str = "") {
        data = new char[std::strlen(str) + 1];
        std::strcpy(data, str);
    }

    // Implement the Rule of Five constructors, copy/move assignments, and destructor here

    const char* c_str() const { return data ? data : ""; }
};

int main() {
    std::string s;
    if (std::cin >> s) {
        ResourcePool rp1(s.c_str());
        ResourcePool rp2 = std::move(rp1);
        std::cout << rp2.c_str() << "n";
    }
    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