Frequent type refers to the type of type modifier constant, and the usual type variable or object value cannot be updated. Therefore, initialization must be performed when defined or explaining the normal type. General constants and object constants 1. General constant general constants refers to a simple type of constant. This constant is defined, and the modifier Const can be used before the type specifier can be used before the type specifier. Such as: int const x = 2; or const INT x = 2; definition or description A constant group can be used in the following format:
#include
Const int N = 6;
Void Print (const INT * P, INT N);
void main ()
{
Int array [n]; for (int i = 0; i
CIN >> Array [I];
Print (array, n);
}
Void Print (const INT * P, INT N)
{
Cout << "{" << * P;
For (int i = 1; i
COUT << "," << * (p i);
COUT << "}" <
}
The Member Function is a member function that will be described using the const keyword, called a common member function. Only when the member function is eligible to operate constant or constant object, the member function that does not use the Const keyword cannot be used to operate the normal object. Member function description format is as follows:
#include
Class R
{
PUBLIC:
R (int R1, int R2) {r1 = r1; r2 = r2;}
Void print ();
Void Print () const;
Private:
Int R1, R2;
}
Void R :: Print ()
{
Cout <
<< "," <
<
}
Void R :: Print () Const
{
Cout <
<< ";" <
<
}
void main ()
{
R a (5, 4);
a.print ();
Const r b (20, 52);
B.Print ();
}
The output result of this example is: 5, 4 20; 52 The class declares two member functions, which are different (in fact, the heavy-duty member function). There is a member function with a Const modifier to handle constants, which also reflects the characteristics of the function overload. Often Data Members Type Modifier Const can also explain the member function, but also document data. Since the const type object must be initialized and cannot be updated, the Const data member can only be initialized by the member initialization list when the CONST data member is described in the class. The following example describes the use of a member initialization list to generate a constructor.
#include
Class A
{
PUBLIC:
A (INT I);
Void print ();
Const Int & r;
Private:
Const Int A;
Static const Int B;
}
Const Int A :: b = 10;
A :: A (INT I): A (i), R (a)
{
}
Void A :: Print ()
{
Cout <
<<: "<<<": "<
<
}
void main ()
{
A A1 (100), A2 (0);
A1.Print ();
A2.Print ();
}
The operation results of the program are: 100: 10: 100 0: 10: 0 In this program, the following three normal type data members are described: const INT & R; const Int a; static const Int b; where R is Chang INT type reference, A is a constant INT variable, B is a static INT type variable. Static data member B is initialized in the program. It is worth noting that the format of the constructor is shown below: A (INT I): A (i), R (a) {} where the colon is a data member initialization list, which contains two initializations, carry with commas Separated, because data members A and R are often type, need to be initialized. <<: "<<<": "<
<