用一般函数设计点类求两点之间的距离
2016-04-13 22:31
204 查看
/* *Copyright (c) 2016,烟台大学计算机学院 *All rights reserved. *文件名称:zyq.cpp *作 者:赵彦庆 *完成日期:2016年4月13日 *版 本 号: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 getx(){return x;} double gety(){return y;} // friend double line(CPoint &p1,CPoint &p2); //友元函数的声明 }; double line(CPoint &p1,CPoint &p2) { double x,y; x=(p1.getx()-p2.getx()); y=(p1.gety()-p2.gety()); return sqrt(x*x+y*y); } /* class Line { public: Line(CPoint xp1,CPoint xp2); Line (Line &l); double getlen(){return len;} private: CPoint p1,p2; double len; }; double line(CPoint &p1,CPoint &p2) { double x=p1.x-p2.x; double y=p1.y-p2.y; return sqrt(x*x+y*y); } */ int main() { CPoint p1(1,1),p2(4,6.2); // Line line(p1,p2); cout<<"p1为:("<<p1.getx()<<","<<p1.gety()<<")"<<endl; cout<<"p2为:("<<p2.getx()<<","<<p2.gety()<<")"<<endl; cout<<"两点间距离为:"<<line(p1,p2)<<endl; return 0; }
知识点总结:一般函数调用类中的成员。
学习心得:一般函数访问类中的函数,需要公共接口。
相关文章推荐
- [SSH]一个Hibernate错误--IllegalArgumentException calling setter
- 刚挣钱的程序员同学该如何花钱?
- 刚挣钱的程序员同学该如何花钱?
- 刚挣钱的程序员同学该如何花钱?
- Dagger2----一个最简单的Dagger2依赖的实现
- Android屏蔽Home按键
- iOS 用 SDWebImage 清理图片缓存
- C#多线程——线程池和回调函数
- Python 中enumerate的使用
- 慢慢解读web.xml
- 某些库函数的模拟实现(一)
- 如何使用CSS实现小三角形效果
- linux及安全《Linux内核设计与实现》第三章——20135227黄晓妍
- TCP/IP篇--各协议简介
- Android自学之百度地图API使用遇到的一些问题
- 神经网络入门基础知识 neural networks basics
- java实例化的理解
- swift 中使用扩展实现代理,让程序猿省心,让代码简洁
- 如何根据wsdl文件生成客户端
- 获取手机通讯录联系人