成员函数、友元函数和一般函数有区别

来源:互联网 发布:人工智能基础课程 编辑:程序博客网 时间:2024/04/28 18:02
/** 程序的版权和版本声明部分* Copyright (c)2014, 烟台大学计算机学院学生* All rightsreserved.* 文件名称: fibnacci.cpp* 作    者:高古尊* 完成日期:2014年4月7日* 版本号: 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 friend cpoin(CPoint &);    double juli();};void cpo(CPoint &);int main(){    CPoint t1(10,13);    cout<<t1.juli()<<endl;  //成员函数这样调用:对象名.函数名()    cpoin(t1);   //友员函数的调用和一般函数无异(但实现中可以不同)    cpo(t1);   //一般函数的调用    return 0;}double CPoint::juli(){    return sqrt(x*x+y*y);}void cpoin(CPoint &t){    cout<<sqrt(t.x*t.x+t.y*t.y)<<endl;}void cpo(CPoint &t){    cout<<t.juli()<<endl;}

0 0