/* 程序中定义Point类。请在横线处补充程序以使程序完整, 使该程序执行结果为:The distance is 3.60555 */ #include<iostream> #include<cmath> using namespace std; class Point{ private: float x,y; public: //请选中下方横线,补充代码 Point(float x1,float y1):_________①________{} float getx( ){return x;} float gety( ){return y;} //请选中下方横线,补充代码 ___________②_____________; }; float dis(Point &a,Point &b) { float dx=a.x-b.x; float dy=a.y-b.y; return sqrt(dx*dx+dy*dy); } int main( ) { Point p1(6,7),p2(3,5); float d=dis(p1,p2); cout<<"The distance is "<<d<<endl; return 0; }
时间: 2024-03-31 13:36:52 浏览: 110
重载为类的成员函数-C++程序设计
#include<iostream>
#include<cmath>
using namespace std;
class Point{
private:
float x,y;
public:
Point(float x1,float y1):x(x1),y(y1){} //①构造函数初始化
float getx( ){return x;}
float gety( ){return y;}
friend float dis(Point &a,Point &b); //②声明友元函数
};
float dis(Point &a,Point &b) {
float dx=a.x-b.x;
float dy=a.y-b.y;
return sqrt(dx*dx+dy*dy);
}
int main( ) {
Point p1(6,7),p2(3,5);
float d=dis(p1,p2);
cout<<"The distance is "<<d<<endl;
return 0;
}
阅读全文