用C++面向对象写个球体的类,要调用另外一个Point的类,就是组合类。

2024-12-28 20:00:39
推荐回答(1个)
回答1:

#include
#include
#define PI 3.14
using namespace std;
class Point
{
double x,y;
public:
Point(){
}
Point(Point& p)//拷贝构造函数
{
this->x=p.x;
this->y=p.y;
}
Point& operator=(const Point& p)//赋值构造函数
{
this->x=p.x;
this->y=p.y;
}
Point(double x,double y) //构造函数
{
this->x=x;
this->y=y;
}
double distanceOfPoint(const Point& p) //计算两点间距离
{
return sqrt((p.x-x)*(p.x-x)+(p.y-y)*(p.y-y));
}
void setPoint(double x,double y) //改变坐标,不实用
{
this->x=x;
this->y=y;
}

};
class Orb
{

Point p;
double r;
public:
Orb(double r,Point p)
{
this->r=r;
this->p=p;
}
double getVolume() //计算体积
{
return 4/3*PI*r*r;
}
void setCenterPoint(const Point&p)//设置圆心
{
this->p=p;
}
void setR(double r)//设置半径
{
this->r=r;
}

};
int main()
{
double r=4;
Point center(3,3);
Orb o(r,center);
double volume=o.getVolume();
cout<<"半径为:"<
}