第七周——成员函数、友元函数、一般函数的区别

/* Copyright (c) 2013, 烟台大学计算机学院
* All rights reserved.
* 作    者:申玉迪
* 完成日期:2014 年 4 月 8 日
* 版 本 号: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){}
    void distance1(CPoint);
    friend void distance2(CPoint &,CPoint &);
    double getX()
    {
        return x;
    }
    double getY()
    {
        return y;
    }

};

void CPoint::distance1(CPoint p)
{
    double l;
    l=sqrt((p.x-x)*(p.x-x)+(p.y-y)*(p.y-y));
    cout<<"两点间距离为:"<<l<<endl;
}

void distance2(CPoint &m,CPoint &n)
{
    double l;
    l=sqrt((m.x-n.x)*(m.x-n.x)+(m.y-n.y)*(m.y-n.y));
     cout<<"两点间距离为:"<<l<<endl;
}

void distance3(CPoint &t,CPoint &s)
{
    double l;
    l=sqrt((t.getX()-s.getX())*(t.getX()-s.getX())+(t.getY()-s.getY())*(t.getY()-s.getY()));
    cout<<"两点间距离为:"<<l<<endl;
}

int main()
{
    CPoint c1(4,5);
    CPoint c2(1,1);
    c1.distance1(c2);
    distance2(c1,c2);
    distance3(c1,c2);
}


 

比着葫芦画葫芦。。。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值