<pre id="bbfd9"><del id="bbfd9"><dfn id="bbfd9"></dfn></del></pre>

          <ruby id="bbfd9"></ruby><p id="bbfd9"><mark id="bbfd9"></mark></p>

          <p id="bbfd9"></p>

          <p id="bbfd9"><cite id="bbfd9"></cite></p>

            <th id="bbfd9"><form id="bbfd9"><dl id="bbfd9"></dl></form></th>

            <p id="bbfd9"><cite id="bbfd9"></cite></p><p id="bbfd9"></p>
            <p id="bbfd9"><cite id="bbfd9"><progress id="bbfd9"></progress></cite></p>

            c/c++程序員面試題

            時間:2024-07-18 18:44:49 面試筆試 我要投稿
            • 相關推薦

            c/c++程序員面試題

              編寫類String的構造函數、析構函數和賦值函數,已知類String的原型為:

            c/c++程序員面試題

              class String

              {

              public:

              String(const char *str = NULL); // 普通構造函數

              String(const String &other); // 拷貝構造函數

              ~ String(void); // 析構函數

              String & operate =(const String &other); // 賦值函數

              private:

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

              };

              解答:

              //普通構造函數

              String::String(const char *str)

              {

              if(str==NULL)

              {

              m_data = new char[1]; // 得分點:對空字符串自動申請存放結束標志'\0'的空

              //加分點:對m_data加NULL 判斷

              *m_data = '\0';

              }

              else

              {

              int length = strlen(str);

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

              strcpy(m_data, str);

              }

              }

              // String的析構函數

              String::~String(void)

              {

               [] m_data; // 或 m_data;

              }

              //拷貝構造函數

              String::String(const String &other)    // 得分點:輸入參數為const型

              {

              int length = strlen(other.m_data);

              m_data = new char[length+1];     //加分點:對m_data加NULL 判斷

              strcpy(m_data, other.m_data);

              }

              //賦值函數

              String & String::operate =(const String &other) // 得分點:輸入參數為const型

              {

              if(this == &other)   //得分點:檢查自賦值

              return *this;

               [] m_data;     //得分點:釋放原有的內存資源

              int length = strlen( other.m_data );

              m_data = new char[length+1];  //加分點:對m_data加NULL 判斷

              strcpy( m_data, other.m_data );

              return *this;         //得分點:返回本對象的引用

              }

              剖析:

              能夠準確無誤地編寫出String類的構造函數、拷貝構造函數、賦值函數和析構函數的面試者至少已經具備了C++基本功的60%以上!

              在這個類中包括了指針類成員變量m_data,當類中包括指針類成員變量時,一定要重載其拷貝構造函數、賦值函數和析構函數,這既是對C++程序員的基本要求,也是《Effective C++》中特別強調的條款。

              仔細學習這個類,特別注意加注釋的得分點和加分點的意義,這樣就具備了60%以上的C++基本功!


            【c/c++程序員面試題】相關文章:

            C++程序員求職信11-21

            C++程序員求職信范文11-21

            普天C++筆試題02-18

            基礎C++/C語言筆試題分享11-21

            有心情的試一試,考查C++/C程序員的基本編程技能11-11

            群碩筆試題Java和C++、C#11-21

            C++工程師筆試題目11-25

            最新凌陽科技C++筆試分享11-21

            UC(優視科技)2014實習筆試題回憶版 C/C++編程11-21

            面試題精選02-18

                    <pre id="bbfd9"><del id="bbfd9"><dfn id="bbfd9"></dfn></del></pre>

                    <ruby id="bbfd9"></ruby><p id="bbfd9"><mark id="bbfd9"></mark></p>

                    <p id="bbfd9"></p>

                    <p id="bbfd9"><cite id="bbfd9"></cite></p>

                      <th id="bbfd9"><form id="bbfd9"><dl id="bbfd9"></dl></form></th>

                      <p id="bbfd9"><cite id="bbfd9"></cite></p><p id="bbfd9"></p>
                      <p id="bbfd9"><cite id="bbfd9"><progress id="bbfd9"></progress></cite></p>
                      飘沙影院