/*
* 程序的版权和版本声明部分
* Copyright (c)2013, 在校学生
* All rightsreserved.
* 文件名称: 项目3.cpp
* 作 者:刘旺
* 完成日期:2014年4月8日
* 版本号: v1.0
*
* 输入描述:
* 问题描述:你需要完成的任务是,利用成员函数、友元函数和一般函数,
* 实现三个版本的求两点间距离的函数,并设计main()函数完成测试。
* 程序输出:
* 问题分析:
*/
#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){}
//请继续写需要的代码
void display(const CPoint &p) ;
friend void display2(const CPoint &p1, const CPoint &p2) ;
double getX(){return x ;}
double getY(){return y ;}
};
void CPoint::display(const CPoint &p)
{
cout << "成员函数: 俩点距离为:" << sqrt((x-p.x)*(x-p.x)+(y-p.y)*(y-p.y)) << endl ;
}
void display2(const CPoint &p1, const CPoint &p2)
{
cout << "友元函数: 俩点距离为:" << sqrt((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y)) << endl ;
}
void display3(CPoint &p1, CPoint &p2)
{
cout << "普通函数: 俩点距离为:" << sqrt((p1.getX()-p2.getX())*(p1.getX()-p2.getX())+(p1.getY()-p2.getY())*(p1.getY()-p2.getY())) ;
}
int main()
{
CPoint p1(1,1) ;
CPoint p2(2,2) ;
p1.display(p2) ;
display2(p1,p2) ;
display3(p1,p2) ;
return 0 ;
}
相信自己一定可以做到,并不断努力着。。。