有如下程序: #include #include using namespace std; class MyString{ public: char str[80]; MyString(con

admin2020-11-11  16

问题 有如下程序:
    #include
    #include
    using namespace std;
    class MyString{
    public:
      char str[80];
      MyString(const char*s){strcpy(str,s);}
      MyString& operator+=(MyString a){
        strcat(str,a.str);
       return*this;
      }
    };
    ostream&operator<<(ostream&s,const MyString&z){return s<    int main(){
      MyString x(’abc’),y(’ode’);
      cout<<(x+=y)<      return 0;
    }
    运行这个程序的输出结果是(    )。

选项 A、abC
B、cde
C、abcde
D、abccde

答案D

解析 在类MyString中,定义了带参数的构造函数MyString(const char*s),其作用是把s指向的字符串拷贝到字符组s仃中。在类中还对运算+=进行重载定义,其作用是把字符串str和a相连接并赋给str,所以在主函数中执行x+=y时,结果为ahccde。
转载请注明原文地址:https://kaotiyun.com/show/aayp777K
0

相关试题推荐
随机试题
最新回复(0)