代码正在编译,但它没有给出正确的音量

时间:2014-05-01 15:00:37

标签: c++

如何更正此代码,它无法正常工作。 长度和宽度没有得到正确的值。 我有问题,我声明了Rectangle :: get()。

#include<iostream>
using namespace std;

class Rectangle{
  protected:
    double length;
    double width;
  public:
    void setter(double len, double wid)
    {
      length = len;
      width = wid;
    }
    double get()
    {
      return length*width;
    }

};
class Block: public Rectangle{
  private:
    double heigth;
  public:
  void setter_h(double hei)
  {
    heigth = hei;
  }
  double get_1()
  {
    return(heigth * Rectangle::get());//this is the problem
  }
};


int main()
{
  double len, hei, wid;
  cout<<"Enter the length: ";
  cin>>len;
  cout<<"Enter the Width: ";
  cin>>wid;
  cout<<"Enter the H: ";
  cin>>hei;

  Rectangle R1;
  R1.setter(len,wid);
  cout<<"Area: " << R1.get();

  Block B1;
  B1.setter_h(hei);
  cout<<"Volume: "<< B1.get_1();
}

有人可以帮帮我吗??? 我完全糊涂了,因为我认为我已经编写了正确的代码,但它给了垃圾价值。

1 个答案:

答案 0 :(得分:2)

因为您没有为B1设置宽度和长度。你也应该打电话给

B1.setter(len, wid);

在致电B1.get_1()之前。