解释class Cylinder extends Circle { private int b; private int c; public int getBO‹ return b; public int getCO{ return c; public void setB(int b) { this.b = b; public void setC(int c) { this.c = c; } public Cylinder (int b,int c) { this.b = b; this.c = c;
时间: 2024-04-05 20:35:18 浏览: 18
这段代码定义了一个Cylinder类,继承自Circle类。Cylinder类有两个私有变量b和c,分别表示圆柱的底面半径和高度。类中定义了公有的getBO和getCO方法,分别用来获取底面半径和高度。同样也有setB和setC方法,用来设置底面半径和高度。最后定义了一个构造函数,用来初始化底面半径和高度。在构造函数中使用了this关键字,用来指代当前对象的底面半径和高度。
相关问题
解释Class Cylinder extends Circle { private int b; private int c; public int getBO{ return b; } public int getCO{ return c; public void setB(int b) { this.b = b; ? public void setC(int c) { this.c = c;
这是一个类 Cylinder 继承自类 Circle,其中 Cylinder 类有两个私有成员变量 b 和 c,分别表示圆柱的高和半径。类 Cylinder 还有三个公共方法:getBO、getCO 和 setB。getBO 和 getCO 分别用于获取圆柱的高和半径,setB 方法用于设置圆柱的高度。注意,setB 方法中的代码有一个语法错误,应该是 this.b = b; 而不是 this.b=b;。同理,setC 方法用于设置圆柱的半径。
#include <iostream> using namespace std; class Circle{ private: int r; public: Circle(int a):r(a){} }; class Cylinder:public Circle{ private: int h; public: Cylinder(int a,int b):Circle(a){ h=b; } void gets(){ double S,V; S=2*pi*r(h+r); v=pi*r*r*h; cout<<S<<" "<<V<<endl; } }; int main(){ int a,b; cin>>a>>b; Cylinder x1(a,b); x1.gets(a,b); return 0: }
这段代码中有几个错误:
1. 缺少头文件,需要添加 `#include <cmath>`。
2. 在 Cylinder 类中,变量 V 没有定义,应该将其改为小写字母 v。
3. 在 Cylinder 类中的 gets 函数中,计算表面积和体积的公式写错了。正确的公式应该是 `S=2*pi*r*h+2*pi*r*r;` 和 `v=pi*r*r*h;`。
4. 在 main 函数中,调用 gets 函数时不需要再传入参数 a 和 b。
修改后的代码如下:
```
#include <iostream>
#include <cmath>
using namespace std;
const double pi = 3.1415926;
class Circle {
private:
int r;
public:
Circle(int a): r(a) {}
};
class Cylinder : public Circle {
private:
int h;
public:
Cylinder(int a, int b): Circle(a) {
h = b;
}
void gets() {
double S, v;
S = 2 * pi * r * h + 2 * pi * r * r;
v = pi * r * r * h;
cout << S << " " << v << endl;
}
};
int main() {
int a, b;
cin >> a >> b;
Cylinder x1(a, b);
x1.gets();
return 0;
}
```