第七周任务(2)

来源:互联网 发布:安卓解压缩软件 编辑:程序博客网 时间:2024/05/12 07:26
#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){}      friend   double L1(CPoint &,CPoint &);//友函数声明。double L2(CPoint c2);//成员函数声明double getx(){return x;}double gety(){return y;}};double L3(CPoint &,CPoint &);//一般函数声明。double L1(CPoint &c1,CPoint &c2)//友函数定义。{double d;d=sqrt((c1.x-c2.x)*(c1.x-c2.x)+(c1.y-c2.y)*(c1.y-c2.y));return d;}double CPoint::L2(CPoint c2)//成员函数定义。{double d;d=sqrt((c2.x-x)*(c2.x-x)+(c2.y-y)*(c2.y-y));return d;}double L3(CPoint &c1,CPoint &c2)//一般函数定义。{double d;d=sqrt((c1.getx()-c2.getx())*(c1.getx()-c2.getx())+(c1.gety()-c2.gety())*(c1.gety()-c2.gety()));return d;}int main(){CPoint c1(5,6),c2(8,2);cout<<"两点间的距离:";cout<<c1.L2(c2);cout<<endl;cout<<"两点间的距离:";cout<<L1(c1,c2);cout<<endl;cout<<"两点间的距离:";cout<<L3(c1,c2);cout<<endl;system("pause");return 0;}     在c++语言中,我从一般函数,成员函数,友函数的区别中,类中成员的封闭性。
原创粉丝点击