"This article links"
Http://www.cnblogs.com/hellogiser/p/100-interview-questions-of-cplusplus-basics-21-30.html
"topic 21"
Run the following code to output the results?
Code
C + + Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21st 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" #include<iostream> using namespacestd;
classA { Public: Virtual voidFun (intNumber =Ten) { Std::cout <<"A::fun with Number"<< number << Endl; } };
classB: PublicA { Public: Virtual voidFun (intNumber = -) { Std::cout <<"B::fun with Number"<< number << Endl; } };
intMain () { b b; A &a = b; A.fun (); return 0; //virtual function Dynamic binding: B, the default argument is determined at compile time ... is 10 }
/* B::fun with Number 10 */ |
Analysis
Virtual functions are dynamically bound, but the default arguments are determined at compile time, so the result is b::fun with Number 10
"topic 22"
Indicate what is wrong with the following program and correct it.
Code
C + + Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21st 22 23 24 25 26 27 28 29 30 |
|
#include<iostream> using namespaceStd
classA { Public: A (); ~a ();
inti =0; //ERROR Static intj =0; //ERROR Const intk =0; //ERROR Const Static Char*p ="Hello World"; //ERROR Static voidFun (); };
A::a () {
}
A::~a () {
}
Static voidFun ()ERROR {
} |
"Correct code"
C + + Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21st 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" #include<iostream> using namespacestd;
classA { Public: A (); ~a ();
inti;//Error Static intJ;//Error Const intK; //Error Const Static Char*p;//Error Static voidFun (); };
inta::j =0; Const Char*a::p ="Hello World";
A::a (): I (0), K (0) {
}
A::~a () {
}
voidA::fun () {
}
intMain () { return 0; } |
"topic 23"
C + + Fundamentals interview Selection 100 series (21-30) [C + + Basics]