Basic knowledge of C + + learning notes

Source: Internet
Author: User
Tags define function goto

#include <iomanip>
setpresition (int n); Sets the output precision of the floating-point number to N.
"Goto Statement"
Goto, also known as unconditional transfer statement, buys the courage to change the order in which statements are executed. The general format of a goto statement is:
Goto marking;
Cases:
#include <iostream>using namespace Std;int main (int args, char *argv[]) {    int ivar = 0;    int num = 0;label:    Ivar + +;    num + = Ivar;    if (Ivar <) {        goto label;    }    cout << num << endl;    return 0;}

The goto statement is less efficient.
A goto statement cannot cross a statement defined by a variable other than a compound statement.
Examples of function usages that call default arguments:
#include <iostream>using namespace std;void outputinfo (const char* pchdata= "Stay Hungry,stay foolish.") {    cout << pchdata << endl;//output Information}int main (int argc, int *argv[]) {    outputinfo ();   Use the default value as the actual parameter of    the function outputinfo ("Lasolmi");  Pass the actual argument directly to    return 0;}

"Variable parameter"
A function that declares a mutable parameter is the same as declaring a normal function, except that there is a "..." in the argument list. For example:
void outputinfo (int num, ...)
For a variable parameter function, you need one by one to read the actual arguments passed by the user when defining the function. You can use the Va_list type and Va_start, Va_arg, va_end 3 macros to read the parameter values passed to the function. Using a mutable parameter requires referencing the STDARG.H header file.
Example: Defining and invoking a variadic function
#include <iostream> #include <stdarg.h>using namespace std;void outputinfo (int num,...) {    va_list arguments;    Va_start (arguments, num);    while (num--) {        char *pchdata = va_arg (arguments,char*);        int iData = va_arg (arguments, int);        cout << pchdata << Endl;        cout << iData << endl;    }    Va_end (arguments);} int main (int argc, int *argv[]) {    outputinfo (2, "Bejing", "a", "Lasolmi");    return 0;}

Overloaded functions: Multiple functions have the same function identifier, but the parameter type or number of parameters is different. When a function is called, the compiler distinguishes which function is called by the type and number of arguments.
inline function
Using inline functions can reduce the overhead of function calls (moving the pointer in the same file to find the cost of calling the function address), and you should define the function as an inline function if the function implementation code is short or if the function is called with a relatively small number of calls.
Storage category for variables: auto; Static Register extern
Auto variable: The default storage type in C + +
The scope of an automatic variable is limited to the individual within which the variable is defined. Dynamic storage, in which different individuals in the stack are allowed to use variables of the same name without confusion.
Static variable:
The definition in the function is used in the function, but the lifetime is the whole program.
Register variable:
The register variable stores the value of the local variable in the register of the CPU, which does not require access to memory and is read and written directly from the register.
(1) The register variable belongs to the dynamic storage mode. A variable that needs to use static storage cannot be defined as a register variable.
(2) The compiler automatically determines which variable is used for register storage. Register can play a role in program optimization.
extern variable:
When using a global variable from another source file, you only need to declare the variable by using the extern keyword in the origin file.
Pointer to function: Pointer variable can also point to a function. A function is assigned to a portal address at compile time, and the function's entry address is called a pointer to a function, which can be pointed to by a pointer variable and then called by the pointer variable.
Cases:
#include <iostream> #include <iomanip>using namespace std;int avg (int a, int b); int main (int argc, int *argv[]) {    int iwidth, ilength, Iresult;    Iwidth = ten;    Ilength = +;    Int (*pfun) (int, int); Define function pointer    pfun = AVG;    Iresult = (*pfun) (iwidth,ilength);    cout << iresult << Endl;    return 0;} int avg (int a, int b) {    return (A+B)/2;}

Reference
The form of a reference:
Data Types & Expressions
For example:
int a = 10;
int & ia = A;
IA = 2;
A reference variable IA is defined, which is the alias of variable A, and the operation on IA is exactly the same as for a. Ia=2 assigns 2 to A,&ia returns the address of a, executes a=2 and performs ia=2 equivalence.
Instructions for using references:
(1) once a C + + reference is initialized, it cannot be used to refer to another object and it cannot be re-constrained.
(2) The reference variable is just an alias for the other variable, and the operation of the original object has the same effect.
(3) There are two main differences between pointer variables and references: A pointer is a data type, and a reference is not a data type, the pointer can be converted to the data type of the variable it points to, so that the type on either side of the assignment operator wants to match, whereas when using a reference, the data type of the reference and variable must be the same Type conversions cannot be performed. The second is that the reference is used more than the pointer side.
Will exactly
(4) The reference should be initialized, otherwise it will be an error.
Passing parameters using references
Swap (int &a, int &b) {
int tmp = A; A = b; b = tmp;
}
Pointer Pass parameters
Swap (int *a, int *b) {
int tmp = *A; *a = *b; *B = tmp;
}
A[I][J] = = * (* (a+i) +j)
"Pointer Array"
A one-dimensional pointer array is defined as:
Type name * array name [array length];
Example: int *p[5];
The array name in the pointer array is also a pointer variable, which is a pointer to the pointer.
Example: Use struct pointer variables to refer to struct members.
#include <iostream>using namespace Std;int main (int argc, int *argv[]) {    struct PERSONINFO {        int index;
   
    char name[30];        Short age;    } *ppersoninfo, pinfo={0, "Lasolmi", up};    Ppersoninfo = &pInfo;    cout << ppersoninfo->index << Endl;    cout << ppersoninfo->name << Endl;    cout << ppersoninfo->age << Endl;    return 0;}
   

"Nesting of structs"
(1) Define a sub-structure in a struct.
(2) Declare other well-defined struct variables when defined.
You can initialize an array directly when you declare the structure of the body:
struct PersonInfo {    int index;    Char name[30];    Short age;} Person[4] = {    {1, "Figo", 20}, (    2, "Ling", +), (3, "Me", "+")    ,    (4, "Cong";

"Shared Body"
The general form of defining a common body type is:
Union Common body type name {    member type shared body member name 1;    Member type shared body member name 2;    ...    Member type shared body member name N;};

Size of the common body: the common body each member occupies its own memory unit. The memory length of a shared body variable equals the length of the longest member. A common body variable cannot hold the value of more than one member at a time, and it can only hold the value of one of its members at a time, which is the value given at the end.
"Enum Type"
Common enumeration type declaration forms:
Enum Enum type name
{
identifier [= integral type constant];
identifier [= integral type constant];
identifier [= integral type constant];
} enumeration variables;
Example: The assignment of an enumeration variable.
#include <iostream>using namespace Std;int main (int argc, int *argv[]) {    enum Weekday {sunday,monday,tuesday, Wednesday,thursday,friday,saturday};    int a = 2, b = 1;    Weekday Day;    Day = (Weekday) A;    cout << Day << Endl;    Day = (Weekday) (A-B);    cout << Day << Endl;    Day = (Weekday) (sunday+wednesday);    cout << Day << Endl;    Day = (Weekday) 5;    cout << Day << Endl;    return 0;}

The output is:
2
1
3
5
"Custom Data Type"
typedef < primitive type name > < new type name >
Cases:
typedef in INTEGER;
INTEGER A, B;
Equivalent to:
int A, B;

C + + Learning notes basic knowledge

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.