These my learning notes about the C++ language

1. What are the types of variables?

1. Auto storage type: memory space is allocated in stack heap mode, which belongs to real-time storage. Its storage space can be overwritten by several variables for multiple times.

Register Storage type: Stored in a general register

Externa storage type: refers to both functions and program segments

4. Static storage type: It is stored at a fixed address in memory and is valid for the entire duration of the program.

2. What is an inline function and what are its characteristics?

A function that uses the keyword inline is called an inline function. The compiler uses the function body to replace the function during compilation, saving the overhead of parameter passing and control transfer. The inline function body cannot contain loop statements or switch statements. An exception interface declaration cannot be made for inline functions.

3. When calling an overloaded function, what distinguishes which function is called?

Overloaded functions have the same function name, but they have different numbers and types of arguments. The compiler automatically determines which function to call based on the best match between the type and number of arguments and parameters.

Xiaobian recommend a learning C language /C++ learning skirt [one hundred and five, three hundred and two, ninety-eight sixty-nine], whether you are cattle or small white, is want to change or want to get into the industry can come to understand progress together to learn! There are development tools, lots of dry goods and technical information to share!

What is the difference between members of public type and members of private type?

Members of public types are declared with the public keyword. Common types define the external interface of the class. Private type members with the private keyword declaration, only allow the function members of the class to access, and any access outside the class is illegal, so, private members on the whole hidden in the class, in the class can not be seen outside the effective control of access rights. Both data members and member functions can be public or private, but data members are best declared private.

5. What is the protected keyword for?

Protected is used to declare members of protected types. Protected types are similar in nature to private types, except that member functions of derived classes can access protected members of the base class during inheritance and derivation.

What do constructors and destructors do?

The function of the constructor is to use a specific value to construct the object when the object is created. The object initializes a specific state and makes the object have the characteristics different from the object. What is completed is a process from general to specific. Destructors and constructor function almost the opposite, it is used to complete the object was to pull out some cleanup work before, in general, the destructor is in the life cycle of an object is coming to an end moment by the system automatically calls, it after the completion of the call object is lost, the corresponding memory space is released.

What is a copy constructor?

The copy constructor is a special constructor that takes a reference to an object of its own class. It uses an existing object to initialize a new object of the same class. This is called when an object of the class is used to initialize another object of the class; If the parameter of a function is a class object, the combination of parameters and arguments of the function is called. If the return value of the function is a class object, the function call completes when it returns.

What is the difference between static data members and ordinary data members?

A regular data member is a concrete object of a class and is allocated memory only when the object is created. Static data members, on the other hand, belong to the entire class. Static data member variables of a class exist even if no object is created. Because the existence of a static data member of a class does not depend on the existence of any class object, the static data member of a class should be displayed in the code initialization, always outside the class. Static members of an externally accessible class can only be accessed by the class name. The static member functions of a class cannot access the ordinary data members directly (they can be accessed indirectly by the object name), whereas any member function of a class can access the static data members of a class. Static members have the same access levels as ordinary members of a class: public, protected, and private. Static members can also take return values, const modifiers, and so on.

On static member functions

Static member function addresses can be stored by ordinary function Pointers, whereas ordinary member function addresses need to be stored by class member function Pointers. Static member functions may not call non-static members of a class. Because static member functions don’t have this pointer. Static member functions cannot be declared virtual, const, or volatile at the same time.

Friend functions and friend classes

Friends are a mechanism for sharing data between different classes and between classes and functions outside of classes. Using the friend keyword declaration, friend functions can access protected and private members of the corresponding class. A friend class whose member functions are all friends of the corresponding class. Friends are not commutative, transitive, and cannot be inherited.

11. What do operators * and & do?

* is called the pointer operator, is a unary operator, represents the value of the object to which the pointer points; The & is called the address-fetch operator, which is also a unary operator used to get the address of an object.

Personally, I think they are a pair of opposite operators.

12. References and Pointers

References are aliases to another variable, so references do not allocate memory. Declaration method :(type identifier & reference name = target variable name; (e.g. Int &r = num;) ). Pointer = address (int * p = &a;); ). For declared Pointers, p refers to the address of variable A, and *p refers to the contents of variable A. Using references instead of Pointers in C++ can improve program security, but Pointers can only be used when variables need to be reassigned to a different address or assigned to NULL.

Const int * p1 / const p2

Const int * p1 declares a pointer p1 to an integer constant, so we cannot change the value of the integer it points to. Int * const p2 declares a pointer constant to hold the address of an integer variable. Once initialized, this pointer cannot be reassigned. (Mainly refer to const modifier who).

Null and wild Pointers

NULL pointer: declare a pointer variable assignment is NULL, this is a NULL pointer, the various types of NULL Pointers are indeed memory address, but does not point to any valid value of memory address, the NULL pointer operations, to access properties and methods, for example, will be thrown NULL pointer exception, because there is no corresponding NULL pointer to memory address to physical address.

Wild pointer: refers to those that free memory, but the pointer is assigned a null value, in this case the pointer to any address, terrible, such as the kernel address or memory address does not belong to the program will be killed, i.e. crash.

Memory leak: can be divided into heap leak and resource leak. Memory allocation failure or memory allocation success but no pointer to it (that is, cannot operate the memory) will lead to more and more memory allocation, resulting in insufficient memory and termination of the program.

C/C++ memory management needs to follow the rules

After applying for memory using malloc or new, you should immediately check whether the pointer value is NULL to prevent using memory with the pointer value of NULL.

Don’t forget arrays and dynamic memory assignment primitives to prevent uninitialized memory from being used as rvalues;

Avoid arrays or pointer subscripts that are out of bounds, especially “more than one” or “less than one” operations;

Dynamic memory application and release must be paired to prevent memory leaks.

Set the pointer to NULL immediately after freeing memory with free or delete to prevent “wild Pointers”.

On inheritance and derivation

The access control attributes of a member inherited from the base class are affected by two factors (1) the access control attributes of the member originally declared in the base class; 2, inheritance). When using the constant relationship, note that neither the constructor nor the destructor of the base class can be inherited. However, the constructor of the base class is automatically called when a derived object is created, and the destructor of the base class is automatically called when the derived class dies. In the case of multiple inheritance, if there is a common base class, there will be a member identity ambiguity problem, which is a good solution to the common base class as virtual base class inheritance (virtual base class inheritance syntax: class derived class name: virtual base class name).

17. On polymorphism

Polymorphism is when the same message is received by objects of different types resulting in completely different behavior. It is a reabstraction of a particular member function of a class. There are many types of polymorphism supported by C++, of which overloading (including function overloading and operator overloading) and virtual functions are the main ones.

18. About abstract classes

The class with pure virtual function is abstract class, the main function of abstract class is through it to establish a common interface for a class family, so that they can effectively play the polymorphic characteristics. Abstract classes declare the common semantics of a set of derived classes that operate on interfaces, and the full implementation of the interface, the body of a pure virtual function, is given by the derived classes themselves. However, the derived class of an abstract class does not have to give an implementation of the pure virtual function. If the derived class does not give an implementation of the pure virtual function, the derivation is still an abstract class.

Fictitious constructor and virtual destructor

In C++, you can’t declare make-believe functions. Polymorphism is the behavior of different objects for the same message. Virtual functions are the basis of the polymorphism during execution and are mainly for objects. Can virtual destructor, the destructor function is in the class object for some necessary cleanup before dying, if a class’s destructor is a virtual function, so it all derived subclass destructor is also a virtual function, the destructor is set to the virtual function, referenced in the use of Pointers to dynamically build, realizes the runtime polymorphism, Make sure that using Pointers to the base class you can call the appropriate destructor to clean up for different objects.

STL containers, iterators, and algorithm concepts

STL’s container library includes seven basic containers: vectors, deques, lists, sets, multisets, maps, and multimaps. These seven types of containers can be divided into two basic types: sequential containers and associative containers.

STL classifies iterators into five categories according to their functions: input iterators, output iterators, forward iterators, bidirectional iterators, and random-access iterators.

The algorithms in the STL standard template library can be roughly divided into four categories: non-mutable sequence algorithms (which do not change the contents of the container), mutable sequence algorithms, sorting related algorithms, and general numerical algorithms.

Stream libraries and INPUT/output

The I/O streaming library is an object-oriented library that provides input/output functionality. A stream is an abstract representation of input/output. The program implements input/output by extracting characters from and inserting characters into the stream. Streams are typically associated with actual character sources or targets, such as disk files, keyboards, or monitors, so the extraction or insertion of streams is really an operation on a physical device. The standard input/output stream object is used to connect the program with the standard input/output device. The standard input stream is CIN, and the standard output stream is COUT, CERR, and CLOG. Standard flow objects are pre-declared in.

22. Exception handling

An exception occurs when an operation has to be interrupted due to something unusual happening in the execution of a function, or when the result of the operation is not defined. An exception is usually an object produced with the throw keyword to indicate that something unexpected has happened. The exception handling mechanism of C++ has many advantages. It can make the exception raising and handling need not be in a function, so that the underlying function can focus on solving the specific problem, and do not have to worry too much about the exception handling. Upper-level callers can design handling of different types of exceptions in place. C++ provides three keywords to handle exceptions:

Try: Segments that may throw exceptions must be opened with a try. Immediately following the try is a program enclosed in braces that may throw an exception.

Throw: The exception is thrown by the keyword throw. The type of the exception object determines which catch statement can catch the exception.

Catch: Programs that handle exceptions must begin with a catch. Following the catch is a program enclosed in braces.

C/C++ macro definition

#define is a macro definition command provided by C language. Its main purpose is to make the program standardized, easy to modify and debug, and to a certain extent, improve the running efficiency of the program. Use the #define command to define an identifier as a string, called the macro name, and the string defined as the replacement text. This command comes in two formats: a simple macro definition and a macro definition with parameters.

24. Lambda functions

Lambda functions were added to C++ as a new feature of C++11, which mainly uses anonymous functions to capture variables within a scope to construct closures. Reference: c++ reference