str_painkiller 2022-04-03 17:23 采纳率: 100%
浏览 55
已结题

c++类与对象练习题,编了很久,调试却总有问题,希望大家帮我指出错误

题目:设计一个空调类airCondition,其中包括:
数据成员:品牌(要求char* 类型)、颜色、攻率、开关状态、设定温度;
构造函数:对品牌、颜色、攻率、设定温度赋初值;
要求1.写出复制构造函数(参数必须被const 修饰),赋值运算符重载函数
2.要求有一定有默认构造函数的生成(=default的使用)
析构函数:用户自定义(注意动态申请的资源的释放);
成员函数:切换开关状态、升温、降温。
主函数中要求至少创建三个对象,其中之一用复制构造函数初始化,另外一个创建后用赋值运算符赋值;
具体信息如:格力、白色、2匹、25度等等。调用其“切换开关状态”函数打开空调,调用其“降温”函数调整温度为20度,并打印空调状态和目前设定的温度到屏幕。

#include<iostream>
#include<string.h>
#include<conio.h>
using namespace std;
class airCondition
{
private:
    char *brand;char *power;char *color;
public:
    char*button;float temp;
    airCondition(char*br,char*po,char*co,char*button,float temp)
    {
        if(br)
        {
            brand=new char[strlen(br)+1];
            strcpy(brand,br);
        }
        if(po)
        {
            power=new char[strlen(po)+1];
            strcpy(power,po);
        }
        if(co)
        {
            color=new char[strlen(co)+1];
            strcpy(color,co);
        };
    }//定义了空调类的构造函数,不变的属性如品牌、颜色等放私有,变的属性如开关、温度放公有。
        ~airCondition();
        airCondition(const airCondition &a);
//析构函数和复制构造函数声明
    void change()
    {
        if(button=="关")
            button="开";
        if(button=="开")
            button="关";
    }//开关函数,按回车实现开或关
    void tempup()
    {
        cout<<"按“↑”增加温度,“ESC”退出升温"<<endl;
        int c;
        while(1)
        {
            c=getch();
            if(c==38)
                temp+=1;
            if(c==27)
                break;
        }
    }//升温函数
    void tempdown()
    {
        cout<<"按“↓”降低温度,“ESC”退出降温"<<endl;
        int d;
        while(1)
        {
            d=getch();
            if(d==40)
                temp+=1;
            if(d==27)
                break;
        }
    }//降温函数
    void print()
    {
        cout<<"品牌:"<<brand<<'\t'<<"功率"<<power<<endl<<"颜色"<<color<<endl<<"状态"<<button<<endl<<"温度"<<temp<<endl;
    }//输出函数
};
airCondition::airCondition(const airCondition &a)
{
    if(a.brand)
        {
            brand=new char[strlen(brand)+1];
            strcpy(brand,a.brand);
        }
        if(a.power)
        {
            power=new char[strlen(a.power)+1];
            strcpy(power,a.power);
        }
        if(a.color)
        {
            color=new char[strlen(a.color)+1];
            strcpy(color,a.color);
        };
}//复制构造函数
airCondition::~airCondition()
{
    if(brand)
        delete []brand;
    if(power)
        delete []power;
    if(color)
        delete []color;
    if(button)
        delete []button;
}
//析构函数
void main()
{
    int m;
    airCondition a1("格力","3匹","黄","关",25);
    airCondition a2("美的","2匹","绿","关",25);
    airCondition a3("海尔","4匹","白","关",25);
    airCondition a4(a1);
    cout<<"请输入空调编号来选择空调"<<endl;
    cin>>m;
    if(m==1)
    {
        a1.print();
        a1.change();
        a1.tempup();
        a1.tempdown();
        a1.print();
    }
    if(m==2)
    {
        a2.print();
        a2.change();
        a2.tempup();
        a2.tempdown();
        a2.print();
    }
    if(m==3)
    {
        a3.print();
        a3.change();
        a3.tempup();
        a3.tempdown();
        a3.print();
    }
    if(m==4)
    {
        a4.print();
        a4.change();
        a4.tempup();
        a4.tempdown();
        a4.print();
    }
    getchar();
    getchar();
}





img

我的代码如上,应该和题目描述的差不多吧,空调温度应该是一度一度调的吧,但是运行总是有问题
希望大家能帮我,大一小白,我真的编了好久

我想要达到的结果
  • 写回答

1条回答 默认 最新

  • [PE]经典八炮 2022-04-03 17:31
    关注

    button没有分配内存,还有字符串比较不能直接用==

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 4月11日
  • 已采纳回答 4月3日
  • 创建了问题 4月3日

悬赏问题

  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器