原创作者: grantren
阅读:1049次
评论:0条
更新时间:2011-05-26
1.分析以下代码的执行结果
- #define macro1(a) #a
- #define macro2(a,b) a##b
- int x = 3;
- int y = 4;
- int xy = 10;
- cout << macro1(xy) << endl;
- cout << macro2(x,y) << endl;
2.下面的代中包含一些错误,请找出改正并重写这个类.
- class Complex {
- public:
- Complex(double real, double imaginary = 0):_real(real), _imaginary(imaginary) {
- }
- void operator+(Complex other) {
- _real = _real + other._real;
- _imaginary = _imaginary + other._imaginary;
- }
- void operator<<(ostream os) {
- os << "(" << _real << "," << _imaginary << ")";
- }
- Complex operator++() {
- ++ _real;
- return *this;
- }
- Complex operator++(int) {
- Complex temp = *this;
- ++ _real;
- return temp;
- }
- private:
- double _real, _imaginary;
- };
3.指出以下代码中的错误
- std::vector<int> vec;
- //...
- const std::vector<int>::iterator iter = vec.begin();
- *iter = 10;
- ++iter;
- std::vector<int>::const_iterator cIter = vec.begin();
- *cIter = 10;
- ++cIter;
4 设计一个类,使这个类无法被其他类继承.
5 找出以下代码中的错误.
- class B {
- public:
- virtual ~B();
- void operator delete (void*, size t) throw();
- void operator delete[](void*, size t) throw();
- void f(void*, size_t) throw();
- };
- class D : public B {
- public:
- void operator delete (void*) throw();
- void operator delete[](void*) throw();
- };
- //...
- typedef void (B::*PMF)(void*, size_t);
- PMF p1 = &B::f;
- PMF p2 = &B::operator delete;
6.请写出以下代码的执行结果并说明为什么.
- #include
- using namespace std;
- class B {
- public:
- int f(int i) { cout << "f(int): "; return i+1; }
- // ...
- };
- class D : public B {
- public:
- double f(double d) { cout << "f(double): "; return d+1.3; }
- // ...
- };
- int main()
- {
- D* pd = new D;
- cout << pd->f(2) << '\n';
- cout << pd->f(2.3) << '\n';
- }
7. 在C++中, 用户可以直接调用构造函数和析构函数吗? 为什么?
8. 请改正下列代码
- #include
- using namespace std;
- template <class T>
- class Father {
- protected:
- T value;
- };
- template <class T>
- class Son : public Father
- {
- public:
- Son(T v) {value = v; cout << value;} #这一行不能通过编译,请改正
- };
- int main() {
- Son<int> a(343);
- return 0;
- }
9. 为何空类的大小不是零?
10. 请写出以下代码的执行结果并说明为什么.
- #include
- class A {
- public:
- void foo(){std::cout << "demo" << std::endl;}
- };
- int main(void) {
- static_cast(0)->foo();
- return 0;
- }
11. 一个类中可以存在多于一个的拷贝构造函数吗?
评论 共 0 条 请登录后发表评论