String

来源:互联网 发布:天刀捏脸好看的数据 编辑:程序博客网 时间:2024/05/19 09:38

<!-- /* Font Definitions */ @font-face{font-family:宋体;panose-1:2 1 6 0 3 1 1 1 1 1;mso-font-alt:SimSun;mso-font-charset:134;mso-generic-font-family:auto;mso-font-pitch:variable;mso-font-signature:3 135135232 16 0 262145 0;}@font-face{font-family:"Cambria Math";panose-1:2 4 5 3 5 4 6 3 2 4;mso-font-charset:1;mso-generic-font-family:roman;mso-font-format:other;mso-font-pitch:variable;mso-font-signature:0 0 0 0 0 0;}@font-face{font-family:"/@宋体";panose-1:2 1 6 0 3 1 1 1 1 1;mso-font-charset:134;mso-generic-font-family:auto;mso-font-pitch:variable;mso-font-signature:3 135135232 16 0 262145 0;} /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal{mso-style-unhide:no;mso-style-qformat:yes;mso-style-parent:"";margin:0cm;margin-bottom:.0001pt;text-align:justify;text-justify:inter-ideograph;mso-pagination:none;font-size:10.5pt;mso-bidi-font-size:12.0pt;font-family:"Times New Roman","serif";mso-fareast-font-family:宋体;mso-font-kerning:1.0pt;}p.MsoHeader, li.MsoHeader, div.MsoHeader{mso-style-noshow:yes;mso-style-unhide:no;mso-style-link:"页眉 Char";margin:0cm;margin-bottom:.0001pt;text-align:center;mso-pagination:none;tab-stops:center 207.65pt right 415.3pt;layout-grid-mode:char;border:none;mso-border-bottom-alt:solid windowtext .75pt;padding:0cm;mso-padding-alt:0cm 0cm 1.0pt 0cm;font-size:9.0pt;font-family:"Times New Roman","serif";mso-fareast-font-family:宋体;mso-font-kerning:1.0pt;}p.MsoFooter, li.MsoFooter, div.MsoFooter{mso-style-noshow:yes;mso-style-unhide:no;mso-style-link:"页脚 Char";margin:0cm;margin-bottom:.0001pt;mso-pagination:none;tab-stops:center 207.65pt right 415.3pt;layout-grid-mode:char;font-size:9.0pt;font-family:"Times New Roman","serif";mso-fareast-font-family:宋体;mso-font-kerning:1.0pt;}p.MsoDate, li.MsoDate, div.MsoDate{mso-style-noshow:yes;mso-style-unhide:no;mso-style-link:"日期 Char";mso-style-next:正文;margin:0cm;margin-bottom:.0001pt;text-align:justify;text-justify:inter-ideograph;mso-pagination:none;font-size:10.5pt;mso-bidi-font-size:10.0pt;font-family:"Times New Roman","serif";mso-fareast-font-family:宋体;mso-font-kerning:1.0pt;}span.Char{mso-style-name:"页眉 Char";mso-style-noshow:yes;mso-style-unhide:no;mso-style-locked:yes;mso-style-link:页眉;mso-ansi-font-size:9.0pt;mso-bidi-font-size:9.0pt;mso-font-kerning:1.0pt;}span.Char0{mso-style-name:"页脚 Char";mso-style-noshow:yes;mso-style-unhide:no;mso-style-locked:yes;mso-style-link:页脚;mso-ansi-font-size:9.0pt;mso-bidi-font-size:9.0pt;mso-font-kerning:1.0pt;}span.Char1{mso-style-name:"日期 Char";mso-style-noshow:yes;mso-style-unhide:no;mso-style-locked:yes;mso-style-link:日期;mso-ansi-font-size:10.5pt;mso-font-kerning:1.0pt;}.MsoChpDefault{mso-style-type:export-only;mso-default-props:yes;font-size:10.0pt;mso-ansi-font-size:10.0pt;mso-bidi-font-size:10.0pt;mso-ascii-font-family:"Times New Roman";mso-fareast-font-family:宋体;mso-hansi-font-family:"Times New Roman";mso-font-kerning:0pt;} /* Page Definitions */ @page{mso-page-border-surround-header:no;mso-page-border-surround-footer:no;}@page Section1{size:595.3pt 841.9pt;margin:70.9pt 3.0cm 70.9pt 3.0cm;mso-header-margin:42.55pt;mso-footer-margin:1.0cm;mso-paper-source:0;layout-grid:17.5pt .7pt;mso-layout-grid-char-alt:2824;}div.Section1{page:Section1;}-->

已知类String的原型为:

    class String

    {

      public:

        String(constchar *str = NULL); // 普通构造函数

        String(constString &other);        // 拷贝构造函数

        ~ String(void);                     // 析构函数

        String &operate =(const String &other);    // 赋值函数

      private:

        char    *m_data;                // 用于保存字符串

    };

       请编写String的上述4个函数。

标准答案:

 

// String的析构函数

       String::~String(void)               // 3

{

    delete [] m_data;                     

// 由于m_data是内部数据类型,也可以写成 delete m_data;

       }

 

       //String的普通构造函数            

       String::String(constchar *str)      // 6

{

    if(str==NULL)                         

    {

       m_data = new char[1];    // 若能加 NULL 判断则更好

       *m_data = /0;                     

    }                                         

    else

    {

       int length = strlen(str);          

       m_data = new char[length+1];  // 若能加 NULL 判断则更好     

       strcpy(m_data, str);               

    }

}  

// 拷贝构造函数

    String::String(constString &other)   // 3

    {  

    int length = strlen(other.m_data);

    m_data = new char[length+1];      // 若能加 NULL 判断则更好   

    strcpy(m_data, other.m_data);        

}

// 赋值函数

    String &String::operate =(const String &other)   // 13

    {  

       // (1) 检查自赋值                     // 4

       if(this ==&other)

           return*this;

   

// (2)释放原有的内存资源            // 3

       delete []m_data;

      

       // 3)分配新的内存资源,并复制内容 // 3

    int length = strlen(other.m_data);

    m_data = new char[length+1];         // 若能加 NULL 判断则更好

        strcpy(m_data, other.m_data);

      

       // 4)返回本对象的引用            // 3

       return *this;

}  

原创粉丝点击