《C++面向对象程序设计》实验报告
通函数)
注:以点(0,0)和(3,4)作为测试数据,求出它们之间的距离。 ①
[源程序]
#include
class Point {
private: float x,y; public: Point(float a,float b){x=a;y=b;}; void distance(Point p); };
void Point::distance(Point p) { float distance; distance=sqrt((p.x-x)*(p.x-x)+(p.y-y)*(p.y-y)); cout<<\两点间距离为:\}
void main() { Point p1(0,0),p2(3,4); p1.distance(p2); } ②
[源程序]
#include
class Point {
private:
第 6 页
《C++面向对象程序设计》实验报告
double x,y; public: Point(double a,double b){x=a;y=b;}; friend void distance(Point p1,Point p2); };
void distance(Point p1,Point p2) { double distance; distance=sqrt((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y)); cout<<\两点间距离为:\}
void main() { Point p1(0,0),p2(3,4); distance(p1,p2); }
第 7 页