Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/146.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 为什么可以';我用体积表示的面积值吗?我得到的音量:-1405824736_C++ - Fatal编程技术网

C++ 为什么可以';我用体积表示的面积值吗?我得到的音量:-1405824736

C++ 为什么可以';我用体积表示的面积值吗?我得到的音量:-1405824736,c++,C++,创建一个名为Rectangle且具有长度、宽度和面积的类。使用适当的成员函数从用户处读取长度和宽度,并计算矩形的面积。从类矩形中创建一个名为Box的派生类。使用适当的成员函数读取高度并计算体积 #include <iostream> using namespace std; class Rectangle { protected: int length; int breadth; int area; public:

创建一个名为Rectangle且具有长度、宽度和面积的类。使用适当的成员函数从用户处读取长度和宽度,并计算矩形的面积。从类矩形中创建一个名为Box的派生类。使用适当的成员函数读取高度并计算体积

#include <iostream>
using namespace std;

class Rectangle
{
    protected:
        int length;
        int breadth;
        int area;
    public:
        int input();
        int calc();
};

int Rectangle::input()
{
    cout<<"Enter the length and breadth:"<<endl;
    cin>>length>>breadth;

    return 0;
}

int Rectangle::calc()
{
    area=length*breadth;
    cout<<"Area: "<<area<<endl;

    return 0;
}

class Box:public Rectangle
{
    int height;

    public:
        int input();
        int vol();
};

int Box::input()
{
    cout<<"Enter the height:";
    cin>>height;

    return 0;
}

int Box::vol()
{
    cout<<"Volume: "<<area*height<<endl;

    return 0;
}

int main()
{
    Rectangle r;
    Box b;

    r.input();
    r.calc();
    cout<<endl;
    b.input();
    b.vol();

    return 0;
}
#包括
使用名称空间std;
类矩形
{
受保护的:
整数长度;
整数宽度;
内部区域;
公众:
int输入();
int calc();
};
int矩形::输入()
{
coutbreadth;
返回0;
}
int矩形::calc()
{
面积=长度*宽度;

cout在这种情况下,您的问题似乎是您没有为变量
b
设置长度和宽度。
框::input
仅初始化
高度
如果您想使用与
r
相同的输入,则此代码可能会更简单

//modify Box::input to:
int Box::input()
{
    Rectangle::input(); // will initialise length, breadth
    cout<<"Enter the height:";
    cin>>height;

    return 0;
}

...

//Then in main
int main()
{
    Box b;

    b.input();
    b.calc();
    cout<<endl;
    b.vol();

    return 0;
}
//修改框::输入到:
int-Box::input()
{
矩形::输入();//将初始化长度、宽度
库尔特;
返回0;
}
...
//那么大体上
int main()
{
方框b;
b、 输入();
b、 计算();

cout这是因为您在调用
Rectangle::calc()
时正在计算面积,而在调用
Box::vol()时正在计算面积
,您的变量
区域
开始初始化。您得到的可能是一些垃圾值。在
vol
中,重新计算区域或调用
this->calc
。而且您缺少长度和宽度

将成员初始化为默认值是一种很好的做法,默认值是默认构造函数。一个好处是您不会得到垃圾值。对于如下矩形,您应该有一个默认构造函数:

Rectangle::Rectangle()
: length(0), breadth(0), area(0)
{
}
在这个框中,应该有这样的构造函数

Box::Box()
: Rectangle::Rectangle ()
{
}

另外,我认为在类成员函数中输入不是一个好主意。您应该有
Rectangle::Rectangle(int-length,int-width)
这样的设置器,比如
Rectangle::setLength(int-l)
。像这样输入并设置值。

您对程序输出的惊讶向我表明,您认为
r
对象和
b
矩形
子对象在某种程度上是相同的。它们不是

Box::Box()
: Rectangle::Rectangle ()
{
}
您的代码存在以下问题

  • 您没有正确初始化类的成员变量
  • Box::input
    不准确。它需要确保
    矩形
    子项目的成员也已输入
  • 您的程序具有未定义的行为,因为它使用未初始化的成员变量
  • 从规划指南的角度来看,最好:

  • 使用
    operator>>(std::istream&,Box&)
    operator>>(std::istream&,Rectangle&)
    而不是
    input()
    成员函数。这将允许您从任何流读取数据,而不仅仅是
    std::cin
  • 删除成员变量
    area
    。将其替换为返回计算区域的函数
    area()
  • 更新
    vol()
    函数以返回对象的卷,而不是将其打印到
    std::cin
  • 这是我建议的程序版本,应该可以运行

    #include <iostream>
    using namespace std;
    
    class Rectangle
    {
        protected:
            int length = 0;
            int breadth = 0;
        public:
            int area() const;
    
            friend std::istream& operator>>(std::istream& in, Rectangle& r);
    };
    
    int Rectangle::area() const
    {
       return length*breadth;
    }
    
    std::istream& operator>>(std::istream& in, Rectangle& r)
    {
        return in >> r.length >> r.breadth;
    }
    
    class Box: public Rectangle
    {
        int height = 0;
    
        public:
            int volume() const;
    
        friend std::istream& operator>>(std::istream& in, Box& b);
    };
    
    std::istream& operator>>(std::istream& in, Box& b)
    {
       // Read the Rectangle sub-object of the Box and then the
       // height of the Box..
       Rectangle& r = b;
       return in >> r >> b.height;
    }
    
    int Box::volume() const
    {
        return this->area()*height;
    }
    
    int main()
    {
        Box b;
    
        cout<<"Enter the length, breadth, and height: ";
        cin >> b;
    
        cout << "Area of the Rectangle sub-object: " << b.area() << std::endl;
        cout << "Volume of the Box: " << b.volume() << std::endl;
    
        return 0;
    }
    
    #包括
    使用名称空间std;
    类矩形
    {
    受保护的:
    整数长度=0;
    整数宽度=0;
    公众:
    int区域()常数;
    friend std::istream&operator>>(std::istream&in,矩形&r);
    };
    int Rectangle::area()常量
    {
    返回长度*宽度;
    }
    std::istream&operator>>(std::istream&in、矩形和r)
    {
    返回>>r.length>>r.width;
    }
    类框:公共矩形
    {
    整数高度=0;
    公众:
    int volume()常量;
    friend std::istream&operator>>(std::istream&in,Box&b);
    };
    标准::istream&operator>>(标准::istream&in,Box&b)
    {
    //读取长方体的矩形子对象,然后读取
    //箱子的高度。。
    矩形&r=b;
    返回>>r>>b.高度;
    }
    int Box::volume()常量
    {
    返回此->区域()*高度;
    }
    int main()
    {
    方框b;
    cout b;
    
    可能是溢出。输入较小的数字,如20和50。即使在2或3上也不起作用。在派生类中传递area的值时,传递的area的值为-919949024。
    矩形r;
    -您不需要它,框b;
    已经包含一个
    矩形
    子对象。
    r.input();r.calc();
    -将其替换为
    b.Rectangle::input();b.calc();
    。不过最好重新考虑类的设计,这样您就不必在每次使用它们时都记住所有这些舞蹈。读取未初始化的变量会使您的程序有更大的灵活性。