问题及代码:
/*Copyright (c)2016,烟台大学计算机与控制工程学院
*All rights reserved.
*文件名称:main.cpp
*作 者:郝昱猛
*完成日期:2016年4月20日
*
*问题描述:两点间距离之成员函数
*/
#include<iostream>
#include<cmath>
using namespace std;
class CPoint
{
public:
CPoint(double xx=0,double yy=0):x(xx),y(yy){}
double getX(){return x;}
double getY(){return y;}
private:
double x;//横坐标
double y;//纵坐标
};
class Line
{
public:
Line(CPoint p1,CPoint p2);
void len1();
private:
CPoint p1,p2;
double len;
};
Line::Line(CPoint xp1,CPoint xp2):p1(xp1),p2(xp2)
{
double x=p1.getX()-p2.getX();
double y=p1.getY()-p2.getY();
len=(double)sqrt(x*x+y*y);
}
void Line::len1()//成员函数len1()的实现,len1前加 Line
{
cout<<"The distance is: "<<len<<endl;
}
int main()
{
CPoint myp1(1.0,1.0),myp2(4.0,5.0);
Line line(myp1,myp2);
line.len1();
return 0;
}
运行结果: