C ++ day03 Study Notes

Source: Internet
Author: User

1. Once the CIN input stream is corrupted, it cannot be restored.

2. function: RelatedCodeTo achieve a specific purpose.
(1) Names can be used to enhance code reusability.
(2) improve maintainability.

3. Function Definition = Declaration + function body:
<Return type> function name (<parameter table> ...){
Return...
}
Function return value: the return value can be saved only when the function is called.
When a function is defined, the parameters in parentheses are called parameters that make sense when the function is called.
When a function is called, The passed parameters in parentheses are called real parameters.

4. A function can only be declared before it is called, because in the compilation phase, the syntax check can be completed by referring to the Declaration.
Function declaration <return type> function name (<parameter table> ...);
Separation of declaration and definition for parallel development!

5. global variables and local variables
Local variable: The variable declared inside the function. It can only be used inside the declared function. If it is used without initialization, its value is a junk number.
Global variables: they can be used together by all functions and must be declared outside all functions (including main functions. Try to use it as little as possible, and the impact is too great. If it is not initialized, It is used. The default value is 0.

6. Data zone: Only global data is stored. data in the data zone is stable,ProgramIt is created at startup, and disappears at the end of the program.
Heap Area
Code Area
STACK: stores local variables. Insert data from the top of the stack.
If the function is not called, the local variable does not exist.
There is a mark between the main function and other functions. Once the function returns, the stack falls down to the mark.
The data in the stack changes frequently.
When a function is created during a call, the return value of the function disappears.

7. Value Transfer
Instead of passing real parameters, a function call creates a temporary variable and transmits it to the function.
(1) create a temporary variable for the form parameter. The value of the temporary variable is the value of the copy real parameter.
(2) The use of the form parameter is actually the use of the temporary variable and does not change the size of the real parameter.

8. Default Parameters
(1) area (float a, float B = 10); when only one parameter is passed, the default position of parameter B is 10.
(2) Place the parameters with default values on the right

9. inline functions
"Inline" is added during declaration, which is highly efficient and does not support structure control statements. It must be defined first and does not support function prototypes.

10. recursive functions
Call yourself within the function.
There must be a clear return condition-recursive exit

11. Function Overloading
(1) function names are the same.
(2) Different parameter tables (different parameter numbers, different parameter types, and different parameter order ).
(3) close to Parameter Name and return type
The corresponding function is automatically called according to the parameter table matching.
After compilation, the function name will change, and the abbreviation of the form parameter will be added as the suffix. No function name is the same.
Automatic type upgrade is not performed
It is best not to use the default parameter value for the overloaded function.

12. Const
This parameter cannot be modified inside the function.

13. requirement analysis, design, and code implementation

View code

 
========================================================== =Chang. h========================================================== =CharChange (Char);
View code

========================================================== =Chang_impl.cc ========================================================== = Char Change ( Char  C ){  If (C> = 97 & C <= 122  ){  Return C- 32  ;}  Else   If (C> =65 & C <= 90  ){  Return C + 32  ;}  Else  {  Return  C ;}} 
View code

========================================================== = Main. CC ========================================================== = # Include <Iostream> # Include  "  Change. h  "      Using   Namespace  STD;  Int  Main (){  Char C = '  0  '  ;  While ( 1 ) {Cout < "  Enter a char [0 exit]>  "  ; CIN > C;  If (C = '  0  '  ){  Break  ;} Cout < " ====>  " <Change (c) < Endl ;}  Return   0  ;} 

14. external variables and internal variables
External variable: extern int g; ---- declare external variable, introduce a variable, declare it in other source programs
Internal variables: variables declared in the source file

The external variable must be a global variable!

Scope lifecycle storage location
Global variable (external variable) All function programs start-> program end data Zone
Call the internal function of the local variable function-> return stack of the Function
When a function is called for the first time in a static local function (only initialized once)-> the end data area of the program-only one copy of data is required, and other functions are not expected to access the function.
Static global program start in the source file-> program end data Zone

Job: implement banking services =========== A. CC ==========================
Long ID;
Int password;
Double balance;

Long (ID) Create (balance, password); // create an account
Void save (SUM );
INT (staus 0,-1) withdraw (sum, password );
Float query ();
==========================================
Generatorid (); // obtain the unique ID.
Int showmenu (); // real Main Menu

Savemenu (); // collect account information, and then call create account
Createmenu ();
Withdrawmenu ();
Querymenu ();
==========================================

*** Today's key points: (1) differences between form parameters and real parameters
(2) Recursion
(3) function Overloading

Exercise: Observe the feilactor sequence, 34 ...... the first and second items are both 1, and each of the following items is the sum of the first two adjacent items.
Use recursive methods to calculate the result of the number N of the ferro-tangent series.

 

 

 

 

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.