Pregunta de entrevista de HCLTech

Design a C++ class to make LED blink.

Respuesta de la entrevista

Anónimo

8 ago 2025

answer will be - #include #include // for sleep_for #include // for seconds using namespace std; class LED { private: string color; bool state; // true = ON, false = OFF public: LED(string c) { color = c; state = false; // initially OFF } void on() { state = true; cout << color << " LED is ON" << endl; } void off() { state = false; cout << color << " LED is OFF" << endl; } void blink(int times, int delaySec) { for (int i = 0; i < times; i++) { on(); this_thread::sleep_for(chrono::seconds(delaySec)); off(); this_thread::sleep_for(chrono::seconds(delaySec)); } } }; int main() { LED led1("Red"); cout << "Blinking LED..." << endl; led1.blink(5, 1); // blink 5 times with 1-second delay return 0; }