c++程序题,编写一个c++程序,定义一个有两个点的类,由点类派生出圆,在由圆派生出球,求圆的面积球的体积

期末题会考,求高手帮忙,谢谢
2024-12-03 18:24:40
推荐回答(1个)
回答1:

徒手也一下 试一试
class Point{
};
class Circle:Point{
protected:
float r;
public:
Circle();
Circle(float r);
float getArea();
};
Circle::Circle(float r){
this -> r = r;
}
Circle::Circle(){
this -> r = 0;
}
floatCircle::getArea(){
returnPI*r*r;
}
class Ball :Circle{

public:
Ball(float r);
float getArea();
};
floatBall::getArea(){
return (PI/3)*r*r*r;
}
Ball::Ball(float r){
this -> r = r;
}
int main(){
Circle c = Circle(3);
cout << c.getArea() << endl;
Ball b = Ball(2);
cout << b.getArea();
return0;
}