题目:设计一个空调类airCondition,其中包括:
数据成员:品牌(要求char* 类型)、颜色、攻率、开关状态、设定温度;
构造函数:对品牌、颜色、攻率、设定温度赋初值;
要求1.写出复制构造函数(参数必须被const 修饰),赋值运算符重载函数
2.要求有一定有默认构造函数的生成(=default的使用)
析构函数:用户自定义(注意动态申请的资源的释放);
成员函数:切换开关状态、升温、降温。
主函数中要求至少创建三个对象,其中之一用复制构造函数初始化,另外一个创建后用赋值运算符赋值;
具体信息如:格力、白色、2匹、25度等等。调用其“切换开关状态”函数打开空调,调用其“降温”函数调整温度为20度,并打印空调状态和目前设定的温度到屏幕。
这是我本来写的代码但是没有实现所给的要求,希望有人能够修改一下,完成要求。因为自己改了好多遍都没有实现,实在改不出来了,直接贴上第一次写的。
- 写回答
- 好问题 0 提建议
- 追加酬金
- 关注问题
- 邀请回答
-
2条回答 默认 最新
- 吉拉尔 2022-04-05 17:34关注
#include <iostream> #include <string> #include <cstring> using namespace std; #define OPEN true #define CLOSE false class Aircondition { private: char *g_brand; string g_colour; double g_power; public: float g_tempreture; bool status; // 开关状态 public: Aircondition() = default; Aircondition(char *brand, string colour, double power, float tempreture) { g_brand = new char[strlen(brand) + 1]; strcpy(g_brand, brand); g_colour = colour; g_power = power; g_tempreture = tempreture; status = false; } // 复制构造 Aircondition(const Aircondition &a) { g_brand = new char[strlen(a.g_brand) + 1]; strcpy(g_brand, a.g_brand); g_colour = a.g_colour; g_power = a.g_power; g_tempreture = a.g_tempreture; status = a.status; } // 赋值运算符重载 Aircondition &operator=(const Aircondition &a) { g_brand = new char[strlen(a.g_brand) + 1]; strcpy(g_brand, a.g_brand); g_colour = a.g_colour; g_power = a.g_power; g_tempreture = a.g_tempreture; status = a.status; } ~Aircondition() { delete[] g_brand; } void show() { cout << "品牌: " << g_brand << endl; cout << "颜色: " << g_colour << endl; cout << "功率: " << g_power << "匹" << endl; cout << "设定温度: " << g_tempreture << "度" << endl; } bool Switch() { status = !status; if (status == true) cout << "空调启动" << endl; else cout << "空调关闭" << endl; } void tempreture_turnup(int t) { g_tempreture = g_tempreture + t; } void tempreture_turndown(int t) { g_tempreture = g_tempreture - t; } }; int main() { Aircondition a("格力", "白色", 2, 25); a.show(); a.Switch(); if (a.status == OPEN) { int aim_tempreture; cout << "目标温度:"; cin >> aim_tempreture; if (aim_tempreture > a.g_tempreture) a.tempreture_turnup(aim_tempreture - a.g_tempreture); else if (aim_tempreture < a.g_tempreture) a.tempreture_turndown(a.g_tempreture - aim_tempreture); } a.show(); Aircondition b(a); //复制构造 b.show(); Aircondition c = b; // 赋值重载 c.show(); }
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 1无用
悬赏问题
- ¥15 2020长安杯与连接网探
- ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
- ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
- ¥16 mybatis的代理对象无法通过@Autowired装填
- ¥15 可见光定位matlab仿真
- ¥15 arduino 四自由度机械臂
- ¥15 wordpress 产品图片 GIF 没法显示
- ¥15 求三国群英传pl国战时间的修改方法
- ¥15 matlab代码代写,需写出详细代码,代价私
- ¥15 ROS系统搭建请教(跨境电商用途)