C ++ learning Summary

Source: Internet
Author: User

First, talk about const
I have summarized them in three aspects: 1. Const constant; 2. Const pointer; 3. Application of const in function declaration.
1, const constant
Its definition form is
Const type_name val_name;
Note the initialization problem. Initialization is required when defining common constants. If data members in a class cannot be initialized directly in the class, you need to initialize the class in the class constructor initialization list.
It also needs to be noted that it is different from macro definition. Const constants have data types, while macro constants have no data types. The compiler can perform type security checks on the former, the latter is simply a replacement of characters.
2. Const modifier pointer
If the const is on the left side of the asterisk, the const is used to modify the content pointed to by the pointer, that is, the pointer points to a constant. If the const is on the right side of the asterisk, the const is used to modify the pointer, the pointer point cannot be changed. Therefore, you must initialize the pointer while defining it.
3. Application of const in function declaration
In a function declaration, const can modify the return value of a function or a function parameter. For a member function, it can also modify the entire function. Such a function is a common member function,
Declare the regular member function in the form of type_name function_name () const;
Note that the following const qualifier must be included when defining a common member function outside the class. (It is mainly different from static member functions)
Modifying function parameters can prevent accidental modification and enhance Program Long member functions are not allowed to modify other data members in the class, but also enhance the robustness of the program.
Function overload
There are several basis for function Overloading
The parameter type, the number of parameters, and the order of parameters are also functions with the const qualifier added.
When matching, first strictly match, and then convert compatible types such as Char to int. Finally, you can customize the type conversion,
Default function parameters
The default function parameter is set from the right end, and the matching starts from the left end.
Next let's talk about classes.
There is a lot of content about the class,
The difference between a class and a struct is that the Members in the class are private by default, while the Members in the struct are public by default. functions can be defined in the class but cannot be defined in the struct. In the words of our teacher, struct is a type of structured data space, and classes not only describe the data space but also the custom type of its operations.
Constructor
Its declarative form is
Class_name ();
Here, we need to talk about the execution sequence of the constructor when instantiating an object of A derived class during inheritance. First, we need to activate the constructor of the base class, after completing the memory space allocation for the base class members and then executing the constructor function body, the teacher gives a better understanding of the explanation: "allocating space is equivalent to building a house, the function body of the constructor is decorated. Then, the space of the member of the derived class is allocated, and the function body of the constructor of the derived class is executed. If there is no custom constructor, the system automatically generates the default non-argument constructor. If you define the constructor yourself, the system will not kick the default constructor. Let's talk about the initialization list of the constructor. The initialization list is required when the class contains constant members and referenced members. Another situation is that during inheritance, if the base class has no constructor and the parameter constructor has no default parameters, the constructor of the derived class must initialize the base class in the initialization list.
Destructor
Its declarative form is
~ Class_name ();
The role of the Destructor is to release the applied memory space. If there is no custom one, the system will provide the default destructor. However, it should be noted that the Destructor cannot release the heap space applied by new. This space needs to be released by the programmer. If it is not released, it may cause memory leakage. Another point is that during inheritance, the destructor of the base class should be declared as a virtual function as much as possible. The reason is that if a base class Pointer Points to a derived class, when deleting it, it will call the destructor of the base class instead of the destructor of the derived class, resulting in Memory leakage.
Copy constructor
Let's talk about the concept of copy construction. Copy construction creates an object based on other objects when creating an object. We call this creation activity a copy structure, which is the same, if no custom copy constructor is available, the system provides the default copy constructor. By default, the copy paparazzi function is a kind of shortest copy function. When the class contains pointer members, by default, the copy constructor simply copies the pointer value. In this way, the same memory area is released twice during the analysis, causing an exception in the program. Therefore, you need to customize the copy constructor. Such a copy is also called a deep copy,
The Declaration Form of the copy constructor is
Class_name (conat class_name & parameter_name );
Of course, the parameter can also be class_name * parameter_name;
Conversion Constructor
The conversion constructor is used for type conversion. In this case, there is a Class A and a class B data. Now we need to convert the type B data to, define a conversion constructor A (const B & parameter_name) like this in class );
Speaking of this, I want to talk about operator custom implicit conversion.
Its declarative form is like this
Operator type_name (); the difference is that this class type is converted to type_name type. In contrast to the one above, it is worth noting that there is a return value, and the return value type is the type to be converted.
Value assignment operator
Or, if there is no custom value assignment operator, the system will provide the default value assignment operator, but if you need to make a deep copy between objects, you need a custom value assignment operator,
The Declaration Form of the value assignment operator is
Class_name & operator = (class_name & parameter_name );
Here, we declare a reference function that returns the class_name type to the left value when assigning values.
Note that the defined function body requires a self-assigned value check. The reason is that the defined assignment operator is that the assigned object already exists, you need to release the space of the assigned object and re-allocate the resource. If you assign a value to yourself, the object to be assigned will be destroyed, assignment cannot be completed,
Static Member
A static member is constructed before the main function. its lifecycle is equivalent to a global variable. There are two points to note: one is the initialization of the static data member, which needs to be initialized outside the class,
The format is type_name class_name: static_data_name = value;
The second is the problem of static member functions. When it is defined outside the class, there is no need to add the static keyword, and there is no default this pointer inside it, therefore, the static member function cannot be a non-static data member in the member class. The call form is class_name: function_name (); similar to a global function, except that the scope of the class is added.

Let's talk about inheritance now
First, there are three inheritance Methods: public, private, and protected. Under the three inheritance methods, the permission of the derived class to inherit members in the base class changes
Under Public inheritance
The derived class can directly access the public and protected members of the base class. However, when the external class is used, protected does not allow direct access from the outside. It must be noted that, the pointer of the base class type can point to the derived class in the public inheritance mode. In this case, any object of the derived class can be replaced by the base class, but it is not possible in the other two ways.
Protected inheritance
The public and protected members of the base class are considered as protected members outside the class of the derived class.
Private inheritance
Both public and protected of the base class are considered as private members outside the class of the derived class.
In the three inheritance modes, the Private Members of the base class are considered as private members outside the class of the derived class.
Multi-inheritance means that a class is derived from multiple base classes at the same time.
Assume that there is a base Class, Class A inherits the base class, and Class B also inherits the base class, class C inherits the data of Class A and Class B at the same time, so that class C has two copies of the data of class base, which will lead to the unknown access to which the data is actually accessed, therefore, the concept of virtual inheritance is introduced, so that Class A and Class B can virtualize the base class, in this way, when class C inherits both Class A and Class B, it will not generate two copies of the base class data, which leads to the concept of virtual inheritance.
Polymorphism
First, if both the base class and the derived class contain the same member function, if a base class Pointer Points to the derived class under the premise of Public inheritance, when calling this member function, the member function of the base class is called, and we hope that it can call the corresponding type of function based on the type of transmitted parameters to achieve a kind of polymorphism. In this way, we need to introduce the concept of virtual functions,
Its definition form is virtual type_bame function_name (parameter );
The method to implement polymorphism is to declare the function in the base class as a virtual function. The corresponding function in the derived class does not need to be declared as a virtual function. Specifically, a virtual function table is maintained when the program is running, I am not very clear about how it works. In short, the functions we want are implemented in this way. Here we add that the polymorphism of C ++ language is divided into the polymorphism at compilation and the polymorphism at runtime, polymorphism during compilation is implemented through function overloading and templates, while Runtime polymorphism is implemented through virtual functions.
Abstract class
The definition of an abstract class is that there must be at least one class of pure virtual function in this class. Note that abstract classes cannot be directly instantiated, but can be indirectly instantiated, instantiate a derived class that is not an abstract class to call the constructor of the abstract class implicitly. This also indicates that the constructor of the abstract class is necessary.
One additional note is the concept of pure virtual functions.
The format is
Virtual type_name function_name () = 0;
You need to redefine the function in the derived class. If the derived class does not define it, the derived class continues to be an abstract class.

Finally, let's talk about exception handling.
It mainly talks about throes, try, catch
The Catch Block makes sense only when the throw statement is used in the statement segment of the try program. No matter which level of function is directly or indirectly called by the try program segment, the throw statement is executed, will be caught, and once an exception is thrown (execute the throw Statement), the next executed statement is the catch statement that captures the exception, all the intermediate-level functions, and its Defined variables are all pushed out of the stack.
Five Aspects of the catch statement block,
1. Provide the correct working information and save data that may be damaged before exiting the program.
2. handle exceptions based on throw statements.
3. Try to convert an exception that cannot be processed into a form that can be processed by another exception handler and throw the exception.
4. Run the initial statement again.
5. If you need to return a valid value to the main function.

1. Catch is often followed by try, and the number is unlimited.
2. The data types contained in catch are the same as those of functions.
3. If the catch matching the exception cannot be found later, the system calls the abort () function by default to terminate the program.
4. After the catch is executed, the program will executeCode
Note that the catch type matching is very strict. For example, if the char type is thrown, the int type cannot be caught.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.