问题及代码:
/*
*Copyright (c)2016,烟台大学计算机与控制工程学院
*All rights reserved.
*文件名称:main.cpp
*作 者:王艺霖
*完成日期:2016年4月9日
*版 本 号:v1.0
*
*问题描述:成员函数,友元函数和一般函数的区别
*/
#include <iostream>
#include<cmath>
using namespace std;
class CPoint//点类
{
private:
int x;//横坐标
int y;//纵坐标
public:
CPoint(int xx=0,int yy=0):x(xx),y(yy){}
int getX(){return x;}
int getY(){return y;}
};
class CLine
{
public:
CLine(CPoint xp1,CPoint xp2);
double getresult(){return result;}
private:
CPoint p1,p2;
double result;
};
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());
result=sqrt(x*x+y*y);
}
int main()
{
CPoint pointOne(0,0),pointTwo(3,4);
CLine line(pointOne,pointTwo);
cout<<"两点之间的距离为:";
cout<<line.getresult()<<endl;
return 0;
}
运行结果: