#include <iostream>
#include <cstdint>
using namespace std;
class SensorFrame {
private:
uint8_t* data;
public:
// 1. Constructor: Allocates unique memory for the frame
SensorFrame(uint8_t arr[8]) {
data = new uint8_t[8];
for (int i = 0; i < 8; i++) {
data[i] = arr[i];
}
}
// 2. Copy Constructor: Performs a DEEP COPY
// This ensures that 'B' gets its own unique memory buffer.
SensorFrame(const SensorFrame& other) {
data = new uint8_t[8];
for (int i = 0; i < 8; i++) {
data[i] = other.data[i];
}
}
// 3. Destructor: Clean up memory to avoid leaks
~SensorFrame() {
delete[] data;
}
// 4. Copy Assignment Operator (Optional for this specific main, but good practice)
SensorFrame& operator=(const SensorFrame& other) {
if (this != &other) { // Protect against self-assignment
for (int i = 0; i < 8; i++) {
data[i] = other.data[i];
}
}
return *this;
}
void setIndex3() {
data[3] = 99;
}
void print() const {
for (int i = 0; i < 8; i++) {
cout << (int)data[i];
if (i != 7) cout << " ";
}
cout << endl;
}
};
int main() {
uint8_t arr[8];
for (int i = 0; i < 8; i++) {
int temp;
if (!(cin >> temp)) break;
arr[i] = (uint8_t)temp;
}
SensorFrame A(arr);
SensorFrame B = A; // Now calls the Copy Constructor for a deep copy
B.setIndex3(); // Only affects B's internal array
A.print(); // Outputs original values
B.print(); // Outputs modified values
return 0;
}
Input
1 2 3 4 5 6 7 8
Expected Output
1 2 3 4 5 6 7 8 1 2 3 99 5 6 7 8