设计一个点类,求两个之间距离

chimes298 2014-09-06 05:11:25
各位老师,设计一个点类,求两个之间距离,程序如下写法有什么缺陷,或者不好的习惯?
#include <iostream>
#include <math.h>
using namespace std;

class point
{private:
float x;
float y;

public:
point():x(0),y(0){};
point(float a,float b):x(a),y(b){};
point(const point &p){ x=p.x; y=p.y;};

virtual float distance(const point &p);
virtual ~point(){};
};

float point::distance(const point &p)
{
float d=sqrt(pow(p.x-x,2)+pow(p.y-y,2));
return d;
}

int main()
{
point a(2,3);
point b(1,1);
float res=a.distance(b);
cout << res << endl;
return 0;
}
...全文
1508 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
qq_34564266 2016-04-06
  • 打赏
  • 举报
回复
#include <iostream> #include <math.h> using namespace std; class point { private: float x; float y; float z; public: point() :x(0), y(0),z(0){}; point(float a, float b,float z) :x(a), y(b),z(c){}; point(const point &p){ x = p.x; y = p.y; z = p.z; }; virtual float distance(const point &p); virtual ~point(){}; }; float point::distance(const point &p) { float d = sqrt((x*x + y*y + z*z)); return d; } int main() { point a(2, 3,6); point b(1, 1,4); float res = a.distance(b); cout << res << endl; return 0; } 请问这个代码中c点有什莫问题吗??谢谢了
昆仑道长 2014-09-07
  • 打赏
  • 举报
回复
从类的实现功能上来看, 还不错; 但是从测试程序来看,有些地方还是可以改进的. 控制台的程序最好说明一下你的输出结果是用来干什么的. 比如,你可以这样:
cout<<"the distance between point(2,3) and point(1,1) is : " <<res<<endl;
cout<<"点a(2,3)到点b(1,1)之间的距离="<<res<<endl;
山在岭就在 2014-09-07
  • 打赏
  • 举报
回复
还可以吧感觉,我也是新手
chimes298 2014-09-07
  • 打赏
  • 举报
回复
多谢各位指点
飞天御剑流 2014-09-06
  • 打赏
  • 举报
回复
引用 楼主 chimes298 的回复:
各位老师,设计一个点类,求两个之间距离,程序如下写法有什么缺陷,或者不好的习惯?
#include <iostream>
#include <math.h>
using namespace std;

class point
{private:
        float x;
        float y;

 public:
        point():x(0),y(0){};
        point(float a,float b):x(a),y(b){};
        point(const point &p){ x=p.x; y=p.y;};

        virtual float distance(const point &p);
        virtual ~point(){};
};

float point::distance(const point &p)
{
        float d=sqrt(pow(p.x-x,2)+pow(p.y-y,2));
        return d;
}

int main()
{
 point a(2,3);
 point b(1,1);
 float res=a.distance(b);
 cout << res << endl;
 return 0;
}
求两点间距离的操作不是一个点自身的行为,不应作为point的成员函数,应实现为独立的算法。
FeelTouch Labs 2014-09-06
  • 打赏
  • 举报
回复
话说,你这个程序真没看出啥问题。 这里有一个类供你参考:
class Point
{
	friend double length(const Point &,const Point &);
public:
	Point(double xx,double yy): x(xx),y(yy) {}
	void setx(double xx){x = xx;}
	double getx(){return x;}
	void sety(double yy){y = yy;}
	double gety(){return y;}
private:
	double x,y;		
};
brookmill 2014-09-06
  • 打赏
  • 举报
回复
point():x(0),y(0){}; // 最后这个分号看着有点别扭,我是不写的,不过这可能是个人习惯问题吧
brookmill 2014-09-06
  • 打赏
  • 举报
回复
float point::distance(const point &p) const // 成员函数能加上const就加上,当然声明也要加 { return sqrt(pow(p.x-x,2)+pow(p.y-y,2)); // 没必要多用一个变量d,float这样的基本类型还好,如果是类就得多调用一次拷贝构造函数 }
熊熊大叔 2014-09-06
  • 打赏
  • 举报
回复
顺便说一句,c的标准库(math.h)中有一个函数 double hypot(double x, double y); 用来计算sqrt(power(x, 2) + power(y, 2))
Falleyes 2014-09-06
  • 打赏
  • 举报
回复
如果你不写继承类,不建议把distance和析构函数声明为virtual。

64,683

社区成员

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

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