/*
*Copyright (c) 2016,烟台大学计算机学院
*All rights reserved.
*文件名称:zyt.cpp
*作 者:张毓桐
*完成日期:2016年9月10日
*版 本 号:v1.0
*
*问题描述:用友元函数设计点类求两点之间的距离
*输入描述:
*程序输出:两点间距离
*/
#include<iostream>
#include<cmath>
using namespace std;
class CPoint
{
private:
double x; // 横坐标
double y; // 纵坐标
public:
CPoint(double xx=0,double yy=0):x(xx),y(yy){}
double getx(){return x;}
double gety(){return y;}
friend double line(CPoint &p1,CPoint &p2); //友元函数的声明
};
/*
class Line
{
public:
Line(CPoint xp1,CPoint xp2);
Line (Line &l);
double getlen(){return len;}
private:
CPoint p1,p2;
double len;
};
*/
double line(CPoint &p1,CPoint &p2)
{
double x=p1.x-p2.x;
double y=p1.y-p2.y;
return sqrt(x*x+y*y);
}
int main()
{
CPoint p1(1,1),p2(4,6.2);
// Line line(p1,p2);
cout<<"p1为:("<<p1.getx()<<","<<p1.gety()<<")"<<endl;
cout<<"p2为:("<<p2.getx()<<","<<p2.gety()<<")"<<endl;
cout<<"两点间距离为:"<<line(p1,p2)<<endl;
return 0;
}
第七周项目1-点类-友元函数
最新推荐文章于 2024-03-18 10:33:36 发布