亞信2011筆試題目

1、對於如下程序:

  1. #include  <iostream>  
  2. using namespace std;  
  3. class A  
  4. {  
  5. public:  
  6.     A()  
  7.     {  
  8.         cout<<"A"<<endl;  
  9.     }  
  10. };  
  11.   
  12. int  main(void)  
  13. {  
  14.     A a[4], b,*p;  
  15. }  

會輸出多少個A?(   C   )

A、2                   B、3                        C、5                            D、6

p只是一個對象指針,並沒有指向一個對象的內存空間,所以沒有調用構造函數。

2、頭文件中的 ifndef/define/endif 有什麼作用?
答:防止該頭文件被重複引用,避免變量、類型等被重新定義。

3、const 有什麼用途?(請至少說明兩種)

答:(1)可以定義 const 常量。
(2)const可以修飾函數的參數、返回值,甚至函數的定義體。被const修飾的東西都受到強制保護,可以預防意外的變動,能提高程序的健壯性。
4、如下的字符串函數,用於生存一個字符串 ”連接號碼異常” ,並返回它的指針

  1. char* strfun()  
  2. {  
  3.     char str[20];  
  4.     strcpy(str, “連接號碼異常”);  
  5.     printf(“%s \n”, str);             //printf語句1  
  6.     return str;  
  7. }  
  8. void main()  
  9. {  
  10.     char *pstr = strfun();  
  11.     printf("%s \n", pstr);            //printf語句2  
  12. }  

問題1 : printf語句1和printf語句2哪個能在屏幕上正在打印出來?

問題2 : 如果不能正常在屏幕上打印出字符串,請說明原因。

問題3 : 如果不修改strfun的聲明,請問該如何修改上述程序的錯誤。

答:

問題1:語句1可以正常打印,語句2不能正常打印;

問題2:語句2使用的指針所指向的內存空間str[20],在函數strfun返回時已經被釋放了;

問題3:可以將函數strfun中的語句char str[20];改爲char *str = new char[20];

5、下面是交換兩個double型數據的函數,      

  1. void swap( double* p1, double* p2 )  
  2.  {  
  3.      double *p;  
  4.      *p = *p1;  
  5.      *p1 = *p2;  
  6.      *p2 = *p;  
  7.  }  
  8.  void main()  
  9.  {  
  10.      double a = 0.1;  
  11.      double b = 0.2;  
  12.      swap( &a, &b );  
  13.  }  

請找出上述代碼的錯誤,指出錯誤的原因,並改正。

答:函數swap中混淆了double型指針與double型變量的差別,對於一個未初始化的指針訪問其內存空間是非常危險的。對swap函數修改如下:

  1. void swap( double* p1, double* p2 )  
  2. {  
  3.     double p;  
  4.     p = *p1;  
  5.     *p1 = *p2;  
  6.     *p2 =p;  
  7. }  

6、在電信業務的後臺處理程序中,經常會涉及到處理字符串,除了用char *處理字符串之外,C++還爲我們提供了封裝了的字符串類string,其本質也是用一個動態數組來保存字符串,類String的原型爲:

  1. class String  
  2. {  
  3. public:  
  4.     String(const char *str = NULL); // 普通構造函數  
  5.     String(const String &other);        // 拷貝構造函數  
  6.     ~String(void);                      // 析構函數  
  7.     String & operate =(const String &other);    // 賦值函數  
  8. private:  
  9.     char *m_data;               // 用於保存字符串  
  10. };  

請編寫String的上述4個函數普通構造函數、拷貝構造函數、析構函數和賦值函數。

代碼如下:

  1. class String  
  2. {  
  3. private:  
  4.     char *m_data;  
  5. public:  
  6.     String();  
  7.     String(const char *str = NULL);  
  8.     String(const String &other);  
  9.     ~String(void);  
  10.     String & operator =(const String &other);  
  11. };  
  12. String::String()  
  13. {  
  14.     m_data = NULL;  
  15. }  
  16.   
  17. String::String(const char *str = NULL)    //帶一個指針的普通構造函數  
  18. {  
  19.     if(str == NULL)  
  20.     {  
  21.         m_data = new char[1];  
  22.         assert(m_data != NULL);  
  23.         *m_data = '\0';  
  24.     }  
  25.     else  
  26.     {  
  27.         int length=strlen(str);  
  28.         m_data = new char[length+1];  
  29.         assert(m_data != NULL);  
  30.         strcpy(m_data,str);  
  31.     }  
  32. }  
  33.   
  34. String::String(const String &other)     //拷貝構造函數  
  35. {  
  36.     m_data = new char[other.length+1];  
  37.     assert(m_data != NULL);  
  38.     strcpy((*this).m_data,other.m_data);  
  39. }  
  40.   
  41. String::~String(void)            //析構函數  
  42. {  
  43.     if(m_data != NULL)  
  44.     {  
  45.         delete m_data;  
  46.         m_data = NULL;  
  47.     }  
  48. }  
  49.   
  50. String & String::operator=(const String &other)     //賦值函數  
  51. {  
  52.     if(&other != this)  
  53.     {  
  54.         delete [](*this).m_data;  
  55.         (*this).m_data = new char[other.length+1];  
  56.         assert((*this).m_data != NULL);  
  57.         strcpy((*this).m_data,other.m_data);  
  58.     }  


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章