您的位置:首页 > 其它

第5周-项目4-成员函数-求点类中距离-区别成员函数、友元函数和一般函数

2015-04-11 13:44 295 查看

问题及代码:

/*   
*Copyright (c)2015,烟台大学计算机与控制工程学院   
*All rights reserved.   
*文件名称:point.cpp   
*作    者:单昕昕   
*完成日期:2015年4月11日   
*版 本 号:v1.0   
*问题描述:求点类中距离。
*程序输入:两个点。
*程序输出:两个点的距离。
*/
#include <iostream>
#include <cmath>
using namespace std;
class CPoint
{
private:
    double x;  // 横坐标
    double y;  // 纵坐标
public:
    CPoint(double xx=0,double yy=0):x(xx),y(yy) {}
    double Distance1(CPoint p) const; //两点之间的距离
    void input();  //以x,y 形式输入坐标点
    void output(); //以(x,y) 形式输出坐标点
};

double CPoint::Distance1(CPoint p) const//两点之间的距离
{
    return sqrt((p.x-x)*(p.x-x)+((p.y-y)*(p.y-y)));
}
void CPoint::input()//以x,y 形式输入坐标点
{
    char c;
    cin>>x>>c>>y;
}
void CPoint::output()//以(x,y) 形式输出坐标点
{
    cout<<"("<<x<<","<<y<<")"<<endl;
}

int main()
{
    CPoint p1,p2,p;
    cout<<"请以“x,y”形式输入点p1的坐标:";
    p1.input();
    cout<<"请以“x,y”形式输入点p2的坐标:";
    p2.input();
    cout<<"p1到p2的距离="<<p1.Distance1(p2)<<endl;
    return 0;
}


运行结果:



知识点总结:

用成员函数求点类中距离。

学习心得:

复习了一下之前学习的成员函数~~
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐