题目
设计一个描述二维平面内一点的 CPoint 类,成员包括:x,y。需要实现的功能(成员函数):构造函数、设置坐标、获取坐标、获取极坐标、求两点之间的距离。然后再设计一个描叙三维平面内一点的C3DPoint 类,该类为 CPoint 类的派生类,新添加的成员括:z。需新添加或重新定义的功能包括:构造函数、设置坐标、获取坐标、求两点之间的距离。请在 main() 函数中测试之。
相关阅读
完整代码
#include "bits/stdc++.h"
using namespace std;
class CPoint{
protected:
double x;
double y;
public:
CPoint(){
cin >> x >> y;
}
void setPoint(double x, double y){
this->x = x;
this->y = y;
}
double getX(){
return x;
}
double getY(){
return y;
}
double getP(){
return sqrt(x*x+y*y);
}
double getAngle(){
return atan(y/x);
}
double getDistance(CPoint cPoint){
return sqrt((cPoint.getX()-x)*(cPoint.getX()-x)+(cPoint.getY()-y)*(cPoint.getY()-y));
}
};
class C3DPoint: public CPoint{
protected:
double z;
public:
C3DPoint():CPoint(){
cin >> z;
}
void setC3DPoint(double x, double y, double z){
setPoint(x, y);
this->z = z;
}
double getZ(){
return z;
}
double getDistance(C3DPoint c3DPoint){
return sqrt((c3DPoint.x-x)*(c3DPoint.x-x) + (c3DPoint.y-y)*(c3DPoint.y-y) + (c3DPoint.z-z)*(c3DPoint.z-z));
}
};
int main(){
CPoint cPoint;
cPoint.setPoint(1,2);
cPoint.getX();
cPoint.getY();
cPoint.getP();
cPoint.getAngle();
CPoint cPoint1;
cPoint.getDistance(cPoint1);
C3DPoint c3DPoint;
c3DPoint.setC3DPoint(1,2,3);
c3DPoint.getX();
c3DPoint.getY();
c3DPoint.getZ();
C3DPoint c3DPoint1;
c3DPoint.getDistance(c3DPoint1);
}