有如下程序: #inc1ude <iostream> #inc1ude <cmath> using std::cout; class Point{ public: friend double distance

admin2019-05-06  9

问题 有如下程序:
    #inc1ude <iostream>
    #inc1ude <cmath>
    using std::cout;
    class Point{
    public:
    friend double distance(const Point &p);    //p距原点的距离
    Point(int xx=0, int yy=0): x(xx), y(yy){}    //①
    private:
    int x,y;
    };
    double distance(const Point &p){    //②
    return sqrt(p.x*p.x+p.y*p,y);
    }
    int main(){
    Point pl(3,4);
    cout<<distance(pl);    //③
    return 0;
    }
    下列叙述中正确的是(     )。

选项 A、程序编译正确
B、程序编译时语句①出错
C、程序编译时语句②出错
D、程序编译时语句③出错

答案A

解析 这段程序是正确的,计算p距离原点的距离。语句①是类Point的构造函数,在构造函数中给变量xx和yy赋初值0。语句②是计算点p距原点的距离的函数distance。语句③是计算点pl距离原点的距离并输出。
转载请注明原文地址:https://kaotiyun.com/show/Xo8p777K
0

最新回复(0)