-1
如何更正此代码,它的工作不正常。 长度和宽度没有得到正确的值。 我有问题,我宣布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();
}
有人可以帮我吗? 因为我认为我已经编写了正确的代码,但它为卷提供了垃圾值,所以我总是感到困惑。
您应该做B1.setter,因为您从矩形继承,您正在另一个对象上设置数据。 – Lefsler