[YTU]_2619 (B 友元类-计算两点间距离)
生活随笔
收集整理的這篇文章主要介紹了
[YTU]_2619 (B 友元类-计算两点间距离)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
題目描述
類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類成員函數的類外定義。
注意:提交時不用提交主程序,其它都要提交。
輸入
兩個點的坐標(橫坐標和縱坐標)
輸出
兩個點的距離(保留了兩位小數)
樣例輸入
1.0 1.0 2.0 2.0樣例輸出
1.41#include <iostream> #include <iomanip> #include <math.h> using namespace std; class Point; class Distance { public:float Dis(Point &p,Point &q); }; class Point { public:friend class Distance;Point(float x1,float y1):x(x1),y(y1){} private:float x,y; }; float Distance::Dis(Point &p,Point &q) {return sqrt((q.x-p.x)*(q.x-p.x)+(q.y-p.y)*(q.y-p.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;? }《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀
總結
以上是生活随笔為你收集整理的[YTU]_2619 (B 友元类-计算两点间距离)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: [YTU]_2618 ( B 求类中数据
- 下一篇: [YTU]_2626( B 统计程序设计