在线等c++题,定义一个point类,拥有静态数据成员X,Y;构造函数,输入坐标点

如图

#include <cstdio>
#include <cmath>
const double PI = acos(-1);
class point
{
protected:
double X, Y;
public:
point(double x = 0, double y = 0): X(x), Y(y) {}
void input()
{
scanf("%lf %lf", &X, &Y);
}
void output()
{
printf("%f %f\n", X, Y);
}
};
class circle : public point
{
protected:
double R;
public:
circle(double x = 0, double y = 0, double r = 0): point(x, y), R(r) {}
double circle_area()
{
return PI * R * R;
}
void input()
{
scanf("%lf %lf %lf", &X, &Y, &R);
}
void output()
{
printf("%f\n", circle_area());
}
};
class cylinder : public circle
{
protected:
double h;
public:
cylinder(double x = 0, double y = 0, double r = 0, double h = 0): circle(x, y, r), h(h) {}
double surface_area()
{
return PI * 2 * R + 2 * circle_area();
}
void input()
{
scanf("%lf %lf %lf %lf", &X, &Y, &R, &h);
}
void output()
{
printf("%f\n", surface_area());
}
};
int main()
{
circle a(1, 2, 3);
a.output();
cylinder b(4, 5, 6, 7);
b.output();
return 0;
}

您看这个代码符合要求吗?如果不行还可以改!

追问

c++的不是c

追答

好的,我改成<iostream>。

要算PI,用了<cmath>。

之前的代码算表面积时忘了乘h,现已更正。

#include <iostream>
#include <cmath>
const double PI = acos(-1);
using namespace std;
class point
{
protected:
double X, Y;
public:
point(double x = 0, double y = 0): X(x), Y(y) {}
void input()
{
cin >> X >> Y;
}
void output()
{
cout << X << " " << Y << endl;
}
};
class circle : public point
{
protected:
double R;
public:
circle(double x = 0, double y = 0, double r = 0): point(x, y), R(r) {}
double circle_area()
{
return PI * R * R;
}
void input()
{
cin >> X >> Y >> R;
}
void output()
{
cout << circle_area() << endl;
}
};
class cylinder : public circle
{
protected:
double h;
public:
cylinder(double x = 0, double y = 0, double r = 0, double h = 0): circle(x, y, r), h(h) {}
double surface_area()
{
return PI * 2 * R * h + 2 * circle_area();
}
void input()
{
cin >> X >> Y >> R >> h;
}
void output()
{
cout << surface_area() << endl;
}
};
int main()
{
circle a(1, 2, 3);
a.output();
cylinder b(4, 5, 6, 7);
b.output();
return 0;
}

温馨提示:答案为网友推荐,仅供参考