C ++ learning notes (From MOOC)
(MOOC video learning-C ++ departure study memo)
1: The default value of the function parameter, which can only be on the rightmost side. If there are three parameters and only one of them has a default value, this is incorrect.
Write the default value when declaring a function. Do not write the default value when defining the function. (Some compilers may encounter errors .)
2: inline function: Replace the call statement with the actual parameters and code of the function during compilation.
Inline
Inline functions are built by the compiler.
The logic is simple. We recommend that you use inline when calling frequently.
Recursive functions cannot be inline functions.
3: Memory application new memory release delete NOTE: When releasing an array, use delete [] p;
Application may fail,
Int * p = new int [100];
If (NULL = p ){
// Memory application failed
}
Note: after the memory is released, assign the corresponding pointer to NULL;
4 string aaa = "Hello" + "world! "; // This definition is invalid and can only be a constant + variable.
5: the functions defined in the class are first compiled into inline functions!
Bytes ------------------------------------------------------------------------------------
1: constructor initialization list
Class Student {
Public:
Student (): name ("Tom" "), age (33) {}// start with a colon. Separate multiple variables by commas.
Private:
Int age;
String name;
}
Note: The initialization list is executed before the constructor. It can only be used for constructors.
Constants can only be initialized using the initialization list.
2: copy the constructor. When an object is assigned to another object, the copy constructor is automatically called.
If no, a default copy constructor is automatically generated.
3. destructor ~ Class Name () to release memory.
Parameters are not allowed. Cannot be overloaded. No return value. If no, a default value is automatically generated.
Bytes ------------------------------------------------------------------------------------
When C ++ inherits, when the child class and the parent class have functions of the same name
That is to say, the subclass function hides the function of the parent class.
For example, the parent class is Person and the subclass is Solider.
The hidden function is play ()
Call the subclass function solider. play ();
Call solider. Person: play ();
Note: even if the play function of the subclass is added with parameters, the function of the parent class must be called in the following way.
If a parent class pointer is used to point to a subclass object, the destructor of the parent class should be defined as a virtual function. Virtual
If B and C inherit A, D inherit B, C
This is called Diamond inheritance. At this time, A must have only one copy in the memory.
B and C must use virtual inheritance. The following code is used:
Class B: virtual public {};
Bytes ------------------------------------------------------------------------------------
When C ++ inherits, when the child class and the parent class have functions of the same name
That is to say, the subclass function hides the function of the parent class.
For example, the parent class is Person and the subclass is Solider.
The hidden function is play ()
Call the subclass function solider. play ();
Call solider. Person: play ();
Note: even if the play function of the subclass is added with parameters, the function of the parent class must be called in the following way.
If a parent class pointer is used to point to a subclass object, the destructor of the parent class should be defined as a virtual function. Virtual
Abstract classes in C ++ are implemented through pure virtual functions.