一个简单的问题,请帮忙用c++解决

jhonsn 2005-07-09 10:16:05
1.设计一个描述平面中圆的类,一个圆用圆点坐标x,y和半径r表示,要求能对一个圆能进行以下操作:
(1)能修改半径r
(2)能修改原点坐标x,y
(3)能得到半径r
(4)能得到原点坐标x,y
(5)能得到圆面积
另外,在建立一个圆类对象时,缺省圆点坐标是圆点,半径是0,请设计类class Circle
...全文
105 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
jhonsn 2005-07-09
  • 打赏
  • 举报
回复
谢谢大家,因为我没有学C++,学的是别的语言.我先验证一下,然后结帐
MagicCarmack 2005-07-09
  • 打赏
  • 举报
回复
楼主为什么不自己写了


boxban 2005-07-09
  • 打赏
  • 举报
回复

class Circle
{
public:
Circle(int r = 0, int x = 0, int y = 0) : m_r(r), m_x(x), m_y(y) {}
~Circle(){}

public:
void setRadius(int r) { m_r = r;} //能修改半径r
int getRadius() const { return m_r; } //(3)能得到半径r
void setCenter(int x, int y) { m_x = x; m_y = y; } //(2)能修改原点坐标x,y
void getCenter(int& x, int& y) const { x = m_x; y = m_y; } //(4)能得到原点坐标x,y
double getArea() const { return (3.14159 * m_r * m_r); } //能得到圆面积

private:
int m_x, m_y, m_r;
};
UltraEdit32 2005-07-09
  • 打赏
  • 举报
回复
/**
* Circle.cpp
* Ahh, I just practise typing in the morning.
*/

class Point
{
private:
int xPos;
int yPos;
public:
//constructors
Point() {}
Point(Point& p);
Point(int x, int y);
};

Point::Point(Point& p)
{
this->xPos = p.xPos;
this->yPos = p.yPos;
}

Point::Point(int x, int y) : xPos(x), yPos(y)
{}


class Circle
{
private:
double radius;
Point center;

static const double PI = 3.1415926;
public:
//constructors
Circle() {}
Circle(int r, Point& c);

double getR();//to get the radius
void setR(double r);//to set the radius
Point& getC();//to get the center
void setC(Point& c);//to set the center

double getArea();
};

Circle::Circle(int r, Point& c) : radius(r), center(c)
{}

double Circle::getR()
{
return this->radius;
}

void Circle::setR(double r)
{
this->radius = r;
}

Point& Circle::getC()
{
return this->center;
}

void Circle::setC(Point& c)
{
center = c;
}

double Circle::getArea()
{
double r = this->getR();
double pi = Circle::PI;
return pi*r*r;
}

#if 1 //set to 0 if you don't want to build the following code
#include <iostream>
int main()
{
using namespace std;
Circle circle1;
circle1.setR(1);
Point center(0, 0);
circle1.setC(center);
cout << circle1.getArea() << endl;
return 0;
}
#endif

manuever 2005-07-09
  • 打赏
  • 举报
回复
这个很简单,楼主应该可以写得出来。

64,683

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧