非常满意意 2022-05-31 16:09 采纳率: 90.9%
浏览 133
已结题

C++编写一个自定义字符串类mystring

完善以下函数

#include <iostream>
using namespace std;
const int maxsize = 256;
class mystring
{
    char str[maxsize];
    int len;
public:
    mystring()
    {
        len = 0;
        str[0] = '\0';
        cout << "缺省构造函数" << endl;
    }
    mystring(char* s)
    {
        ...//要完善的部分
        cout << "构造函数(用C风格字符串初始化)" << endl;
    }
}//复制构造函数
mystring(mystring& ms)
{
    ...//要完善的部分
    cout << "拷贝构造函数" << endl;
}
~mystring()
{
    cout << "析构函数" << endl;
}
void show()
{ //打印字符串
    ...//要完善的部分
}
};
int main()
{
    char str[21] = "Hello C++";
    mystring A(str);
    mystring B = A;
    A.show();
    return 0;
}
  • 写回答

1条回答 默认 最新

  • HWsir 2022-05-31 16:58
    关注

    img

    
    #include <iostream>
    using namespace std;
    const int maxsize = 256;
    class mystring
    {
        char str[maxsize];
        int len;
    public:
        mystring()
        {
            len = 0;
            str[0] = '\0';
            cout << "缺省构造函数" << endl;
        }
        mystring(char* s)
        {
            //...//要完善的部分
            int i=0;
            for(i=0;s[i]!='\0';i++)
            {
                str[i]=s[i];
            }
            len=i;
            cout << "构造函数(用C风格字符串初始化)" << endl;
        }
    //复制构造函数
    mystring(mystring& ms)
    {
        //...//要完善的部分
        len=ms.len;
        for(int i=0;i<len;i++)
        {
            str[i]=ms.str[i];
        }
        cout << "拷贝构造函数" << endl;
    }
    ~mystring()
    {
        cout << "析构函数" << endl;
    }
    void show()
    { //打印字符串
        //...//要完善的部分
        for(int i=0;i<len;i++)
       {
            cout<<str[i];
        }
        cout<<'\n';
    }
    };
    int main()
    {
        char str[21] = "Hello C++";
        mystring A(str);
        mystring B = A;
        A.show();
        return 0;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 6月8日
  • 已采纳回答 5月31日
  • 赞助了问题酬金5元 5月31日
  • 创建了问题 5月31日

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效