C++ 空调类实现:设置温度、开关状态和打印信息
#include
class airCondition { private: string brand; string color; string power; bool switchStatus; int targetTemperature;
public: airCondition(string brand, string color, string power, int targetTemperature) { this->brand = brand; this->color = color; this->power = power; this->targetTemperature = targetTemperature; switchStatus = false; }
void toggleSwitch() {
switchStatus = !switchStatus;
}
void increaseTemperature() {
targetTemperature++;
}
void decreaseTemperature() {
targetTemperature--;
}
void printStatus() {
cout << "Brand: " << brand << endl;
cout << "Color: " << color << endl;
cout << "Power: " << power << endl;
cout << "Switch status: " << (switchStatus ? "On" : "Off") << endl;
cout << "Target temperature: " << targetTemperature << " degrees" << endl;
}
};
int main() { airCondition ac("格力", "白色", "2 匹", 25); ac.toggleSwitch(); ac.decreaseTemperature(); ac.printStatus(); return 0; }
// 输出: // Brand: 格力 // Color: 白色 // Power: 2 匹 // Switch status: On // Target temperature: 20 degrees
原文地址: https://www.cveoy.top/t/topic/p9Z1 著作权归作者所有。请勿转载和采集!