检查这个代码#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; };
时间: 2024-02-04 18:03:52 浏览: 70
使用include <iostream>时一定要加using namespace std
代码没有结束,缺少最后的括号和分号。另外,在Cylinder类的构造函数中,应该调用父类构造函数来初始化Shape类中的r成员变量。正确的代码如下:
```
#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+2*3.14*r*height;
}
double Cylinder::volume(){
return 3.14*r*r*height;
}
```
阅读全文