• 第六周项目4-成员函数、友元函数和一般函数有区别


    (1)阅读下面的程序,体会注释中的说明。
    //例:使用成员函数、友元函数和一般函数的区别
    #include <iostream>
    using namespace std;
    class Time
    {
    public:
        Time(int h,int m,int s):hour(h),minute(m),sec(s) {}
        void display1();    //display1是成员函数
        friend void display2(Time &);  //display2是友元函数
        int getHour(){return hour;}
        int getMinute(){return minute;}
        int getSec(){return sec;}
    private:
        int hour;
        int minute;
        int sec;
    };
    void Time::display1()  //成员函数display1的实现,dispaly1前加Time::
    {
        //以hour形式直接访问私有数据成员,实质是this->hour形式
        cout<<hour<<":"<<minute<<":"<<sec<<endl;
    }
    void display2(Time &t)  //友元函数dispaly2的实现,不加Time::,友元并不是类的成员
    {
        //虽然不是类的成员函数,却可以用t.hour的形式直接访问私有数据成员——这就是友元
        cout<<t.hour<<":"<<t.minute<<":"<<t.sec<<endl;
    }
    void display3(Time &t)  //display3是一般函数,dispaly3前不加Time::
    {
        //不能直接访问,只能用公共接口t.getHour()形式访问私有数据成员
        cout<<t.getHour()<<":"<<t.getMinute()<<":"<<t.getSec()<<endl;
    }
    int main()
    {
        Time t1(10,13,56);
        t1.display1();  //成员函数这样调用:对象名.函数名()
        display2(t1);   //友员函数的调用和一般函数无异(但实现中可以不同)
        display3(t1);   //一般函数的调用
        return 0;
    }

    (2)模仿上面的示例,完成求点类中距离的任务。你需要实现求距离函数的三种版本:分别利用成员函数、友元函数和一般函数求两点间距离的函数,并设计main()函数完成测试。

    提示:此项目和例子的区别在于“距离是一个点和另外一个点的距离”,不同版本在参数上有体现。三个版本建议分开测试,也可以如示例,放在一个程序中完成。

    成员函数:

    /*
    * Copyright (c) 2015,烟台大学计算机学院
    * All right reserved.
    * 作者:邵帅
    * 文件:Demo.cpp
    * 完成时间:2015年04月16日
    * 版本号: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 Distance(CPoint p);
        void input();
        void output();
    };
    double CPoint::Distance(CPoint p)
    {
        double s;
        s=sqrt((p.x-x)*(p.x-x)+(p.y-y)*(p.y-y));
        return s;
    }
    void CPoint::input()
    {
        char ch;
        cout<<"请输入坐标(x,y):";
        cin>>x>>ch>>y;
    }
    void CPoint::output()
    {
        cout<<"("<<x<<", "<<y<<")"<<endl;
    }
    int main()
    {
        double d;
        CPoint p1,p2,p;
        cout<<"请输入第1个点p1,";
        p1.input();
        cout<<"请输入第2个点p2,";
        p2.input();
        d=p1.Distance(p2);
        cout<<"两点的距离为:"<<d<<endl;
        return 0;
    }
    


    友元函数

    /*
    * Copyright (c) 2015,烟台大学计算机学院
    * All right reserved.
    * 作者:邵帅
    * 文件:Demo.cpp
    * 完成时间:2015年04月16日
    * 版本号: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) {}
        friend double Distance(CPoint &,CPoint &);
        void input();
        void output();
    
    };
    double Distance(CPoint &p1,CPoint &p2)
    {
        double s;
        s=sqrt((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y));
        return s;
    }
    void CPoint::input()
    {
        char ch;
        cout<<"请输入坐标(x,y):";
        cin>>x>>ch>>y;
    }
    void CPoint::output()
    {
        cout<<"("<<x<<", "<<y<<")"<<endl;
    }
    int main()
    {
        double d;
        CPoint p1,p2,p;
        cout<<"请输入第1个点p1,";
        p1.input();
        cout<<"请输入第2个点p2,";
        p2.input();
        d=Distance(p1,p2);
        cout<<"两点的距离为:"<<d<<endl;
        return 0;
    }
    



    一般函数

    /*
    * Copyright (c) 2015,烟台大学计算机学院
    * All right reserved.
    * 作者:邵帅
    * 文件:Demo.cpp
    * 完成时间:2015年04月16日
    * 版本号: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) {}
    
        void input();
        void output();
        double getx()
        {
            return x;
        };
        double gety()
        {
            return y;
        };
    };
    double Distance(CPoint &,CPoint &);
    double Distance(CPoint &p1,CPoint &p2)
    {
        double s;
        s=sqrt((p1.getx()-p2.getx())*(p1.getx()-p2.getx())+(p1.gety()-p2.gety())*(p1.gety()-p2.gety()));
        return s;
    }
    void CPoint::input()
    {
        char ch;
        cout<<"请输入坐标(x,y):";
        cin>>x>>ch>>y;
    }
    void CPoint::output()
    {
        cout<<"("<<x<<", "<<y<<")"<<endl;
    }
    int main()
    {
        double d;
        CPoint p1,p2,p;
        cout<<"请输入第1个点p1,";
        p1.input();
        cout<<"请输入第2个点p2,";
        p2.input();
        d=Distance(p1,p2);
        cout<<"两点的距离为:"<<d<<endl;
        return 0;
    }
    



    @ Mayuko

  • 相关阅读:
    kfx格式的复活
    HTC(HTML Component)开发简介
    window.open window.showModelDialog 打开一个新窗口/子窗口中调用父窗口的方法
    用 Firebug 动态调试和优化应用程序
    HTC——浏览器上的舞者
    innerHTML、innerText和outerHTML、outerText的区别
    window.parent与window.opener的区别与使用
    window.parent与window.opener、window.showModalDialog的区别 opener和showModalDialog刷新父页面的方法
    HTML Component(HTC)
    Hibernate下数据批量处理解决方案
  • 原文地址:https://www.cnblogs.com/mayuko/p/4567508.html
Copyright © 2020-2023  润新知