嘻唰唰第六批Problem H: B 友元类-计算两点间距离
2015-07-21 18:47
393 查看
Description
类Distance定义为类Point的友元类来实现计算两点之间距离的功能。Point类中有两个私有数据成员X和Y来表示点的两个坐标(横坐标和纵坐标), 成员函数需要自己定义。
主程序输入两个Point点的坐标,计算两个点之间的距离。
类Distance的声明如下:
class Distance
{ public:
float Dis(Point & p,Point & q);
};
程序主函数如下:
int main()
{
float x1,y1,x2,y2;
cin>>x1>>y1>>x2>>y2;
Point p(x1,y1), q(x2,y2);
cout<<setiosflags(ios::fixed);
cout<<setprecision(2);
Distance d;
cout<<d.Dis(p,q)<<endl;
return 0;
}
要求:将整个程序补充完整,即添加Point类,并完成Distance类成员函数的类外定义。
注意:提交时不用提交主程序,其它都要提交。
Input
两个点的坐标(横坐标和纵坐标)Output
两个点的距离(保留了两位小数)Sample Input
1.0 1.0 2.0 2.0
Sample Output
1.41
代码;
#include <iostream> #include <iomanip> #include <cmath> using namespace std; class Point; class Distance { public: float Dis(Point & p,Point & q); }; class Point { private: float x; float y; public: Point(float a,float b):x(a),y(b) {} friend class Distance; }; float Distance::Dis(Point & p,Point & q) { return sqrt((p.x-q.x)*(p.x-q.x)+(p.y-q.y)*(p.y-q.y)); } int main() { float x1,y1,x2,y2; cin>>x1>>y1>>x2>>y2; Point p(x1,y1), q(x2,y2); cout<<setiosflags(ios::fixed); cout<<setprecision(2); Distance d; cout<<d.Dis(p,q)<<endl; return 0; }
相关文章推荐
- 第三方框架-01.MJExtension基本使用
- OJ第六批——Problem J: B 虚拟继承(虚基类)-沙发床(改错题)
- 嘻唰唰第六批Problem G: B 求类中数据成员的最大值-类模板
- 理解Node.js的事件循环(代码是异步单线程,内部实现用的还是进程和线程,基于池化的线程实现异步)
- 管理神话2:专家只有权这样做
- What is CAS
- IOS第二天
- iOS WriteToFile的使用
- 新手导航
- OJ第六批——Problem I: B 继承 圆到圆柱体
- UASCO Combination Lock 解析 and C 语言实现
- C语言基础6
- avalon.js 多级下拉框实现
- 试用AWS的Lambda和API Gateway
- 嘻唰唰第六批Problem F: A代码完善--简易二元运算
- poj 2312 Battle City 优先队列
- 内存泄露——不包含头文件类的析构函数不执行
- Android设计模式之美---Factory Method
- day06
- 【web开发学习笔记】Structs2 Action学习笔记(两)