#include <iostream> #include <string> class Buffer { private: int id; public: Buffer(int i) : id(i) {} // TODO: Implement the assignment operator Buffer& operator=(const Buffer& other) { // TODO: Check for self-assignment using 'this' pointer // Compare the address of the current object (this) with the address of 'other' if (this == &other) { std::cout << "Self-Assignment Detected" << std::endl; return *this; } // TODO: Perform assignment if not self this->id = other.id; std::cout << "Assignment Complete" << std::endl; return *this; } void log() { std::cout << "Buffer ID: " << id << std::endl; } }; int main() { int N; if (!(std::cin >> N)) return 0; for (int i = 0; i < N; ++i) { std::string cmd; std::cin >> cmd; if (cmd == "ASSIGN") { int i1, i2; std::cin >> i1 >> i2; Buffer b1(i1); Buffer b2(i2); b1 = b2; b1.log(); } else if (cmd == "SELF") { int i1; std::cin >> i1; Buffer b1(i1); b1 = b1; // Self-assignment b1.log(); } } return 0; }
Test Cases
Test Results
Input
2 ASSIGN 10 20 SELF 99
Expected Output
Assignment Complete Buffer ID: 20 Self-Assignment Detected Buffer ID: 99