Static Data Member: That is to say, all objects of this class share this data member. If you change the value of a static member of an object, all the objects will change. For example:
Class Clock {
Int Hour, Minute;
Static int Second; // defines a static member.
}
// When calling a function:
Clock c1, c2, c3;
// The three entities share the Second data, and the change is equal to the full change.
// When calling directly:
Clock: Second
Static function Member: It is generally used to access static data members, that is, it can be accessed without defining entities. In general, function members can only access objects. If you want to use static functions to access non-static data, you must use parameters, which is troublesome and not recommended.
Class Clock {
Public:
Static int getSecond (); // defines a static function member.
Private:
Static int Second;
}
// When calling, you can directly access:
Clock: getSecond ();
Youyuan function:When defining a class, we define an objective object, such as a person or a car, but there are some abstract objects, such as relatives, which have no entity, and it is difficult to form a class. A friend function exists to define this relationship. For example, if a table is defined before, but there is a time difference between different time zones, we need to calculate the time difference between the two entity tables. We cannot create a class to do this, but we need to calculate it again, the two tables are involved. The classes placed in Clock seem a little nondescribable. so, just add one before the function.FriendThis function is defined outside the class to indicate the relationship between classes.
Class Clock {
Int Hour, Minute, Second;
Public:
Friend int timeLag (Clock & c1, Clock & c2 );
// Other functions are omitted
}
// When calling, you do not need to apply the entity:
Clock c1 (1, 1 );
Clock c2 (2, 2 );
Int lag = timeLag (c1, c2 );
Youyuan class: This is quite tangled. For example, I defined A friend Class A in Class B. That is to say, Class B is A trusted Class A, and Class A is arrogant at this time, it can protect the private and protected members of Class B. At the same time, as A little feedback I trust you (Oh, It's not Sima Yi's 3_3), all functions of Class A are membership functions of Class B. (Really tangled Ah =)
Three points worth attention: 1. The relationship between friends and friends cannot be transmitted. It can be expressed as follows: A = B = C but! = C. 2. The relationship between friends and friends is one-way. 3. The relationship between friends and friends is not inherited.
Class B {
Private:
....
Friend class A; // in this case, class A is A friend class of class B.
....
}
Regular reference: A const is added before the parameter type of the function. Therefore, this parameter cannot be updated and what is used.
Regular object: If a const is added before the object name, the object must be initialized and cannot be updated.
Void display (const char & ){
Cout <a <endl;
}
// You can only assign a value once for reference.
Int const pie= 3.14;
Pie = 2; // nonono you can't
Common member functions: After the function declaration, add a const before the semicolon. This function becomes a common member function.Only common member functions can be called.,The data member of the class cannot be updated.If an object is a common object, this object can only call its common member functions. At the same time, it can be used for heavy load differentiation. For example:
Class {
Private:
Int a, B;
Public:
Void func ();
Void func () const; // This is a common member function.
... // Other functions are omitted
}
// When writing a function, you must specify the following for a frequently-member function:
Void A: func () const {// const is indispensable, otherwise it becomes the first function
.......
}
// Call time:
A test1 (2, 2 );
Const A test2 (5, 5 );
Test1.func (); // call void func ();
Test2.func (); // call void func () const;
Regular data member: If const is added to a data member, no function can assign a value to the member. It can only be initialized by the constructor.
Several pre-compiled keywords:
# IncludeThis is generally divided into two formats: <File Name> "file name", the former is a standard search, and the file is in the include subdirectory; the latter is first searched in the current directory, no standard search is performed. Therefore, double quotation marks are used in the same directory, and files outside are used <>.
# DefineDefine a symbolic constant such as # define PI 3.14, but it would be better to use const int Pi = 3.14.
# UNDEFDelete the macro defined by # define. After that, # The macro defined by define is discarded.
# IfConditional compilation command, that is, to participate in compilation only when certain conditions are met. Compilation can generate different target code under different conditions .. I don't think it is very common. Is it used for debugging? I have never used it as a common coder ~