亚信联创2011.9.17招聘会笔试题

1、对于如下程序:

[csharp]  view plain copy
  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、如下的字符串函数,用于生存一个字符串 ”连接号码异常” ,并返回它的指针

[cpp]  view plain copy
  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型数据的函数,      

[cpp]  view plain copy
  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函数修改如下:

[cpp]  view plain copy
  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的原型为:

[cpp]  view plain copy
  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个函数普通构造函数、拷贝构造函数、析构函数和赋值函数。

代码如下:

[cpp]  view plain copy
  1. class String  
  2. {  
  3. private:  
  4.     char *m_data;                 //私有成员,保存字符串  
  5. public:  
  6.     String(const char *str = NULL);       //普通构造函数  
  7.     String(const String &other);          //复制构造函数  
  8.     ~String(void);                        //析构函数  
  9.     String & operator =(const String &other);      //赋值函数  
  10. };  
  11.   
  12. String::String(const char *str = NULL)    //带一个指针的普通构造函数  
  13. {  
  14.     if(str == NULL)  
  15.     {  
  16.         m_data = new char[1];    //分配一个字节  
  17.         assert(m_data != NULL);  
  18.         *m_data = '\0';  
  19.     }  
  20.     else  
  21.     {  
  22.         m_data = new char[strlen(str)+1];    //分配空间容纳str内容  
  23.         assert(m_data != NULL);  
  24.         strcpy(m_data,str);  
  25.     }  
  26. }  
  27.   
  28. String::String(const String &other)     //拷贝构造函数  
  29. {  
  30.     m_data = new char[strlen(other.m_data)+1];  
  31.     assert(m_data != NULL);  
  32.     strcpy(m_data,other.m_data);  
  33. }  
  34.   
  35. String::~String(void)            //析构函数  
  36. {  
  37.     if(m_data != NULL)  
  38.     {  
  39.         delete []m_data;  
  40.         m_data = NULL;  
  41.     }  
  42. }  
  43.   
  44. String & String::operator=(const String &other)     //赋值函数  
  45. {  
  46.     if(&other == this)            //如果对象与other是同一个对象  
  47.         return *this;  
  48.     delete []m_data;       //释放堆内存  
  49.     m_data = new char[strlen(other.m_data)+1];  
  50.     assert(m_data != NULL);  
  51.     strcpy(m_data,other.m_data);  
  52.     return *this;  
  53. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值