Object-oriented analysis and design Experimental report one
A Storage categories for variables
Auto Static register extern
Auto variables
Local variables in a function, such as those not specifically declared as static storage classes, are dynamically allocated storage space, and data is stored in dynamic storage. The parameters in the function and the variables defined in the function, including the variables defined in the compound statement, belong to this class, which is allocated by the system when the function is called, and the storage space is freed automatically at the end of the function call. This type of local variable is called an automatic variable. Automatic variables are declared with the keyword auto as the storage class.
int f (int a)/ * define F function, A is parameter */{ int b,c=3 ; /* defining B,C Automatic variables */
with Static declaring local variables
Sometimes you want the value of a local variable in a function to remain the original value after the function call ends, and you should specify the local variable as a static local variable and declare it with the keyword static.
floatTestinta) {Autointb=0; Static floatC=0.5; cout<<"B ="<<b<<"c="<<c<<Endl; //the definition of C is a static variable,//does not disappear after the function call ends, but retains the original valueC++; return(a+b+c);}intMainvoid){ intA=2, I; for(i=0;i<3; i++) {cout<<test (a) <<Endl; } return 0; }
1) Static local variables are static storage classes that allocate storage units within a static storage area. is not released during the entire run of the program. The automatic variable (i.e. dynamic local variable) belongs to the dynamic storage class, which is the dynamic storage space, which is released after the function call is finished.
2) Static local variables are assigned the initial value at compile time, that is, only the initial value is assigned, and the initial value of the automatic variable is performed at the function call, and each call to the function re-gives the initial value, which is equivalent to executing an assignment statement.
3) If you do not assign an initial value when defining a local variable, a static local variable is automatically assigned the initial value of 0 (for a numeric variable) or a null character (for a character variable). In the case of automatic variables, if the initial value is not assigned it is an indeterminate value.
Register variables
To improve efficiency, local variable values are allowed to be placed in registers in the CPU, which are called "register variables" and are declared with the keyword register.
intFacintN) {Registerinti,f=1; for(i=1; i<=n;i++) F=f*i; return(f);}intMainvoid){ inti; for(i=0; i<=5; i++) cout<<i<<"!="<<FAC (i) <<Endl; return 0;}
1) only local automatic variables and formal parameters can be used as register variables;
2) A computer system has a limited number of registers, can not define any number of register variables;
3) Local static variables cannot be defined as register variables.
with extern declaring an external variable
An external variable (that is, a global variable) is defined outside the function and is scoped to the end of the program file, starting at the definition of the variable. If an external variable is not defined at the beginning of the file, its valid scope is limited to the definition to the end of the file. If the function before the definition point wants to refer to the external variable, then the variable should be declared as an "external variable" with the keyword extern before the reference. Indicates that the variable is an external variable that has already been defined. With this declaration, it is possible to use the external variable legitimately from the point of declaration.
intMaxintXinty) { intZ; Z=x>y?x:y; return(z);}intMainvoid){ externb; //A , B is defined outside the function,//It is scoped to start at the variable definition,//to the end of this program file. cout<<max (b) <<Endl; return 0;} intA= -, b=-8;
two . Scope
(1) Global variables:
Global variables are defined outside the body of all functions, and the part of the program (and even the code in other files) can be used. The global variable is not affected by the scope (that is, the lifetime of the global variable continues until the end of the program). If you use the extern keyword in one file to declare a global variable that exists in another file, the file can use this data.
(2) Local variables:
Local variables appear within a scope, and they are confined to a function. Local variables are often referred to as automatic variables because they are automatically generated when they enter the scope and disappear automatically when they leave the scope. The keyword Auto can explicitly describe the problem, but the local variable defaults to auto, so it is not necessary to declare it as auto.
#include <iostream>using namespacestd;intb =0;//B is a global variable intMaxintXinty) {b=9; intZ//z is local variable default to auto only within function body scopeZ=x>y?x:y; returnZ;} intMain () {intA =b; intj =1; cout<<a++<<Endl; { intj =8;//J only works in {}cout<<j<<Endl; } cout<<j<<Endl; cout<<"the maximum value of a and B"<<max (b) <<Endl; cout<<"the maximum value of a and B (b changed in function max)"<<max (b) <<Endl; return 0;}
three . Types of Parameters
Value passing, pointer passing, and reference passing
1, the value of the pass: The parameter is a copy of the argument, changing the value of the function parameter does not affect the value of the external argument, this is the most commonly used as a method of communication, but also the simplest method of transfer, only need to pass parameters, return value that is return to consider;
void Swap1 (int A,int b)
2. Pointer passing: The pointer passing parameter is essentially a value pass, it passes an address. "In the process of value passing, the parameters of the called function are treated as local variables of the called function, that is, the memory space is opened in the stack inside the function to hold the value of the argument that is put in by the main function, thus becoming a copy of the argument (remember this, the parameter in the function is a copy of the arguments)". Because the pointer passes the address of the external argument, the natural external argument value changes when the parameter value of the function is changed.
void Swap2 (int *a,int *b)
3. Reference passing: Although the parameters of the modulated function also open up memory space in the stack as local variables, the address of the argument variable placed by the main function is stored in the stack. Any operation of the modulated function on the formal parameter is handled as an indirection, that is, by accessing the real parametric in the central melody function through the address stored in the stack (the arguments and parameters are combined by reference, the white point is: a person, there are two names of that, the latter would like to elaborate). Therefore, any changes to the parameters directly affect the arguments.
void swap3 (int &a,int &b)
#include <iostream>using namespacestd;//Value PassingvoidSWAP1 (intAintb) { inttemp; Temp=A; A=b; b=temp;}//Reference DeliveryvoidSWAP2 (int&a,int&b) { inttemp; Temp=A; A=b; b=temp;}//Pointer passingvoidSWAP3 (int*a,int*b) { inttemp; Temp= *A; *a = *b; *b =temp;} intMain () {intA =1, B =2; cout<<"now the values for A and B are"<<a<<" "<<b<<Endl; Swap1 (A, b); cout<<"After passing the parameter function with the SWAP1 () value, the value of a, B is"<<a<<" "<<b<<Endl; cout<<"now the values for A and B are"<<a<<" "<<b<<Endl; Swap2 (A, b); cout<<"After the SWAP2 () reference passes the parameter function, the value of a, B, respectively, is"<<a<<" "<<b<<Endl; cout<<"now the values for A and B are"<<a<<" "<<b<<Endl; SWAP3 (&a,&b); cout<<"After passing the parameter function with the SWAP3 () pointer, the value of a, B is"<<a<<" "<<b<<Endl; return 0;}
C + + namespaces and scoped function parameters Object-oriented experiment report