检查这个代码#include<iostream> using namespace std; class Shape { protected: int r; public: Shape(int r); virtual double surfaceArea()=0; virtual double volume()=0; }; Shpe::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::surfaceArea(){ 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; } int main(int argc,char*argv[]){ return 0; }
时间: 2024-02-15 13:05:03 浏览: 115
使用include <iostream>时一定要加using namespace std
这段代码存在一个拼写错误,将 Shape 类的构造函数定义写成了 Shpe::Shape(int r),应该将 Shpe 改为 Shape,即 Shape::Shape(int r)。
此外,Ball 类中的 surfaceArea 函数定义有误,应该将第二个 surfaceArea 函数改为 volume 函数。
修改后的代码如下:
```c++
#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;
}
int main(int argc,char*argv[]){
return 0;
}
```
阅读全文