以下程序运行后的输出结果是______。includeinclude usingnamespacestd;classY以下程序运行后的输出结果是______。include <iostream>include <string>using namespace std;class Y;class X{int x;char *strx;public:X(int a, char *str){x=a;strx=new char[strlen(str)+1]strcpy (strx,str);}void show(Y

题目
以下程序运行后的输出结果是______。includeinclude usingnamespacestd;classY

以下程序运行后的输出结果是______。

include <iostream>

include <string>

using namespace std;

class Y;

class X

{

int x;

char *strx;

public:

X(int a, char *str)

{

x=a;

strx=new char[strlen(str)+1]

strcpy (strx,str);

}

void show(Y &ob);

};

class Y

{

prlvate:

int y;

char *stry;

public:

Y(int b,char *str)

{

y=b;

stry=new char[strlen(str)+1];

strcpy(stry,str);

}

friend void X::show(Y &ob);

};

void X::show{Y &ob)

{

cout<<strx<<",",

cout<<ob.stry<<endl;

}

int main{

{

X a (10, "stringX");

Y b (20, "stringY");

a. show (b);

renurn 0;

}


相似考题
参考答案和解析
正确答案:stringX stringY
stringX stringY 解析:本题考核友元函数的应用。该程序中,类X的成员函数show()在类Y中说明为类Y的友元函数,因此,在该友元成员show()中可以访问类Y的私有成员stry.成员函数show()的功能就是输出类X的私有成员strx和 Y对象ob的私有成员stry。主函数main()中定义了 X类的一个对象a和Y类的一个对象b,并且都进行了初始化.然后调用对象a的成员函数show,输出对象a中私有成员strx中的内容和对象b中私有成员stry中的内容,即字符串stringX和stringY。
更多“以下程序运行后的输出结果是______。include<iostream>include <string>usingnamespacestd;classY ”相关问题
  • 第1题:

    以下程序的运行结果是【】。 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。

  • 第2题:

    有以下程序: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')。

  • 第3题:

    【单选题】以下程序运行后的输出结果是_____。#include <iostream>using namespace std;void main(){ char m; m='B'+32; cout<<m<<endl;}

    A.B

    B.b

    C.66

    D.98


    7

  • 第4题:

    有以下程序: 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。

  • 第5题:

    有以下程序:include include using namespace std;class Y;class X{private

    有以下程序: #include <iostream> #include <string> using namespace std; class Y; class X { private: int x; char *strx; public: X(int a, char *str) { x=a; strx=new char[strlen(str)+1]; strcpy(strx,str); } void show(Y &ob) ; }; class Y { private: int y; char *stry; public: Y(int b,char *str) { y=b; stry=new char[strlen(str)+1]; strcpy(stry, str); } friend void X: :show(Y &ob) ; }; void X: :show(Y &ob) { cout<<strx<<", "; cout<<ob, stry<<end1; } int main ( ) { X a(10,"X"); Y b (20, "Y"); a. show(B) ; return 0; } 执行后的输出结果是( )。

    A.X,Y

    B.a,b

    C.X,X

    D.Y,Y


    正确答案:A
    解析:本题考核类的定义和友元函数的应用。①该程序中,类X的成员函数show()在类Y中说明为友元,因此,在该友元成员show()中可以访问类Y的私有成员stry。②成员函数show()的功能就是输出类X的私有成员strx和Y对象ob的私有成员stry,③主函数main()中定义了X类的一个对象a和Y类的一个对象b,并且都进行了初始化。然后调用对象a的成员函数show,输出对象a中私有成员strx中的内容和对象b中私有成员stry中的内容,即字符串stringX和stringY。