C ++ from scratch (2)-Basic Knowledge 2

Source: Internet
Author: User

First, let's take a look at today's score:

Although it has not reached three hundred, there is still progress.

Today's goal is 350.

Continue to record the blind spots in C ++.

Static Member

It can be divided into static data members and static function members.

Static data members are actually global variables in the category. Therefore, the definition (initialization) of static data members should not be put in the header file.

Static data members are shared by all objects of the class, including objects of the class derived classes. That is, the derived class Object shares the static data member of the base class with the base class object.

Static data members can be optional parameters of member functions, but normal data members cannot.

Static member function addresses can be stored as common function pointers, while common member function addresses must be stored as class member function pointers.

. Static member functions cannot call non-static members of a class. Because the static member function does not include the this pointer.

Static member functions cannot be declared as virtual, const, and volatile functions at the same time.

The last point is that static members can be accessed independently, that is, they can be accessed without creating any object instance. Only one copy exists in the bucket. It can be called through classes and objects.

Modifying global variables with static changes the scope of its scope, from the original project visible to the current source file visible.

Static members are initialized in the global zone.

Youyuan

Class, that is, access a private or protected member through an instance. This is forbidden. But in terms of practicality, it is indeed sometimes necessary to access outside, C ++ adds a statement called "friend (friend)" function, grant "Privilege" to some functions (either global functions or member functions of other classes) so that they can access the private and protected members of the class.
Youyuan functions must be declared in the class, and youyuan functions must not be member functions of this class. Therefore, such a "Privilege" is not completely object-oriented. Of course, we can skip it. In addition, the statement of the youyuan function is invalid in the derived class, unless it is declared again in the derived class, of course, there is no problem in using the type conversion as the base class.

All member functions of the youyuan class are the youyuan functions of the other class and can access the hidden information (including private and protected members) in the other class ).
When you want a class to be able to access the private members of another class, you can declare this class as another type of membership class.

Note:
(1) Friendship cannot be inherited.
(2) The relationship between friends and friends is unidirectional and not interchangeable. If Class B is a friend of Class A, Class A is not necessarily a friend of class B. It depends on whether there is a corresponding declaration in the class.
(3) The relationship between friends and friends is not transmitted. If Class B is a friend of Class A, Class C is a friend of Class B, and class C is not necessarily a friend of Class A, it also depends on whether there is a corresponding declaration in the class.

Macro definition

Macro definition without parameters: macro definition is also called macro replacement or macro replacement ". Format: # The identifier of the define identifier string is a symbolic constant, also known as a "macro name ".
Before explaining how to use macros with parameters, let's take a look at the several notes when using macros with parameters.

There cannot be spaces between macro names and parameter table brackets.

Macro replacement is only used for replacement, and calculation and expression solving are not performed. This requires special attention.

Function calls are performed when the program runs after compilation and memory is allocated. Macro replacement is performed before compilation without memory allocation.

Extern usage

In C, the modifier extern is used before the declaration of a variable or function to indicate that "this variable/function is defined elsewhere and must be referenced here ".

In fact, to call functions and variables in other files, you only need to include the file with # include. Why use extern? Because the use of extern will accelerate the compilation process of the program, which can save time.

In C ++, extern has another function, which is used to indicate the call specifications of C or C ++ functions. For example, to call a C-Database Function in C ++, you must declare the function to be referenced using extern "C" in C ++. This is for the linker and tells the linker to use the c Function Specification for link. The main reason is that the naming rules in the target code are different after the C ++ and C Programs are compiled. This is used to solve the name matching problem.

Internal and external functions

If a function can only be called by other functions in this file, it is called an internal function. When defining an internal function, add static before the function name and function type. The general format of the function header is
Static type identifier function name (table of parameters)
For example
Static int fun (int A, int B)
Internal functions are also called static functions. Internal functions can be used to restrict functions to files. If different files contain internal functions with the same name, normally, the functions and external variables that can only be used by the same file are put in one file. before them, all functions and external variables are given static to make them localized. Other files cannot be referenced 。

When defining a function, if the keyword extern is appended to the leftmost end of the function header, this function is an external function and can be called by other files 。
If the function header can be written
Extern int fun (int A, int B)
In this way, function fun can be called for other files. If extern is omitted when defining a function, the default function is an external function. All functions used earlier in this book are external functions 。
In the file that needs to call this function, use extern to declare that the function used is an external function 。

Copy constructor

The name of the copy constructor must be the same as the class name. The form parameter of the function is a reference variable of the current type and must be a reference.

When an initialized custom class object is used to initialize another newly constructed object, the copy constructor is automatically called, if you do not have a custom copy constructor, the system will provide a default copy constructor to complete this process.

The definition of copy and shortest copy can be simply understood as: If a class has resources (heap, or other system resources), when the object of this class is copied, this process can be called Deep copy. If the object has resources but the copying process does not copy resources, it is considered as a light copy.
A program running error occurs when resources are released after the resources are copied in a shortest environment!

Operator overload

The operation objects of pre-defined operators in C ++ can only be basic data types. However, similar operations are also required for many user-defined types (such as classes. In this case, you must redefine these operators in C ++ and assign new functions to existing operators so that they can be used for specific operations of specific types. The essence of Operator Overloading is function overloading. It provides the scalability of C ++ and is also one of the most attractive features of C ++.

Operator Overloading is implemented by creating operator functions, which define the operations to be performed by overloaded operators. The definition of an operator function is similar to that of other functions. The only difference is that the name of an operator function is composed of the operator keyword and the operator symbol to be overloaded.

Rules to be followed:

(1) Apart from class Relational operators ".", member pointer operators ". *", scope operators ":", sizeof operators, and three-object operators "? : ", All operators in C ++ can be overloaded.

(2) Heavy-duty operators are restricted to operators that allow heavy-duty within the range of existing operators in the C ++ language. New operators cannot be created.

(3) Operator Overloading is essentially a function overload. Therefore, the compiler selects Operator Overloading Based on the function overloading principle.

(4) The operator after the overload cannot change the operator's priority and combination, nor change the number and syntax structure of the operator's operands.

(5) An operator overload cannot change the meaning of the Operator Used for an object of internal type. It can only be used with user-defined objects, or when user-defined objects and internal objects are used together.

(6) Operator Overloading is an appropriate transformation to the original operator for new types of data. The overload function should be similar to the original function to avoid the use of overload operators without any destination.

Iomanip

Iomanip. H is the header file of I/O traffic control. Just like the formatted output in C, the header file in the new C ++ version has replaced iomanip. h. Io indicates the input and output, and manip is the abbreviation of manipulator (the manipulator is valid only in C ++ ).

SETW (n): Set the domain width to n characters.

Setfill (c) is set to C

Setbase (int n) converts a number into N-Base

Setprecision (n) is set to show N decimal places

Setiosflags (IOs: Left) left alignment
Setiosflags (IOs: Right) Right-aligned

Today's c ++, starting from 343

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.