#include <iostream>
#include <string>
#include <vector>
// Class to represent a smart device
class SmartDevice {
public:
virtual void status() const = 0;
virtual void toggle() = 0;
virtual ~SmartDevice() {}
};
// Class to represent a smart light
class SmartLight : public SmartDevice {
public:
SmartLight(const std::string& name) : name(name), isOn(false) {}
void status() const override {
std::cout << name << " Light is " << (isOn ? "On" : "Off") << std::endl;
}
void toggle() override {
isOn = !isOn;
}
private:
std::string name;
bool isOn;
};
// Class to represent a smart thermostat
class SmartThermostat : public SmartDevice {
public:
SmartThermostat(double initialTemp) : temperature(initialTemp) {}
void status() const override {
std::cout << "Thermostat Temperature is " << temperature << "°C" << std::endl;
}
void toggle() override {
// Placeholder for toggle functionality, not applicable for thermostat
std::cout << "Thermostat temperature adjustment not supported in this simulation.\n";
}
void setTemperature(double newTemp) {
temperature = newTemp;
}
private:
double temperature;
};
// Class to represent a smart security system
class SmartSecurity : public SmartDevice {
public:
SmartSecurity() : isArmed(false) {}
void status() const override {
std::cout << "Security System is " << (isArmed ? "Armed" : "Disarmed") << std::endl;
}
void toggle() override {
isArmed = !isArmed;
}
private:
bool isArmed;
};
int main() {
// Create smart devices
SmartLight livingRoomLight("Living Room");
SmartThermostat thermostat(22.0);
SmartSecurity securitySystem;
// List of devices
std::vector<SmartDevice*> devices = { &livingRoomLight, &thermostat, &securitySystem };
int choice;
do {
std::cout << "\nSmart Home Automation System\n";
std::cout << "1. Toggle Light\n";
std::cout << "2. Set Thermostat Temperature\n";
std::cout << "3. Toggle Security System\n";
std::cout << "4. Show Status\n";
std::cout << "5. Exit\n";
std::cout << "Enter your choice: ";
std::cin >> choice;
switch (choice) {
case 1:
livingRoomLight.toggle();
break;
case 2: {
double newTemp;
std::cout << "Enter new temperature: ";
std::cin >> newTemp;
thermostat.setTemperature(newTemp);
break;
}
case 3:
securitySystem.toggle();
break;
case 4:
for (const auto& device : devices) {
device->status();
}
break;
case 5:
std::cout << "Exiting system.\n";
break;
default:
std::cout << "Invalid choice. Please try again.\n";
}
} while (choice != 5);
return 0;
}