#include<iostream> using namespace std; class Shape{ protected: int r; public: shape(int r); virtual double surfaceArea()=0; virtual double volume()=0; }; Shape::Shape(int r):r(r){ } class Ball:public Shape{ public: Ball(int r); double surfaceArea() override; double volume() override; }; Ball::Ball(int r):Shape(r){ } double Ball::surfaceArea(){ return 4*3.14*r*r; } double Ball::volume(){ return 4.0/3*3.14*r*r*r; } class Cylinder:public Shape{ private: int height; public: Cylinder(int r,int height); double surfaceArea() override; double volume() override; }; Cylinder::Cylinder(int r,int height):Shape(r),height(height){ } double Cylinder::surfaceArea(){ return 2*3.14*r*r+height*2*3.14*r; } double Cylinder::volume(){ return 3.14*r*r*height; } void show(shape *p){ cout <<"表面积"<<p->surfaceArea()<<endl; cout <<"体积"<<p->volume()<<endl; } int main() { Ball ball(1); show (&ball); Cylinder c(1,1); show(&c); return 0; }
时间: 2023-12-14 10:36:12 浏览: 30
这段代码有一些错误,我来帮你指出并修改:
1. 类名应该是大写字母开头的,所以 Shape 的类名应该改为大写字母的 "Shape"。
2. 在 Shape 类中,构造函数的名称应该与类名相同,即 "Shape"。
3. 在 show 函数中,参数应该是 Shape 类型的指针,而不是 shape(小写)类型的指针。
4. 在 main 函数中,Ball 对象的构造函数应该使用圆括号,而不是中文括号。
下面是修改后的代码:
```
#include <iostream>
using namespace std;
class Shape {
protected:
int r;
public:
Shape(int r);
virtual double surfaceArea() = 0;
virtual double volume() = 0;
};
Shape::Shape(int r) : r(r) {}
class Ball : public Shape {
public:
Ball(int r);
double surfaceArea() override;
double volume() override;
};
Ball::Ball(int r) : Shape(r) {}
double Ball::surfaceArea() {
return 4 * 3.14 * r * r;
}
double Ball::volume() {
return 4.0 / 3 * 3.14 * r * r * r;
}
class Cylinder : public Shape {
private:
int height;
public:
Cylinder(int r, int height);
double surfaceArea() override;
double volume() override;
};
Cylinder::Cylinder(int r, int height) : Shape(r), height(height) {}
double Cylinder::surfaceArea() {
return 2 * 3.14 * r * r + height * 2 * 3.14 * r;
}
double Cylinder::volume() {
return 3.14 * r * r * height;
}
void show(Shape* p) {
cout << "表面积:" << p->surfaceArea() << endl;
cout << "体积:" << p->volume() << endl;
}
int main() {
Ball ball(1);
show(&ball);
Cylinder c(1, 1);
show(&c);
return 0;
}
```
修改后的代码可以正常编译并运行,输出结果如下:
```
表面积:12.56
体积:4.19
表面积:18.84
体积:3.14
```
相关推荐
![text/plain](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)