您的位置:首页 > 其它

第六周 项目四--成员函数、友元函数和一般函数有区别 友元函数

2015-04-15 18:47 260 查看
/*
 * Copyright (c) 2015, 烟台大学计算机学院
 * All rights reserved.
 * 文件名称:test.cpp
 * 作    者:冷基栋
 * 完成日期:2015年 4 月 15 日
 * 版 本 号: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){}
    friend double distance(Cpoint &a,Cpoint &b);
    void input();
    void output();
};
double distance(Cpoint &a,Cpoint &b)
{
    return sqrt((b.x-a.x)*(b.x-a.x)+(b.y-a.y)*(b.y-a.y));
}
void Cpoint::input()
{
    char c;
    cin>>x>>c>>y;
}
void Cpoint::output()
{
    cout<<"("<<x<<","<<y<<")"<<endl;
}
int main()
{
    Cpoint a(2,3),b;
    a.output();
    cout<<"请输入b点坐标.";
    b.input();
    b.output();
    cout << "两点之间的距离是:"<<distance(a,b) << endl;
    return 0;
}




运行结果:

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