Non-Copyable UART Handle

#include <iostream>
#include <cstdint>
using namespace std;

class UARTHardware {
private:
    int baud;

public:
    // 1. Constructor to initialize the UART with a specific baud rate
    UARTHardware(int baudRate) : baud(baudRate) {}

    // 2. Delete the copy constructor
    // This prevents: UARTHardware copy = original;
    UARTHardware(const UARTHardware&) = delete;

    // 3. Delete the copy assignment operator
    // This prevents: copy = original;
    UARTHardware& operator=(const UARTHardware&) = delete;

    // 4. Send function to simulate hardware transmission
    void sendByte(uint8_t value) const {
        cout << "TX: " << (int)value << endl;
    }
};

int main() {
    int baudRate;
    if (!(cin >> baudRate)) return 0;

    int temp;
    if (!(cin >> temp)) return 0;
    uint8_t byteToSend = (uint8_t)temp;

    // Create the unique hardware handle
    UARTHardware uart(baudRate);

    // Demonstration of non-copyable constraint:
    // If you uncomment the line below, the compiler will provide an error
    // stating that 'UARTHardware' cannot be copied.
    // UARTHardware copy = uart; 

    uart.sendByte(byteToSend);

    return 0;
}

Solving Approach

 

 

 

 

 

Upvote
Downvote
Loading...

Input

9600 65

Expected Output

TX: 65