/*
* Copyright(c)2016,烟台大学计算机与控制工程学院
* All rights reserved.
* 文件名称:第七周项目1:成员函数,友元函数和一般函数的区别
* 作者:刘默涵
* 完成日期:2016年4月14日
* 版本号:v1.0
*
* 问题描述:在互联网中使用的IP地址占4字节,可以用四段法表示,每段值的范围为0-255,中间用“.”隔开,例如202.194.116.97.其实,也可以看看一个有4字节的无符号整形值3401741409.现设计一个IP地址类,用于保存IP地址,并实施在IP地址上的一些操作。
* 输入描述:
* 程序输出:
*/
- using namespace std;
- class CPoint
- {
- private:
- double x; // 横坐标
- double y; // 纵坐标
- public:
- CPoint(double xx=0,double yy=0):x(xx),y(yy) {}
- void display1(CPoint &); //成员函数声明
- friend void display2(CPoint &,CPoint &); //友元函数声明
- double getx()
- {
- return x;
- }
- double gety()
- {
- return y;
- }
- };
- void display3(CPoint &,CPoint &); //一般函数声明
- void CPoint::display1(CPoint &p) //成员函数的实现,dispaly1前加CPoint::
- {
- double d;
- d=sqrt((p.x-x)*(p.x-x)+(p.y-y)*(p.y-y));
- cout<<"两点间的距离为:"<<d<<endl;
- }
- void display2(CPoint &t1,CPoint &t2) //友元函数的实现,dispaly2前不加Time::,并不是类的成员函数
- {
- double d;
- d=sqrt((t1.x-t2.x)*(t1.x-t2.x)+(t1.y-t2.y)*(t1.y-t2.y));
- cout<<"两点间的距离为:"<<d<<endl;
- }
- void display3(CPoint &t1,CPoint &t2) //一般函数的实现,dispaly1前加Time::
- {
- double d;
- d=sqrt((t1.getx()-t2.getx())*(t1.getx()-t2.getx())+(t1.gety()-t2.gety())*(t1.gety()-t2.gety()));
- cout<<"两点间的距离为:"<<d<<endl;
- }
- int main()
- {
- CPoint p1(10,13), p2(-5,6);
- p1.display1(p2);
- display2(p1,p2);
- display3(p1,p2);
- return 0;
- }