1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
#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; } |
Explanation
- SmartDevice Class:
- Attributes:
- Abstract base class for all smart devices.
- Methods:
status()
: Pure virtual function to display the status of the device.toggle()
: Pure virtual function to toggle the device’s state.
- Attributes:
- SmartLight Class:
- Attributes:
name
: Name of the light.isOn
: State of the light (on/off).
- Methods:
status()
: Prints whether the light is on or off.toggle()
: Toggles the light between on and off.
- Attributes:
- SmartThermostat Class:
- Attributes:
temperature
: Current temperature setting.
- Methods:
status()
: Prints the current temperature.toggle()
: Placeholder for toggle functionality (not used here).setTemperature(double newTemp)
: Sets a new temperature value.
- Attributes:
- SmartSecurity Class:
- Attributes:
isArmed
: State of the security system (armed/disarmed).
- Methods:
status()
: Prints whether the security system is armed or disarmed.toggle()
: Toggles the security system between armed and disarmed.
- Attributes:
- Main Function:
- Device Creation: Creates instances of
SmartLight
,SmartThermostat
, andSmartSecurity
. - Device Management: Uses a vector of
SmartDevice*
to manage and interact with devices. - User Interface:
- Provides a menu for the user to toggle devices, set the thermostat temperature, show the status of devices, or exit the program.
- Handles user input to perform actions on the devices.
- Device Creation: Creates instances of
Usage
- Smart Home Control: Simulates a basic smart home system where users can interact with and manage smart devices.
- Device Status and Control: Allows toggling devices on and off, setting temperatures, and displaying the current status of devices.