C++ Questions

1.分析以下代码的执行结果

  1. #define macro1(a)    #a   
  2. #define macro2(a,b)  a##b   
  3.   
  4. int x = 3;   
  5. int y = 4;   
  6. int xy = 10;   
  7. cout << macro1(xy) << endl;   
  8. cout << macro2(x,y) << endl;   
  9.   

 

2.下面的代中包含一些错误,请找出改正并重写这个类.

  1. class Complex {   
  2. public:   
  3.     Complex(double real, double imaginary = 0):_real(real), _imaginary(imaginary) {   
  4.     }   
  5.        
  6.     void operator+(Complex other) {   
  7.         _real = _real + other._real;   
  8.         _imaginary = _imaginary + other._imaginary;   
  9.     }   
  10.        
  11.     void operator<<(ostream os) {   
  12.         os << "(" << _real << "," << _imaginary << ")";   
  13.     }   
  14.   
  15.     Complex operator++() {   
  16.         ++ _real;   
  17.         return *this;   
  18.     }   
  19.        
  20.     Complex operator++(int) {   
  21.         Complex temp = *this;   
  22.         ++ _real;   
  23.         return temp;   
  24.     }   
  25.        
  26.     private:   
  27.         double _real, _imaginary;   
  28. };  

3.指出以下代码中的错误

  1. std::vector<int> vec;   
  2. //...   
  3. const std::vector<int>::iterator iter = vec.begin();   
  4. *iter = 10;   
  5. ++iter;   
  6.   
  7. std::vector<int>::const_iterator cIter = vec.begin();   
  8. *cIter = 10;   
  9. ++cIter;  

 4 设计一个类,使这个类无法被其他类继承.

 5 找出以下代码中的错误.

  1. class B {   
  2. public:   
  3.  virtual ~B();   
  4.  void operator delete  (void*, size t) throw();   
  5.  void operator delete[](void*, size t) throw();   
  6.  void f(void*, size_tthrow();   
  7. };   
  8.   
  9. class D : public B {   
  10. public:   
  11.  void operator delete  (void*) throw();   
  12.  void operator delete[](void*) throw();   
  13. };   
  14.   
  15. //...   
  16.   
  17. typedef void (B::*PMF)(void*, size_t);   
  18. PMF p1 = &B::f;   
  19. PMF p2 = &B::operator delete;   

6.请写出以下代码的执行结果并说明为什么.

  1. #include<iostream></iostream>   
  2.  using namespace std;   
  3.   
  4.  class B {   
  5.  public:   
  6.   int f(int i) { cout << "f(int): "return i+1; }   
  7.   // ...   
  8.  };   
  9.   
  10.  class D : public B {   
  11.  public:   
  12.   double f(double d) { cout << "f(double): "return d+1.3; }   
  13.   // ...   
  14.  };   
  15.   
  16.  int main()   
  17.  {   
  18.   D* pd = new D;   
  19.   
  20.   cout << pd->f(2) << '\n';   
  21.   cout << pd->f(2.3) << '\n';   
  22.  }   


7. 在C++中, 用户可以直接调用构造函数和析构函数吗? 为什么?

8. 请改正下列代码

  1. #include <iostream></iostream>   
  2.  using namespace std;   
  3.   
  4.  template <class T>   
  5.  class Father {   
  6.   protected:   
  7.    T value;   
  8.  };   
  9.     
  10.  template <class T>   
  11.  class Son : public Father<t></t>   
  12.  {   
  13.   public:   
  14.    Son(T v) {value = v; cout << value;} #这一行不能通过编译,请改正   
  15.  };   
  16.     
  17.  int main() {   
  18.    Son<int> a(343);   
  19.    return 0;   
  20.  }   
  21.   


 9. 为何空类的大小不是零?

 

10. 请写出以下代码的执行结果并说明为什么.

  1. #include  <iostream></iostream>   
  2. class A {       
  3. public:        
  4.   void foo(){std::cout << "demo" << std::endl;}   
  5. };       
  6.       
  7. int main(void) {       
  8.   static_cast(0)->foo();       
  9.   return 0;       
  10. }      

 

11. 一个类中可以存在多于一个的拷贝构造函数吗?

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值