蓝星461 2022-09-21 16:59 采纳率: 80%
浏览 32
已结题

c++矩形类问题,为什么这个输出为乱码

#include
using namespace std;
class Rectangle{
public:
Rectangle();
Rectangle(int _length,int _width);
void ShowArea();
void setRectangle(int,int);
void getArea(int);
~Rectangle(){cout<<"GoodBye!"<<endl;
}
private:
int length,width,area;

};
Rectangle::Rectangle() {setRectangle(0,0);}
Rectangle::Rectangle(int _length,int _width){setRectangle(_length,_width);
}
void Rectangle::setRectangle(int _length,int _width){length=_length;width=_width;
}
void Rectangle::getArea(int area){area=length*width;
}
void Rectangle::ShowArea(){cout<<"矩形的面积为:"<<area<<endl; }
int main(){int a=1,b=2;
Rectangle myRectangle;
cout<<"请输入矩形的长和宽;"<<endl;
myRectangle.setRectangle(a,b);
myRectangle.ShowArea();
return 0;
}

img

  • 写回答

1条回答 默认 最新

  • 梦里逆天 2022-09-21 17:25
    关注

    你的getArea获取了面积不返回的吗?showArea()里面都没有参数,它要显示的area是从石头里蹦出来的吗?

    #include <iostream>
    using namespace std;
    class Rectangle{
    public:
    Rectangle();
    Rectangle(int _length,int _width);
    void ShowArea(int);
    void setRectangle(int,int);
    int getArea();
    ~Rectangle(){cout<<"GoodBye!"<<endl;
    }
    private:
    int length,width,area;
    
    };
    Rectangle::Rectangle() {setRectangle(0,0);}
    Rectangle::Rectangle(int _length,int _width){setRectangle(_length,_width);
    }
    void Rectangle::setRectangle(int _length,int _width){length=_length;width=_width;
    }
    int Rectangle::getArea(){
        int area=length*width;
        cout<<area<<endl;
        return area;
    }
    void Rectangle::ShowArea(int area){cout<<"矩形的面积为:"<<area<<endl; }
    
    int main()
    {
    int a=1,b=2;
    Rectangle myRectangle;
    cout<<"请输入矩形的长和宽;"<<endl;
    cin>>a >> b;
    myRectangle.setRectangle(a,b);
    int area = myRectangle.getArea();
    myRectangle.ShowArea(area);
    return 0;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 9月30日
  • 已采纳回答 9月22日
  • 修改了问题 9月21日
  • 创建了问题 9月21日

悬赏问题

  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 shape_predictor_68_face_landmarks.dat
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料