The Rule of Three

Hard Design Patterns & Final Challenges PRO
🔒 Login to Unlock

Problem Statement

Create a `Buffer` class managing dynamic memory `int* data`. Implement the Rule of Three: a Destructor, a Copy Constructor, and a Copy Assignment Operator. Have them print ‘Destructor’, ‘Copy Constructor’, and ‘Copy Assignment’ respectively. In `main()`, within a local block `{}`, create `b1(val)`, then `Buffer b2 = b1;`, and `Buffer b3(0); b3 = b1;`.

Input Format

A single integer.

Output Format

Print the operational trace.

Constraints

None

Sample Input

42

Sample Output

Copy Constructor
Copy Assignment
Destructor
Destructor
Destructor

Explanation

The Rule of Three states that if a class requires a user-defined destructor, copy constructor, or copy assignment operator, it almost certainly requires all three to safely manage memory.

Starter Code

#include <iostream>

class Buffer {
    int* data;
public:
    Buffer(int v) : data(new int(v)) {}
    // Implement the Rule of Three here
};

int main() {
    int val;
    if (std::cin >> val) {
        {
            // Instantiate b1, b2, and b3 according to the prompt
        }
    }
    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