亚信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.     }  


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