My C ++ notes (data sharing and protection) and notes

Source: Internet
Author: User

My C ++ notes (data sharing and protection) and notes

* Data sharing and protection: * 1. Scope: * scope is a region where an identifier is valid in the body of the program. In C ++, the identifiers have the function prototype scope, local scope (Block scope), Class scope, and namespace scope. * (1). function prototype scope: * The function prototype scope is the smallest scope in C ++. The function prototype must contain the type description of the parameter. When a function is declared as a prototype, the parameter scope is the scope of the function prototype. For example, double area (double radius); the range of the radius identifier is between the brackets in the area parameter list of the function. * Because only the parameter type is used in the parameter list of the function prototype and the identifier does not work, this parameter can be omitted in the program, but for the readability of the program, generally, the parameter identifier must be provided when the function prototype is declared. * (2 ). local scope: * The scope of the form parameter in the function parameter list, starting from the declaration in the form parameter list to the end of the whole function body; the scope of the variables declared in the function body starts from the Declaration, braces until the end of the block where the declaration is located; variables with local scopes are also called local variables. * (3 ). class scope: * The class can be seen as a set of famous members. m, a member of Class x, has a class scope and has three access methods for m: if the member function of x does not declare the identifier of the local scope with the same name, then the member m can be directly accessed in the function. m or x: m. That is, the most basic method to access object members in the program. With an expression such as ptr-> m, ptr is a pointer to an object in class x. * (4). namespace scope: * a large program usually consists of different modules. Different modules may be developed by different personnel. Duplicate names may occur between classes and functions in different modules. The namespace will eliminate these errors. Syntax structure: * namespace name {* // various declarations in the namespace (function declaration, class declaration ,...); *} * A namespace determines a namespace scope. All the identifiers declared within the namespace that do not belong to the previously mentioned scopes belong to the namespace scope. You can directly reference the identifiers declared in the current namespace. to reference identifiers of other namespaces, you need to use the following syntax: * namespace :: identifier ** namespace someNs {* class SomeClass {...}; *}; * to reference the class name SomeClass or function name SomeFunc, use the following method: * someNs: SomeClass obj; * sometimes, this namespace limitation is always too long before the identifier. To solve this problem, C ++ provides the using statement, which has two types of using statements: * using namespace:: identifier; * using namespace name; * The namespace can also be nested: * namespace OuterNs {* namespace InnerNs {* class SomeClass {...}; *} * to reference the SomeClass class, use OuterNs: Inne. RNs: SomeClass syntax. * In addition, there are two special namespaces: * global namespace and anonymous namespace. The global namespace is the default namespace. All the identifiers declared outside the declared namespace are in a global namespace, an anonymous namespace is a namespace that requires no name to be declared. The Declaration is as follows: * namespace {* Various declarations in the anonymous namespace (function declaration, class declaration ,...); *} * variables with namespace scopes are also called global variables. ** 2. object survival time: * (1 ). static lifetime: * If the lifetime of an object is the same as that of the program, the object has a static lifetime. All objects declared in the namespace scope have a static lifetime. If an object with a static lifetime is declared in the local scope of the function, the keyword static is used. For example: static int I; * The amount of static variables in a local scope is characterized by the fact that it does not generate a copy with each function call, nor does it expire with the function return. That is to say, when a function returns and the next call, the variable is still the last value. A new copy of the variable will not be created even if a recursive call occurs, and the variable will be shared between each call. * (2). Dynamic Survival: * The local survival object is generated at the Declaration point, and the execution of the end statement is completed. Class member objects also have their own lifetime. The lifetime of member objects without static modification is consistent with that of the objects to which they belong. ** 3. static members of a class: * in structured programming, the basic unit of a program module is a function. Therefore, the data sharing between modules in the memory is achieved through the data sharing between functions, there are two ways: parameter passing and global variables. * (1). static data member: * If an attribute is shared by the entire class and does not belong to any specific object, a static member is declared using the static keyword. A static member has only one copy in each class, and all objects in the class are maintained and used together to share data between different objects in the same class. A class attribute is a data item that describes the common features of all objects of a class. For any object instance, its attribute value is the same as that of a static data member and has a static lifetime. Because a static data member does not belong to any object, it can be accessed through the class name. The common syntax is: * Class Name: identifier; ** (2 ). static function members: * static member functions can directly access static data and function members of the class. Access to non-static members must pass through the object name. * Class A {public: static void f (A a); private: int x ;}; void A: f (A) {cout <x; // This is WRONG! Cout <. x; // The reason why the non-static member of the member class in the static member function needs to specify the object is that the call to the static member function has no target object, therefore, non-static members of the intent class cannot be implicitly passed as non-static member functions do .} ** 4. Friends of A Class: * The relationship between friends and meta functions of different classes or objects is used to share data between them and between them. In layman's terms, it is a class that proactively declares which other classes or functions are its friends. Today, they are offered access licenses for this class. A member function of a common function or class can access the data encapsulated with another class through a friend relationship. To a certain extent, the relationship with youyuan is a damage to data concealment and encapsulation. * You can use the keyword "friend" in a class to Lifecycle other functions or classes as friends. If youyuan is a member function of a common function or class, it is called youyuan function. If youyuan is a class, it is called youyuan class. All functions of youyuan class automatically become youyuan function. * (1) non-member functions modified by the keyword friend in the class. You can use a normal function or a member function of other classes. * # Include "iostream" # include "cmath" using namespace std; class Point {public: Point (int x = 0, int y = 0): x (x ), y (y) {}; int getX () {return x;} int getY () {return y;} friend float dist (Point & p1, Point & p2 ); // you meta function declaration; private: int x, y;}; // you meta function dist definition float dist (Point & p1, Point & p2) {double x = p1.x-p2.x; double y = p1.y-p2.y; return static_cast <float> (sqrt (x * x + y * y);} int main () {Point myP1 (1, 1), myP2 (4, 3); cout <"The distance Is: "; cout <dist (myP1, myP2) <endl; return 0;} * only declares the prototype of the friend function in the Point class, you can see that you can use the object name to directly access the x and y attributes of the Point class. ** (2 ). friend Meta class: * if Class A is A member of Class B, all member functions of Class A are membership functions of class B and can access the private and protected members of Class B. * Class B {* friend class A; // you can specify A as A membership class of B. *}; * Declaring a friend Meta class is an exercise established between classes to share data between classes. * # Include <iostream> using namespace std; class A {public: void display () {cout <x <endl ;}int getX () {return x ;} friend class B; private: int x;}; class B {public: void set (int I); void display (); private: A a;}; void B :: set (int I) {. x = I; // because B is A member of A, you can access all private members of Class A in member functions of Class B;} ** note: the relationship between friends and Yuan cannot be transmitted. B is the friend of A, and C is the friend of B. If C is not declared as the friend of A, there is no friend relationship. The relationship between friends and friends is one-way. If B is A friend of A, B can access the private data of A and protect the data, but the member function of A cannot access the private and protected data of B. A friend relationship cannot be inherited. If B is A friend of A, B's derived class cannot automatically become A's friend. ** 5. Protection of shared data: * (1). Common objects: * The data member values of a common object cannot be changed throughout the lifetime of the object. That is to say, regular objects must be initialized and cannot be updated. * Const type description object name; * class A {public: A (int I, int j): x (I), y (j) {}; private: int x, y ;}; const A a (3, 4); // a is A constant object and cannot be updated. ** (2 ). class member modified by const: * common member functions: the function modified by the const keyword is a common member function. Declaration format: * type specifier function name (parameter table) const; * If you set an object to a constant object, you can only call its common member functions through this constant object, but not other member functions; * The const keyword can be used to differentiate heavy-duty functions. void print (); void print () const; * regular data member: the data member indicated by const is a regular data member, if a common data member is described in a class, no value can be assigned to the member in any function. * # Include <iostream> using namespace std; class A {public: A (int I); void print (); private: const int a; static const int B ;}; const int A: B = 10; // static data member description and initialization outside the class; A: A (int I): a (I) {} // The regular data member can only obtain the initial value through the initialization list; void A: print () {cout <a <":" <B <endl ;} int main () {A a (100), a2 (3);. print (); a2.print (); return 0;} ** common reference: * if you use const to declare a reference, the referenced statement is always referenced, objects that are often referenced cannot be updated. If it is often referenced as a form parameter, no changes will be made to the real parameter. * Const type specifiers & reference names; * Non-const references can only be bound to common objects, but not to common objects, but can be bound to common objects. A common reference, whether bound to a common object or a common object, can only treat this object as a common object when accessing this object through this reference. This means that for reference to the basic data type, the value cannot be assigned to the data. For reference to the class type, the data member cannot be modified or its non-const member function cannot be called. ** 6.c++ multi-file structure and compilation preprocessing command: * (1 ). general structure of C ++: * in multiple file structures, # The include command is used to embed the specified file into the current source file, the embedded file can be a cpp file or an H file. Command include can be written in two ways: # include <File Name> indicates to search for the file to be embedded according to the standard method. The file is located in the include subdirectory of the compiler environment, this method is generally used to embed standard files provided by the system. The other is # include "file name", which indicates that the file to be embedded is first searched in the current directory. If the file is not searched in the standard mode. * (2). external variables: * external variables can be used in the source file and can be used by other files. Variables defined in the namespace scope are all external variables by default. However, if you need to use this variable in other files, you need to declare it with the extern keyword. * (3 ). external functions: * All functions declared outside of all classes (non-member functions) have namespaces. Unless otherwise specified, such functions can be called in different compilation units, as long as they are declared for reference before being called. You can also use extern to declare a function prototype or define a function. ** 7. Standard C ++ Library: * C ++ library retains most C language system functions and other predefined templates and classes. When using the standard C ++ library, you also need to add the following statement to introduce the name in the specified namespace into the current scope: * using namespace std; * if you do not use using namespace std, when using the identifier in the std namespace, use the namespace name std:; * (1 ). compile preprocessing: * # include command: * indicates the file inclusion command. This command embeds another source file into the current source file. Generally, the # include command is used to embed the header file. * # Define and # undef commands: * # define has been widely used in C Programs, but # define can complete some functions, it can be replaced by some language features introduced by C ++. # Define is used in C to define symbolic constants, for example: # define PI 3.14; symbol constants are also defined in C ++, but the better way is to use const in the type description statement. # The role of undef is to delete the macro defined by # define so that it no longer works. * Conditional compilation command: * # if constant expression or # ifdef identifier or # ifndef identifier * program segment; * # elif * program segment; * # else * program segment; * # endif **/

 

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.