设计一个空调类 airCondition其中包括:1数据成员:品牌、颜色、功率、开关状态、设定温度;2构造函数:对品牌、颜色、功率、设定温度赋初值;3成员函数:切换开关状态、升温、降温。要求在 main 函数中创建一个 airCondition 的对象具体信息为:格力、白色、2 匹、25 度。调用其切换开关状态函数打开空调调用其降温函数调整温度为 20 度并打印空调状态和目前设定的温度到屏幕 用c
#include <iostream>
using namespace std;
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
``
原文地址: http://www.cveoy.top/t/topic/ispp 著作权归作者所有。请勿转载和采集!