有以下程序:includeincludeusingnamespacestd;classDistance;classpoint{pub有以下程序: #include <iostream> #include <cmath> using namespace std; class Distance; class point { public: friend class Distance; Point(int a,int B) { x=a; Y=b; } void Print() { cout<<"X= "<<X<

题目
有以下程序:includeincludeusingnamespacestd;classDistance;classpoint{pub

有以下程序: #include <iostream> #include <cmath> using namespace std; class Distance; class point { public: friend class Distance; Point(int a,int B) { x=a; Y=b; } void Print() { cout<<"X= "<<X<<end1; cout<<"Y= "<<Y<<end1; } private: float X,Y; }; class Distance { public: float Dis(Point &p,Point &q); }; float Distance :: Dis(Point &p,Point &q) { float result; result=sqrt((p.X-q.X)*(p.X-q.X)+(p.Y-q.Y)*(p.Y-q.Y)); cout<<result<<end1; retUrn result; } int main() { Point p(10,10),q(10,30); Distance d; d.Dis(p,q); return 0; } 运行后的输出结果是( )。

A.10

B.30

C.0

D.20


相似考题
参考答案和解析
正确答案:D
解析:本题程序通过把类Distance定义为类Point类的友元类来实现计算两点之间距离的功能。主函数中定义两个对象点p,q,然后调用对象d的成员函数Dis()计算两点之间的距离。
更多“有以下程序:#include<iostream>#include<cmath>usingnamespacestd;classDistance;classpoint{pub ”相关问题
  • 第1题:

    以下程序的执行结果是【】。 include include void pnnt(int n) { if (n!=0

    以下程序的执行结果是【 】。

    include<iostream.h>

    include<iomanip.h>

    void pnnt(int n)

    {

    if (n!=0)

    {

    Print(n-1);

    for (int i=1;i<=n;i++)

    cout<<setw(3)<<i;

    cout<<endl;

    }

    }

    void main()

    {

    print(4);

    }


    正确答案:1 12 123 1234
    1 12 123 1234

  • 第2题:

    有以下程序:

    include”iostream.h”

    void main()

    {int i=10;

    int j=5;

    cout((j+i++<<endl;}

    的结果为______。


    正确答案:15。
    15。 解析: 考查++运算符的用法,各种运算符的应用,注意++,--。i++为先做与i有关的运算后,i自加 1,而++i是先给i加1再进行与i有关的操作。

  • 第3题:

    以下程序的输出结果是【】。 include include void main 0 { char s[50]; st

    以下程序的输出结果是【 】。

    include<iostream.h>

    include <string.h>

    void main 0 {

    char s[50];

    strcpy(&s[O], "No" );

    strcpy(&s[1], "123" );

    strcpy (&s[2], "23456" );

    cout<<s;

    }


    正确答案:N123456
    N123456

  • 第4题:

    下面程序的执行结果是【】。 include include using namespace std; void main(

    下面程序的执行结果是【 】。

    include<iostream>

    include<iomanip>

    using namespace std;

    void main()

    {

    cout<<setfill('x')<<setw(10);

    cout<<"Hello"<<endl;

    }


    正确答案:xxxxxHello
    xxxxxHello

  • 第5题:

    有以下程序includeincludevoid main( ){char*p="abcde\0fghjik\0"; cou

    有以下程序 #include<string.h> #include<iostream.h> void main( ) { char*p="abcde\0fghjik\0"; cout<<strlen(p);} 程序运行后的输出结果是

    A.12

    B.15

    C.6

    D.5


    正确答案:D
    解析:C++语言规定了一个字符串结束标志,以字符'\0'代表,在遇到'\0'时,表示字符串结束,由它前面的字符组成字符串。

  • 第6题:

    以下程序的执行结果是【】。 include include include int ma

    以下程序的执行结果是【 】。

    include <iostream.h>

    include <fstream.h>

    include <stdlib.h>

    int main()

    {

    fstream outfile, infile;

    outfile.open("D:\\text.dat",ios::out);

    if(!outfile)

    {

    cout<<"text.dat can't open"<<end1


    正确答案:1:1234567890/2:abcdfghij
    1:1234567890/2:abcdfghij 解析:本题考核文件的I/O操作。先来了解eof()成员函数,该函数返回非0值表示已到文件结尾。程序首先定义fstream类的2个对象 outfile和infile。然后在文件text.dat写入两行字符串“1234567890”和“abcdefghii”。最后用getline()函数读取出来存入字符数组textline[40]中,并按行显示出来。程序中的两个if语句是用于打开文件失败的异常处理。

  • 第7题:

    如有以下程序: #include(iostream> usingnamespacestd; longfun(intn) { if(n)2) return(fun(n-1)+fun(n-2)); else return2; } intmain( ) { cout<<fun(3)<<endl; return0; } 则该程序的输出结果应该是( )。

    A.2

    B.3

    C.4

    D.5


    正确答案:C
    C。【解析】主函数中调用fun(3),在fun内部当n>2时,递归调用fun(n-1)+fun(n-2),否则返回2。所以当n=3时,调用fun(2)+fun(1),即返回2+2,等于4。

  • 第8题:

    有以下程序:include include using namespace std;int main ( ){ ofstream

    有以下程序: #include <iostream> #include <fstream> using namespace std; int main ( ) { ofstream ofile; char ch; ofile.open ("abc.txt"); cin>>ch; while (ch!='#' ) { cin>>ch; ofile.put(ch);

    A.程序编译时出错

    B.abc#

    C.abc

    D.#


    正确答案:C
    解析:本题程序的功能是将从键盘终端输入的内容存储到指定的文件中。

  • 第9题:

    以下程序的运行结果是【】。 include include using namespace std; void main()

    以下程序的运行结果是【 】。

    include<iostream>

    include<string>

    using namespace std;

    void main(){

    chara[10]="China",b[]="Chin",c[]="ese";

    cout<<strlen(strcat(strcpy(a,b),c))<<endl;

    }


    正确答案:7
    7 解析:本题主要考查C++中字符串函数的使用。strcpy(s1,s2)将s2的内容赋值到s1中; strcat(s1,s2)连接s1和s2两个字符串;strlen(s)返回字符数组s的长度。因此最后输出的结果是b和c进行连接后的字符串长度,即7。

  • 第10题:

    有以下程序: include include using namespace std; int main() {char arr[

    有以下程序: #include<iostream> #include<string> using namespace std; int main() { char arr[2][4]; strcpy(arr[0],"you"); strcpy(arr[1],"me"); arr[0][3]='&'; cout<<arr[0]<<end1; return 0; } 执行后的输出结果是( )。

    A.you&me

    B.you

    C.me

    D.err


    正确答案:A
    解析:本题考核字符串函数的使用。主函数中,首先定义了千个二维字符数组art。语句“strcpy(arr,"you");”中的alt代表二维字符数组的首元素地址,此语句的作用是将字符串“you”复制到arr数组的前4个元素中,第4个元素的值为'\0'。语句“strcpy(arr[1],"me");”的作用是把字符串“me”赋值到arr数组的第2行。语句“arr[0][3]='及';”的作用是用字符'&'取代了原来arr[0][3]中的字符'\0'。所以程序最后输出you&me。

  • 第11题:

    有以下程序:include include using namespace std;int main ( ){ char b1[8

    有以下程序: #include <iostream> #include <string> using namespace std; int main ( ) { char b1[8] = "abcdefg"; char b2[8],*pb=b1+3; while (--pb>=b1) strcpy (b2, Pb) ; cout<<strlen (b2) <<end1; return 0; } 程序运行后的输出结果是( )。

    A.8

    B.3

    C.1

    D.7


    正确答案:D
    解析:本题考查常用字符串函数的熟悉程度。先来了解程序中的两个字符串函数:①函数strcpy()的函数原型为:char*strcpy(char*strDest,constchar*strSrC);,其功能是复制strSrc所有字符到strDest,并返回strDest。②函数strlen()的函数原型为:sizetstrlen(constchar*string);,其函数功能为:返回string的长度,不包括结束字符'\0'。再看程序:程序首先定义了一个字符数组b1和一个指针pb,并让指针pb指向数组中的b1[3]。由于在while语句中,每次循环都是把指针pb所指向的字符串复制到数组b2中,所以可以不考虑循环的中间过程,直接分析最后一次循环。循环体中的最后一次循环条件是pb==b1,即指针pb指向了数组元素b1[0],此时把指针pb所指向的字符串复制到数组b2中,就相当于把数组b1中的字符串复制到数组b2中。所以最后数组b2中保存的就是数组b1中的字符串,其长度为7(不包括字符串结束符号'\0')。

  • 第12题:

    下列程序的输出结果是 include usingnamespacestd; int main () {chara []="Hello,W

    下列程序的输出结果是 #include <iostream> using namespace std; int main () { char a [] = "Hello,World": char*ptr = a; while (*ptr) { if(*ptr>= 'a' &&*ptr <='z' cout<<char{*ptr+'A'-'a'); else cout<<*ptr; ptr++; } return 0; }

    A. HELLO. WORLD

    B. Hello, World

    C. hELLO, wORLD

    D. hello, world


    正确答案:A
    解析:本题考核while语句和if语句,while语句中if语句的作用是将小写字母变成大写字母输出、所以main函数的字符串通过while语句全部输出为大写字母。

  • 第13题:

    以下程序的输出结果 ______。 include void main() { int a=0; a+ =(a=8); cout<

    以下程序的输出结果 ______。

    include<iostream.h>

    void main()

    {

    int a=0;

    a+ =(a=8);

    cout<<a;

    }


    正确答案:16
    16

  • 第14题:

    有以下程序: include using namespace std; int main() {int i=010,j=10;cout<<(++i)

    有以下程序:

    include <iostream>

    using namespace std;

    int main()

    {

    int i=010,j=10;

    cout<<(++i)<<","<<i--<<end1;

    return 0;

    }

    则该程序运行后的输出结果是【 】。


    正确答案:910
    9,10 解析:本题考核自增运算符和自减运算符的使用以及常量的表示形式。程序中i的值是八进制,代表十进制中的8,++i是在语句运算前自身加1,而j--是在语句运算结束后减1。

  • 第15题:

    有以下程序: include usingnamespacestd; classCFactorial { private:intvalue;intfac

    有以下程序:

    include <iostream>

    using namespace std;

    class CFactorial

    {

    private:

    int value;

    int fact;

    public:

    CFactorial ( int val );

    void CalculateFactorial();

    void Display();

    };

    CFactorial :: CFactorial( int val )

    {

    value = val;

    fact = 1;

    }

    void CFactorial :: CalculateFactorial()

    {

    int i = value;

    while ( i > 1 )

    fact *= i--;

    }

    void CFactorial :: Display()

    {

    cout<<value<<"!="<<fact<<end1;

    }

    int main()

    {

    CFactorial A( 5 );

    A.CalculateFactorial();

    A.Display();

    return 0;

    }

    程序中,类CPactorial的功能是【 】,该程序运行的结果是【 】。


    正确答案:求自然数的阶乘 120
    求自然数的阶乘 120 解析:类CFactorial中的成员函数CalculateFactorial()实现的功能是:求自然数value的阶乘结果。

  • 第16题:

    有以下程序:include using namespace std; class Base { public: Base() { K=0; } int

    有以下程序:

    include<iostream>

    using namespace std;

    class Base

    {

    public:

    Base()

    {

    K=0;

    }

    int x;

    };

    class Derivedl:virtual public Base

    {

    public:

    Derivedl()

    {

    x=10;

    }

    };

    class Derived2:virtua1 public Base


    正确答案:20。
    20。 解析: 本题中,虽然Derived1和Derived2由于引入了虚基类,使得它们分别对应基类的不同副本。这时数据成员x只存在一份拷贝,不论在类Derived1中修改,还是在类Derived2中修改,都是直接对这惟一拷贝进行操作。本题程序执行语句“Derived obi;”时,就会先调用虚基类Base的构造函数,使得x=0,然后执行类Derived1的构造函数使得x=10,再执行类Derived2的构造函数,使得x=20。最后输出x的值为20。

  • 第17题:

    有以下程序: include usingnamespacestd; intmain() { intnum[2][3],*p; intt,i,j,k=

    有以下程序: #include <iostream> using namespace std; int main() { int num[2][3],*p; int t,i,j,k=10; p=num[0]; for(i=0;i<2;i++) { for(j=0;j<3;j++) { k++; *p=k; for(t=2;t<*p;t++) { if(!(*p%t)) { j--; *p--; break; } *p++; } } cout<<*(p-1)<<end1; return 0; } 程序运行后的输出结果是( )。

    A.19

    B.11

    C.29

    D.23


    正确答案:C
    解析:本题考核数组与指针的关系。程序首先初始化指针p,让其指向二维数组num的首元素,即num[0][0]。程序中,第1个for循环是为二维数组的每行元素赋值而设置的循环,第2个for循环是为二维数组的每列元素赋值而设定的,第3个for循环是为判断数组元素是否是素数而设置的。在第3个for循环中,如果*p的值能被小于它的数整除(即不是素数),则执行“j--;*p--;”,然后跳出第3个for循环。j--的含义是让其前面的赋值语句重新赋值,而*p--的含义是使指针指向前一个单元。语句“*p++;”将指针移到下一个单元,在下一次循环时,k加1,并k赋给*p,看*p是否为素数,这样一直到满足条件为止,即数组num[2][3]中的数都为素数。最后数组num中各元素的值分别为大于10的素数,即分别为:11,13;17,19,23,29。程序最后要求输出*p的值,由于此时指针已指向数组num的最后一个元素,即num[1][2]。所以输出的值是29。

  • 第18题:

    有以下程序:includeusingnamespacestd;definePI3.14classPoint{private: intx,y;pub

    有以下程序: #include <iostream> using namespace std; #define PI 3.14 class Point { private: int x,y; public: Point(int a,int b) { x=a; y=b; } int getx() { return x; } int gety() { return y; } }; class Circle : public Point { private: int r; public: Circle(int a,int b,int c):Point(a,b) { r=c; } int getr() { return r; } double area() { return PI*r*r; } }; int main() { Circle c1(5,7,10); cout<<cl.area()<<endl; return 0; } 程序执行后的输出结果是

    A.314

    B.157

    C.78.5

    D.153.86


    正确答案:A
    解析:本题考核派生类的定义和应用。本程序设计了一个点类Point,包含了横、纵两个坐标数据x和y,由它派生出了圆类Circle,并加入了新的数据成员,即一个半径r和一个求圆面积的函数成员area。在主函数main中,首先定义了一个圆Circle类的对象c1,并通过它的构造函数初始化其数据成员。由此可知,其半径r的值为10,所以其面积为PI*10*10=314,即对象c1的函数成员area的返回值为314。

  • 第19题:

    有以下程序:include include using namespace std;class base{private: cha

    有以下程序: #include <iostream> #include <string> using namespace std; class base { private: char baseName[10]; public: base ( ) { strcpy (baseName, "Base"); } virtual char *myName() {

    A.DerivedBase

    B.BaseBase

    C.DerivedDerived

    D.BaseDerived


    正确答案:A
    解析:本题考核虚函数的应用。类Derived是从基类Base公有派生而来的。因此,Derived是基类Base的子类型。主函数中定义了一个基类对象bb和一个派生类对象dd。从程序中可看出,派生类Derived的对象dd交给了处理基类Base的对象的函数showPtr进行处理。由于在基类中函数myName被定义成虚函数,所以在函数showPtr中调用的myName函数为派生类的成员函数mySame,从而输出Derived。然后输出className,即基类名称Base。

  • 第20题:

    有下列程序:includeusing namespace std;class ONE{ public:virtual void f(){COUt<<"

    有下列程序:

    include<iostream>

    using namespace std;

    class ONE

    {

    public:

    virtual void f(){COUt<<"1";}

    };

    c1assTWO:public ONE

    {

    public:

    TWO(){cout<<"2";}

    };

    class THREE:public TWO

    {

    pub


    正确答案:2213
    2213 解析: 此题考查的是派生类的构造和析构函数。建立TWO的对象bb时,调用TWO的构造函数,输出“2”;THREE类又派生于TWO类,所以建立THREE类的对象cc时又会输出“2”;ONE类的对象指针p指向了THREE类的ONE类的虚函数f(),输出“1”;最后调用THREE类的f(),输出“3”。故最终的输出结果是2213。

  • 第21题:

    有以下程序: #include(iostream) usingnamespacestd; intmain( ) { intx=15: while(x>10&&x<50) { x++; if(x/3) { x++;break; }

    } cout<<x<<endl; return0; } 执行后的输出结果是( )。

    A.15

    B.16

    C.17

    D.18


    正确答案:C
    C。【解析】本题考核选择语句与循环语句的嵌套。由程序可知,当x=16时,满足if条件表达式,进入if分支,执行“x++;break;”,所以最后程序输出x的值为17。

  • 第22题:

    有以下程序:includeusing namespace std;Class A{public:A(){tout{("A"}};classB{pub

    有以下程序: #include<iostream> using namespace std; Class A{ public: A(){tout{("A"} }; classB{public:B(){cout<<"B";>> classC:public A{ B b; public: C(){cout<<"C";} }; int main(){C obj;return 0;} 执行后的输出结果是( )。

    A.CBA

    B.BAC

    C.ACB

    D.ABC


    正确答案:D
    解析: 本题考查的是类的继承和派生。系统首先要通过派生类的构造函数调用基类的构造函数,对基类成员初始化,然后对派生类中的新增成员初始化。

  • 第23题:

    有以下程序:include include void main(){double d= 123.456789;cout<

    有以下程序:#include <iostream.h>#include void main(){ double d= 123.456789; cout<<setprecision(3)<<d<<","; cout<<setprecision(4)<<d<<","; cout<<setprecision(5 )<<d<<end1;}程序执行后的输出结果是( )。

    A.123,123.4,123.45

    B.123,123.5,123.46

    C.123,123,123.4

    D.123.456789,123.456789,123.456789


    正确答案:B