
用C++面向对象写个球体的类,要调用另外一个Point的类,就是组合类。
- 你的回答被采纳后将获得:
- 系统奖励15(财富值+成长值)+难题奖励10(财富值+成长值)+提问者悬赏5(财富值+成长值)
1个回答
推荐于2016-11-07
展开全部
#include<iostream>
#include<cmath>
#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<<"半径为:"<<r<<" 圆心为:(3,3) 体积为:"<<volume<<endl;
}
#include<cmath>
#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<<"半径为:"<<r<<" 圆心为:(3,3) 体积为:"<<volume<<endl;
}
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?

2023-08-15 广告
通常情况下,我们会按照结构模型把系统产生的数据分为三种类型:结构化数据、半结构化数据和非结构化数据。结构化数据,即行数据,是存储在数据库里,可以用二维表结构来逻辑表达实现的数据。最常见的就是数字数据和文本数据,它们可以某种标准格式存在于文件...
点击进入详情页
本回答由光点科技提供
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询