【本文链接】
http://www.cnblogs.com/hellogiser/p/100-interview-questions-of-cplusplus-basics-21-30.html
【题目21】
运行下面的代码,输出结果?
【代码】
C++ Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
/* version: 1.0 author: hellogiser blog: http://www.cnblogs.com/hellogiser date: 2014/9/22 */ #include "stdafx.h" class A class B: public A int main() /* |
【分析】
虚函数动态绑定,但是缺省实参是编译时确定的,所以结果为B::Fun with number 10
【题目22】
指出下面的程序有哪些错误,并改正。
【代码】
C++ Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
#include <iostream> using namespace std; class A int i = 0; // ERROR A::A() } A::~A() } static void fun() // ERROR } |
【正确代码】
C++ Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
/* version: 1.0 author: hellogiser blog: http://www.cnblogs.com/hellogiser date: 2014/9/22 */ #include "stdafx.h" class A int i ; // error int A::j = 0; A::A(): i(0), k(0) } A::~A() } void A::fun() } int main() |
【题目23】