求点类中距离

来源:互联网 发布:tcp对数据大小限制 编辑:程序博客网 时间:2024/06/01 08:04

问题及代码:

/* * Copyright (c) 2014, 烟台大学计算机学院 * All rights reserved. * 文件名称:test.cpp * 作    者:李盈盈 * 完成日期:2015年 04 月 12 日 * 版 本 号:v1.0 * * 问题描述:分别利用成员函数、友元函数和一般函数求两点间的距离。 * 输入描述:无 * 程序输出:三次输出两点间的距离。 */#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){}    double display1(CPoint &);                    //成员函数    friend double display2(CPoint &,CPoint &);     //友元函数    double Getx()    {        return x;    }    double Gety()    {        return y;    }};double CPoint::display1(CPoint &b){    double d;    d=sqrt((x-b.x)*(x-b.x)+(y-b.y)*(y-b.y));    return d;}double display2(CPoint &a,CPoint &b){    double d;    d=sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));    return d;}double display3(CPoint &a,CPoint &b)              //一般函数{    double d;    d=sqrt((a.Getx()-b.Getx())*(a.Getx()-b.Getx())+(a.Gety()-b.Gety())*(a.Gety()-b.Gety()));    return d;}int main(){    CPoint a(1,2),b(3,4);    cout<<"两点间的距离为:"<<a.display1(b)<<endl;    cout<<"两点间的距离为:"<<display2(a,b)<<endl;    cout<<"两点间的距离为:"<<display3(a,b)<<endl;    return 0;}


运行结果:

学习心得:

  只要把三者完全搞清楚了,就不怕有改不正确的程序!加油!

0 0
原创粉丝点击