检查代码错误#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:04:58 浏览: 57
#include_iostream.h_
4星 · 用户满意度95%
在 Shape 类的构造函数的定义中,类名拼写错误,应该是 Shape 而不是 Shpe。
在 Ball 类中,surfaceArea 函数定义了两次,应该将第二个定义修改为 volume 函数。
下面是修改后的程序:
```
#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;
}
```
阅读全文