2012各大公司的C++试题

那么多C++试题,其实最关键的是要知道考察的知识点在哪里。哎,应该好好重新看看《effective c++》了。

网易3道C++试题:

题目1:

[cpp]  view plain copy
  1. class A  
  2. {  
  3. public:  
  4.     A(int j):i(j)  
  5.     {  
  6.         fun1();           //here:在构造函数中调用了虚函数  
  7.     }  
  8.     ~A()  
  9.     {  
  10.     }  
  11.     void fun1()  
  12.        {  
  13.            i *= 10;  
  14.            fun2();  
  15.        }  
  16.     int getValue(){  
  17.         return i;  
  18.     }  
  19. protected:  
  20.     virtual void fun2()  
  21.     {  
  22.         i++;  
  23.     }  
  24. private:  
  25.     int i;  
  26. };  
  27.   
  28. class B:public A  
  29. {  
  30. public:  
  31.     B(int j):A(j)  
  32.     {  
  33.     }  
  34.     virtual ~B()  
  35.     {  
  36.     }  
  37.     void fun1()  
  38.       {  
  39.           i *= 100;  
  40.           fun2();  
  41.       }  
  42. protected:  
  43.     void fun2()  
  44.     {  
  45.         i += 2;  
  46.     }  
  47. private:  
  48.     int i;  
  49. };  
  50. int main()  
  51. {  
  52.      A* p = new B(1);  
  53.      cout<<p->getValue()<<endl;  
  54.      delete p;  
  55.      return 0;  
  56. };  

考点:在构造函数中调用了虚函数。

根据《effective c++》条款09:绝不要在构造函数和析构函数中调用虚函数,因为:这类调用从不下降至子类。此时,对象在derived class调用之前,绝不会成为一个derived对象。

所以,答案是11(而不是12)。

题目2:

[cpp]  view plain copy
  1. class A{  
  2. public:  
  3.     virtual int fun(int i = 1);  
  4. };  
  5. int A::fun(int i){return i + 1;}  
  6. class B:public A{  
  7. public:  
  8.     virtual int fun(int i = 10);  
  9. };  
  10. int B::fun(int i){return i;}  
  11. int main()  
  12. {  
  13. //    A* p = new B(1);  
  14. //    cout<<p->getValue()<<endl;  
  15. //    delete p;  
  16.     A *p = new B();  
  17.     cout << p->fun();  
  18.     return 0;  
  19. };  

考点:哎,虚函数是动态绑定的,但是默认参数是静态绑定的。

所以,答案是1。而不是10。靠!

题目三:

     考察C++中static 成员、const成员、static const成员的初始化时机:

在C++中,static静态成员变量不能在类的内部初始化。在类的内部只是声明,定义必须在类定义体的外部,通常在类的实现文件中初始化,如:double Account::Rate=2.25;static关键字只能用于类定义体内部的声明中,定义时不能标示为static

      在C++中,const成员变量也不能在类定义处初始化,只能通过构造函数初始化列表进行,并且必须有构造函数。

      const数据成员 只在某个对象生存期内是常量,而对于整个类而言却是可变的因为类可以创建多个对象,不同的对象其const数据成员的值可以不同。所以不能在类的声明中初始化const数据成员,因为类的对象没被创建时,编译器不知道const数据成员的值是什么。

      const数据成员的初始化只能在类的构造函数的初始化列表中进行。要想建立在整个类中都恒定的常量,应该用类中的枚举常量来实现,或者static cosnt。

       一个例子如下:

[cpp]  view plain copy
  1. class Test    
  2. {    
  3. public:    
  4.       Test():a(0){}    
  5.       enum {size1=100,size2=200};    
  6. private:    
  7.       const int a;//只能在构造函数初始化列表中初始化    
  8.        static int b;//在类的实现文件中定义并初始化    
  9.       const static int c;//与 static const int c;相同。    
  10. };    
  11.     
  12. int Test::b=0;//static成员变量不能在构造函数初始化列表中初始化,因为它不属于某个对象。    
  13. cosnt int Test::c=0;//注意:给静态成员变量赋值时,不需要加static修饰符。但要加cosnt   
靠,还是做错了。

腾讯笔试题目

[cpp]  view plain copy
  1. #include <iostream>  
  2. using namespace std;  
  3.   
  4. class A  
  5. {  
  6. public:  
  7.         int m_a;  
  8.         A():m_a(1){}  
  9.         virtual void f(){}  
  10. };  
  11.   
  12. class B:public A  
  13. {  
  14. public:  
  15.         int m_a;  
  16.         B():m_a(2){}  
  17.         virtual void f(){}  
  18. };  
  19.   
  20. int main()  
  21. {  
  22.         A *a=new B();  
  23.         B *b=dynamic_cast<B*>(a);  
  24.         cout<<b->m_a<<endl;  
  25.   
  26.         return 0;  
  27. }  

主要是引申dynamic_cast 和static_cast的区别,自己看看百科吧:

http://baike.baidu.com/view/1745213.htm。

dynamic_cast的类一定要加virtual函数。


FROM: http://blog.csdn.net/randyjiawenjie/article/details/6871660

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值