Thinking In C
1. Difference to Private and Protect Keywords.
Subclasses can access the base class Protect member and cannot access the private member.
2. Friends, how Friend declares a friend.
3. Default constructor. When the class declares a configuration function with parameters without declating the parameter constructor, does the compiler also generate a default default constructor? Analyze the following error:
Class a {
PUBLIC:
A (int i) {
COUT << i << endl;
}
}
Void main () {
A a a;
}
4. Does the constructor have a return value? Is the destructor return value? Can the destructor can be used with parameters?
5. Explain the overload
Function overload Allows two or more functions to use the same name restriction condition is that their parameter tables must be different or different from different parameter types.
6. How is the overload function distinguished from each other?
7. Explain the default parameter function
8. Can the next example compile? why. The default parameter function of the overload cannot be generated with other functions.
Void S (INT I) {
COUT << i << endl;
}
Void S (INT I, INT J = 0) {
COUT << i << endl;
}
Void main () {
s (5);
}
9. Can I use the return value to distinguish the overload function?
10. Look at the following question:
a. Constant must be initialized, the following code is also:
Const Int i;
i = 20;
b. Is there a storage control in constant, or just the symbol of compile?
Not necessarily
c. Is a constant that determines its value when compiling?
Const Int i = 100;
Const Int J = i 100;
Long address = (long) & j; // Forced compiler to allocate storage space for constant
Char BUF [J 10];
Void main () {
Const char c = cin.get ();
Const char C2 = c-'a ' ' a ';
COUT << c << "" << C2 << Endl;
}
11. Look at the following question:
a. Can you assign a very amount of object to a constant pointer?
b. Can I assign a constant object to a very amount of pointer? If so, what should I do?
Void main () {
Const Int i = 5;
INT * J = const_cast
}
Const int * x; // constant pointer
INT * const x = & d; // Pointer constant
INT const * x; // constant pointer
Const Int * const x = & d; // constant pointer constant
12. Function call median delivery use constant definitions are meaningless:
Void F (const INT I);
But the constant definition of the return value of the function has special effects, see the case:
Class x {
INT I;
PUBLIC:
Void modify () {cout << "haha" << endl;}
}
Const x fun () {returnix x ();
Void main () {
//! fun (). modify (); constant cannot be left value, so the following example does not be established: //! const x g;
//! g.modify ();
// !!! Can constant can't be left value? See 16 questions
}
13. Typical application of constants:
Void U (const INT * P);
Const char * v ();
14. Analyze the cause of the following example:
Class x {};
X f () {returnx x ();
Void G1 (X &) {}
Void G2 (Const X &) {}
Void main () {
//! G1 (f ()); temporary variables are constants
G2 (f ());
}
15. How to use class constant members, how to initialize?
Class y {
PUBLIC:
CONST SIZE;
Y ();
}
Y :: y (): size (100) {}
Class x {
ENUM {i = 100};
}
//! Class X {
//! Const Int i;
//! X () {};
//!};
16. What role does a member function declared as a constant function?
a. Compiler guarantees that the function must not modify the value of the member variable
b. Allow a constant object to call this function
Class x {
PUBLIC:
Void f () const {
COUT << "Haha ..." << endl;
}
}
Void main () {
Const X CS;
CS.f ();
}
17. What is the role of the volatile key?
Volatile tells the compiler that Volatile Don't make any assumption and optimization of any self-satisfaction. For example, establish a const volatile object, tell the compiler programmer, do not change this object, but not, this object does not change, and tools from the outside may change it, such as port communication of hardware.
18. Inline functions and ordinary functions, macro differences?
Use the inline function, the compiler expands its code instead of inserting a jump. This improves the efficiency of function calls. The inline function is unlike a macro, it is a real function, with parameter type check.
Note: The member functions defined in the Class are inline functions.
19. Which two cases will give the compiler to discard inline?
a. Function title is too complex
b. Explicitly or implicit function addresses in the code
20. Special features of the preprocessor:
a. #define debug (x) cout << # x "=" << x << Endl
b. #define trace (x) cout << # x << Endl, x
c. #define file (x) char * x ## _ string
use:
INT f () {
Return 9;
}
Void main () {
Char i [] = "haha";
Debug (i);
For (int J = 0; j <5; j )
Trace (f ());
File (one) = i;
Cout << one_string << endl;
}
21. Is there any code to execute before or after the first line of code of the main function?
Class Obj {
CHAR CT;
PUBLIC:
Obj (char C) {
CT = C;
Cout << "OBJ :: Obj () for" << c << ENDL;
}
~ Obj () {
COUT << "Obj :: ~ obj () for" << CT << Endl;}
}
Obj a ('a');
Void f () {
Static Obj B ('b');
}
Void main () {
Cout << "Inside main ()" << endl;
f ();
Cout << "Leaving main ()" << endl;
}
OUTPUT:
Obj :: obj () FORA
INSIDE main ()
Obj :: obj () FORB
Leaving main ()
Obj :: ~ obj () forb
Obj :: ~ obj () FORA
22. STATIC keyword:
a. Static storage
b. Control visibility
Below are all variables defined globally
EXTERN INT A = 0; ===> int a = 0; (static)
EXTERN VOID F (); ===> Void f ();
Static int A = 0; ===> This file is visible
STATIC void f (); ===> This file is visible
23. Use of namespace:
Namespace Simon {
Class a {
PUBLIC:
Void print () {
COUT << "this is simon :: a" << endl;
}
}
CHAR STR [] = "this is simon :: str";
}
Void main () {
Simon :: a v;
v.print ();
Using Namespace Simon;
COUT << str << endl;
}
24. What is the difference between static member functions and ordinary member functions?
Static member functions can only access static member variables and static member functions. why? I want to think about the THIS pointer, the static member function is there is no THIS pointer.
25. Use a function in a C library in C to prevent the compiler from accessing the symbols in a C way to access the symbol to do not locate the function entity how to do?
EXTERN "C" float f (int A, float b);
C f () ===> _F_INT_FLOAT
C in f () ===> _f
26. How to change the pointing point of a pointer constant?
Void main () {
INT i (1);
INT J (2);
INT * Const P = & i;
INT * PP = (int *) & p;
* pp = (int) & j;
COUT << * P << endl;
}
27. Explain the reason for the following example error
Const Int III = 20;
INT & q = III; // error, very quoted references can not reference constants
INT & QQ = 20; // Error, very quantified references can not reference constants
Const Int & QQQ = III; / / correct
QQQ = 23; // Error, constant cannot be used as a left value
The reference must be linked to a legal storage space.
a. Quote must be initialized
b. References Once an object is pointing, you can no longer point to others.
c. No NULL can not be cited (unless a constant reference)
28. When using the reference to function parameters, its parameters must be very quantized, that is, the direct value or returned temporary variable Void f (INT &) {}
Void g (const INT &) {}
Void main () {
// f (1);
g (1);
}
29. What situation needs a copy constructor?
Need to pass when passing
Class X;
Void f (x x2);
X x1;
f (x1);
X x2 = x1;
30. Copy constructor form: x :: x (x &) {}
31. Try to avoid the value of the class, what method can prevent the user from passing?
As long as you declare a private copy constructor:
Class x {
X (x & x) {
Cout << "HEHE" << Endl;
}
PUBLIC:
X () {}
}
Void main () {
X xx;
//! X x2 = xx;
}
32. Define a function pointer to point to a class of non-static member functions, and call:
Class X;
Typedef void (x :: * fpt) ();
Class x {
PUBLIC:
INT XI;
FPT PP;
X (int J) {
Xi = j;
PP = x :: f;
(this -> * pp) ();
}
Void f () {
COUT << xi << endl;
}
}
Void main () {
X x1 (1), X2 (2);
FPT P1, P2;
P1 = x :: f;
P2 = x1.f;
(x2. * p1) ();
(x2. * p2) ();
}
Note that non-static members always use it with the THIS pointer, use an object to use it instead of a type. This relationship is emphasized even in the class definition.
33. How many copy constructor calls occurred in the next example? once
Class x {
PUBLIC:
X () {}
X (x &) {
COUT << "Haha ..." << endl;
}
}
Void main () {
X x1;
X x2 = x1;
X2 = x1;
}
34. Avoid using operators overload! ! !
35. Differences for the overloading of class members operators with global operators? Parameter number
36. On the inheritance chain, the constructor of the subclass needs to explicitly call the base class constructor, and what about the destructor? No need, they will be called automatically. grammar:
Foo :: Foo (INT I): Base_foo (i) {...}
37. What is the list of constructor initialization?
The call sequence of the constructor is: the constructor of the primary base class, and then the constructor of the member object, the constructive order is not affected by the order in the initial list. The initial list of the same class member is determined by its order in the class declaration. The sequence of sequence is opposite to the sequence of construction.
38. For example, why must I initialize a list?
Class z {
PUBLIC:
Z (int i) {
COUT << "huhu ..." << endl;
}
}
Class x {
PUBLIC:
X (INT I) {
Cout << "Hehe ..." << Endl;
}
}
Class Y: public x {
PUBLIC:
Z z1;
Y (INT I): X (I), Z1 (6) {
COUT << "Haha ..." << endl;
}
}
Void main () {
Y y (5);
}
Try to move the initialization column of the above example to the inside of the function body, what happens? 39. The function FOO has multiple overloads in the base class, and the subclass is defined in the Subclass, can I call the base class version of the Foo in the subclass?
No, subclass redefine will shield all base class versions.
Class x {
PUBLIC:
f (INT I, INT J) {COUT << "Haha" << endl;}
F (INT i) {cout << "haha" << endl;}
}
Class Y: public x {
PUBLIC:
F (INT I) {cout << "HEHE" << endl;}
}
Void main () {
Y y;
//! Y.F (3, 4);
}
40. Talk about the difference between public, private, protect keyword
41. Talk about the nature of private inheritance
42. How to open private integrated base classes in subclasses
Class x {
PUBLIC:
Void f () {cout << "haha" << endl;}
Void ff () {cout << "HEHE" << endl;
}
Class Y: x {
PUBLIC:
X :: f;
}
Void main () {
Y y;
Y.f ();
//! y.ff ();
}
43. Judging the output of the following example:
Class x {
PUBLIC:
Void foo () {cout << "x :: foo" << Endl;
}
Class Y: public x {
PUBLIC:
Void foo () {cout << "Y :: foo" << Endl;}
}
Void Bar (x * p) {
P-> foo ();
}
Void Barbar (X & X) {
X.foo ();
}
Void main () {
Y y;
X * P;
X & M = Y;
P = & y;
P-> foo ();
BAR (P);
Barbar (M);
}
What do you want to pass by P call Y :: foo ()? Using Virtual to achieve polymorphism
44. Bundle. How is the compiler how to do this?
a. Create a table (vTable) for each class containing virtual functions
b. Place a virtual function address in this table
c. Place a pointer for each class of virtual functions: VPTR, pointing the response vTable
45. Class no_virtual {
Int a;
Void f () {};
}
Class one_virtual {
Int a;
Virtual void f () {};
}
Class ONLY_VIRTUAL {
Virtual void f () {};
}
Class two_virtual {
Virtual void f () {};
Virtual void g () {};
}
Class NULL {};
Void main () {
COUT << Sizeof (no_virtual)
<< Sizeof (One_Virtual)
<< Sizeof (ONLY_VIRTUAL)
<< SIZEOF (Two_Virtual)
<< Sizeof (null)
<< ENDL;
}
Output: 48441 Description Why is this result?
What is the location relationship between VPTR and INT A in one_virtual?
46. Each class with virtual functions has airs, drawing the vTable relationship 47 in the inheritance chain.
INSTRUMENT * i;
BRASS B;
I = & b;
I-> Adjust (1);
Write the assembly code of the last line:
28: Instrument * pi;
29: BRASS B;
004010A
8D 4D F8 Lea ECX, [EBP-8]
004010AB E8 73 FF FF FF CALL @ ilt 30 (Brass :: BRASS) (00401023)
30: pi = & b;
004010B0 8D
45 f
8 Lea Eax, [EBP-8]
004010B3 89 45 FC MOV DWORD PTR [EBP-4], EAX
31: Pi-> Adjust (1);
004010B6 8B F4 MOV ESI, ESP
004010B8
6A
01 Push 1
004010BA 8B 4D FC MOV ECX, DWORD PTR [EBP-4]; THIS pointer
004010BD 8B 11 MOV EDX, DWORD PTR [ECX]; VPTR pointer
004010BF 8B 4D FC MOV ECX, DWORD PTR [EBP-4]; parameters
004010C
2 FF 52 08 Call DWORD PTR [EDX 8];
The call to non-static member functions involves passing the THIS pointer to the function.
48. What is an abstract base class, a pure virtual function?
Can an abstract base class can have an example? Cannot
Can a pure virtual function can be implemented? can
Class x {
PUBLIC:
Virtual void f () = 0 {
COUT << "Haha" << endl;
}
}
49. Increase the new virtual function in the derived class, can not call the pointer of the base class, why? Please explain from the perspective of VTPR with VTABLE.
If it is exactly the exact understanding of the base class pointer points to the derived class object, you need to call this function. RTTI
Class x {
PUBLIC:
Virtual void f () {cout << "haha" << endl;}
}
Class Y: public x {
PUBLIC:
Virtual void f () {cout << "HEHE" << endl;}
Virtual void ff () {cout << "hehehehe" << endl;}
}
Void main () {
X * P;
Y y;
P = & y;
Y * py = null;
PY = static_cast
IF (py) py-> ff ();
}
50. Look at the example below, talk about why avoid value delivery:
Class x {
INT XI;
PUBLIC:
X (INT I) {xi = i;}
Virtual int f () {return xi;}
}
Class Y: public x {int yj;
PUBLIC:
Y (INT I, INT J): X (i), yj (j) {}
Virtual int f () {return x :: f () yj;}
}
Void call (x b) {cout << b.f () << endl;}
Void main () {
Y A (5, 8);
Call (a);
}
OUTPUT: 5
The class object has occurred "object slice" when performing value transmission, whether the default copy constructor is manually provided, and the compiler is initialized in it. Here, the parameter type of Call declares the base class, then the copy constructor of the base class is called, pointing the VTABLE of the object to the base class (originally pointing to VTABLE). So the error happened.
Memolive, what method can force the programmer to give up the value? Private copy constructor
51. Whether it is a manual constructor or a default constructor, whether the copy constructor is still a general constructor, the compiler is in which a piece of code is used to initialize VPTR (of course, the constructor of the type of virtual function)
52. What are the general code of the compiler insert into the constructor?
a. Initializing VPTR (for class with virtual functions)
b. Check the THIS pointer (because the object may have no construction success, Operator New)
c. Call the constructor of the base class (if there is no explicit call base class in the initialization parameter list of the derived class, the compiler will automatically call the non-parameter constructor of the base class (default or manual) even The derived class constructor is called the base class constructor inside the function body. If the base class does not have a parameter constructor (for example, it is only manually providing the constructor of the parameter), compiling an error)
Class x {
PUBLIC:
X () {cout << "x :: x ()" << endl;}
X (INT i) {cout << "x :: x (int)" << endl;}
}
Class Y: public x {
PUBLIC:
Y (INT I) {x (5);
}
Void main () {
Y y (6);
}
Output: x :: x ()
X :: x (int)
53. Why can't the following case cannot be compiled?
Class x {
PUBLIC:
X (INT i) {cout << "x :: x (int)" << endl;}
}
Class Y: public x {
PUBLIC:
}
Void main () {
Y y;
}
How can I pass?
54. If the derived class is changed to:
Class Y: public x {
PUBLIC:
Y () {}
}
Why can't you compile? How to modify?
Rules: You should always call the base class constructor in the initialization list
55. If the virtual function is called in the constructor, only the local version of the virtual function is called.
Class x {
PUBLIC:
Virtual void f () {cout << "x :: f" << endl;}
X () {f ();
}
Class Y: public x {
PUBLIC:
Virtual void f () {cout << "Y :: f" << endl;}
Y () {f ();
}
Class z: public y {
PUBLIC:
Virtual void f () {cout << "z :: f" << end1;} z () {f ();
}
Void main () {
Z z;
}
Output: x :: f
Y :: f
Z :: F
When the Z's constructor is called, the compiler will call the X constructor, then Y, because the constructor points to the VPTR of the object to the type of VTABLE, so that when constructs to the X-X-constructor, VPTR is pointing X of the VTable, so the called function is the function of the X version.
56. Can constructor can't be a virtual function? No. But what will happen when it is derived with a base class pointer? How to solve? Let the patriator function is virtual function
Rule: As long as you use polymorphism, you should make the destructor function to be a virtual function.
The destructor does not need to explicitly call the analytical function of the base class like constructor, and the compiler will automatically call the constructor of the base class.
57. Discussion, if you call the virtual function in the destructor, you should call which version of the virtual function? Local version. Only local version will only process local data, and will not deal with a derived class data that may be "destructure".
Therefore, the evening bundled mechanism is ignored in the destructor.
58. Syntax of the template
Class template:
Template
Class array {
T at [100];
PUBLIC:
Array () {for (INT i = 0; i <100; i ) AT [i] = (t) i;}
T getat (int index);
}
Template
T Array
Void main () {
Array
COUT << Darray.getat (8) << endl;
}
Constant template:
Template
Class array {
Double at [size];
PUBLIC:
Array () {for (int i = 0; i Double getat (int index); } Template Double Array Void main () { Array <10> Darray; COUT << Darray.getat (8) << endl; Array <> dsarray; Cout << DSARRAY.GETAT (99) << ENDL; } Function template: Template T TgetMem (t * pt, int index) {return pt [index]; Void main () { Double DD [100]; For (int i = 0; i <100; i ) DD [i] = i; COUT << TgetMem (DD, 5) << endl; } 59. Multiple inheritance. Should be avoided. When a diamond inheritance occurs in multiple integration: this happens: What is the entity containing two Base classes in the successor? Virtual inheritance Class base {}; Class D1: Virtual base {}; Class D2: Virtual base {}; Class MI: Public D1, Public D2 {}; 60. RTTI. Generally we shoot a pointer to a base class pointer, then use the virtual function using the base class interface. But occasionally we need to know the exact type of a base class pointer to the object, so there is RTTI. Note that RTTI relies on the type information that resides in the virtual function table, so it will be erroneous with RTTI on a class without virtual functions. 61. Two uses of RTTI: a. Get the name of the type, or the comparison type, the type of judgment. TYPEID, such as S is a shape * ===> typeid (* s) .Name () b. Down security map Shape * sp = new circle; Circle * CP = Dynamic_cast IF (cp) cout << "Cast Successed" << ENDL; 62. Note: The object itself is incoming in TypeID, and for the pointer to go, for reference directly, reference, such as: Class X; Class Y: public x; X * xp = new y; TYPEID (* XP) == TypeId (Y); TYPEID (XP) == TypeId (x *); 63. Note that the Void pointer does not have a type, so the RTTI is useless for the Void pointer. 64. RTTI allows us to discover type information with a polymorphic pointer, but use virtual functions as much as possible instead of RTTI. 65. Explain the mechanism of RTTI. RTTI is implemented by placing an additional pointer in vTable. This pointer points to a TypeInfo structure, TypeId () is actually TypeInfo information through VPTR. 66. The exception of C is not a matter of work with Windows structured abnormal she.