A-A+

有以下程序: #include <iostream> #include <cmath> us

2022-08-06 03:52:26 问答库 阅读 175 次

问题详情

有以下程序: #include <iostream> #include <cmath> using namespace std; class Distance; class point { public: friend class Distance; Point(int a,int B) { x=a; Y=b; } void Print() { cout<<"X= "<<X<<end1; cout<<"Y= "<<Y<<end1; } private: float X,Y; }; class Distance { public: float Dis(Point &p,Point &q); }; float Distance :: Dis(Point &p,Point &q) { float result; result=sqrt((p.X-q.X)*(p.X-q.X)+(p.Y-q.Y)*(p.Y-q.Y)); cout<<result<<end1; retUrn result; } int main() { Point p(10,10),q(10,30); Distance d; d.Dis(p,q); return 0; } 运行后的输出结果是()。
A.10
B.30
C.0
D.20请帮忙给出正确答案和分析,谢谢!

参考答案

正确答案:D
解析:本题程序通过把类Distance定义为类Point类的友元类来实现计算两点之间距离的功能。主函数中定义两个对象点p,q,然后调用对象d的成员函数Dis()计算两点之间的距离。

考点:程序