/*
*Copyright (c)2016,烟台大学计算机与控制工程学院
*All rights reserved.
*文件名称:main.cpp
*作 者:隋文韬
*完成日期:2016年4月21日
*版 本 号:v1.0 *
*问题描述:成员函数,友元函数和一般函数的区别
*/
#include<iostream>
#include<cmath>
using namespace std;
class CPoint//点类
{
private:
double x; //横坐标
double y; //纵坐标
public:
CPoint(int xx=0,int yy=0):x(xx),y(yy){}
int getX(){return x;}
int getY(){return y;}
friend double fri1(CPoint &p1,CPoint &p2);//友元函数
};
double fri1(CPoint &p1,CPoint &p2)//友元函数的实现
{
double x=p1.x-p2.x;
double y=p1.y-p2.y;
return sqrt(x*x+y*y);
}
class CLine//直线类
{
public:
CLine(CPoint xp1,CPoint xp2);
double getLen(){return len;}
private:
CPoint p1,p2;
double len;
};
CLine::CLine(CPoint xp1,CPoint xp2):p1(xp1),p2(xp2)
{
double x=static_cast<double>(p2.getX()-p1.getY());
double y=static_cast<double>(p2.getY()-p1.getY());
len=sqrt(x*x+y*y);
}
double fri2(CPoint &p1,CPoint &p2)//一般函数的实现
{
double x=p1.getX()-p2.getX();
double y=p1.getY()-p2.getY();
return sqrt(x*x+y*y);
}
int main()
{
CPoint coordinatesOne(1,1),coordinatesTwo(4,5);
CLine line(coordinatesOne,coordinatesTwo);
cout<<"两点之间的距离为:";
cout<<line.getLen()<<endl;
cout<<"两点之间的距离为:";
cout<<fri1(coordinatesOne,coordinatesTwo)<<endl;
cout<<"两点之间的距离为:";
cout<<fri2(coordinatesOne,coordinatesTwo)<<endl;
return 0;
return 0;
}