亚信联创2011.9.17招聘会笔试题

题目来源:http://blog.csdn.net/hackbuteer1/article/details/6823272

3、const 有什么用途?(请至少说明两种)

答:(1)可以定义 const 常量。

(2)const可以修饰函数的参数、返回值,甚至函数的定义体。被const修饰的东西都受到强制保护,可以预防意外的变动,能提高程序的健壮性。


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;  

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 & operator =(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(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;  


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值