@TOC

C++ program general format, compile instructions, comments, main function

1. Filename header: iostream, old style: iostream.h file extension CPP is a common way of using c++ programs. C++ simple program:

#include <iostream>
using namespace std;
int main(a)
{
    cout<<"Come up and C++ me some time.";
    cout<<endl;
    cout<<"You wont't regret it"<<endl;
    return 0;
}
Copy the code

Running results:#include function header: int main() Compile instruction: using namespace namespace: STD Function body: enclose display information with {} : cout end mian() function: return

3. The main () function

int main(a)
{
    statements;
     return 0;
}
Copy the code
int main(a)The function header indicates that main () can call its function to return an integer value and nothing else. C++ recommended.Copy the code

1. Many existing programs start with the classic C function:

mian()
Copy the code

In C, omitting the return type is equivalent to saying that the function is of type int. However, c++ gradually phased out this usage. You can also use the following variants:

int main(void)
Copy the code

Void indicates that the function takes no arguments. In c++, leaving parentheses empty is equivalent to using void inside parentheses. Some programmers use the following function body, omitting the return statement:

void main(void)Not recommendedCopy the code

2. Why can’t main() be called anything else? C++ programs must contain a main() function (not main(), main(),mane()). Remember that case and spelling are important), when running c++ programs, usually start with the main () function. Therefore, the program is incomplete without the number of main() functions, and the compiler will indicate that main() is not defined.

Cout,cin for output how do I use endL and newlines for input

1.. Use Cout, CIN for output input

Code demo:

#include <iostream>
using namespace std;
int main(a)
{
    int carrots;
    cout<<"How many carrtss do you have ?"<<endl;
    cin>>carrots;
    cout<<"Here are two more";
    carrots=carrots+2;
    cout<<"Now you have "<<carrots<<"carrots"<<endl;
    return 0;
    }
Copy the code

1. In C, use scanf and printf to input and output data (C ++ can also use this, but must include the header file stdio.h) 2. In C++, when you use input/output, you need to include a header file iostream, which contains objects for input/output. Cin represents standard input, cout represents standard output, and error represents standard error.

cin>>carrots; Input, which requires the >> operatorCopy the code
cout<<"How many carrtss do you have ?"<<endl; Output, using the << operatorCopy the code

1. Endl is a control character that restarts a line, is defined in a header file, and is in the namespace STD. When a string is printed, cout does not automatically move to the next line. Code demo:

#include <iostream>
using namespace std;
int main(a)
{
  cout<<"The Good,the";
        cout<<"Bad,";
        cout<<"and the UKulele";
        cout<<endl;
 }
Copy the code

There are no endL characters, and each string follows the preceding string. 1. The old way c++ provides line breaks: C conforms to \n:

cout<<"xxx? \n";
Copy the code

2.\n is treated as a character named newline; 3. When displaying a string, include a newline character in the string instead of adding an endl to the end to reduce the amount of input.

cout<<"\n";
cout<<endl;
Copy the code

Declare and use variables to define and use simple functions

1. The function of the declaration part is to the relevant identifier (such as variable, function, structure, common body, etc.); 2. Declaration of variables:

There are two ways to declare a variable: one is to create storage space (e.g. Int a;) ; Extern int a; extern int A; . The former is called the defining declaration, or simply the definition. The latter is called the referenceing declaration. Simply put, declarations include definitions, but not all declarations are definitions. “Int a;” In general, it is a defining declaration, which is both a declaration and a definition. Extern int a; It is a declaration rather than a definition. Such as:

int main( )
{
    extern int a; This is a declaration, not a definition. Declare a to be a defined external variable
}
int a;  // define a as an integer external variable
Copy the code

2. Assignment statements

carrots=25; will25Assign a value to the carrotsCopy the code

Conforming to = is called the assignment operator.

1 Use a function that returns a value. The function that returns a value generates a value that can be assigned to a variable or expression. For example, an SQRT () function that returns the square root, calculates the square root of 9, and assigns the value to x, uses SQRT (), and must contain the header file cmath(old math.h).

x=sqrt(9);
Copy the code

The expression SQRT (9) calls SQRT (). The expression SQRT (9) is called a function call. The function called is called a calling function, and the function containing the function call is called a calling function. 2. Function prototypes are to functions what variable declarations are to variables — indicating the types involved. For example, to define a parameter with a decimal:

double sqrt(double);
Copy the code

3. Function variants Some functions require multiple information and use multiple parameters separated by commas (,). For example, the function pow() takes two arguments and returns the first as a base and the second as a power of an exponent

double pow(double.double);
Copy the code

To compute 5 put it into the 8th power, do this:

x=pow(5.0.8.0);
Copy the code

4. User-defined functions

.#include <iostream>
using namespace std;
void West Gate(int n);
int main(a)
{
    West Gate(3);
    cout<<"Pick an integer:";
    int count;
    cin>>count;
    West Gate(count);
    cout<<"Done!"<<endl;
    return 0;
}
void West Gate(int n)
{
    cout<<"simon says touch your toes"<<n<<"times"<<endl;
}
Copy the code

The mian() function calls the Simon () function twice, once with 3 and once with the variable count. In between calls, the user enters an integer that sets the value of count. Running results:1. The Simon () function is defined in the same way as main ().

type functionname(argumentlist)
{
statements
}
Copy the code

2. The header of Simon () function is as follows:

void West Gate(int n)
Copy the code

The initial void indicates that the function returns no value, and when the parentheses int n indicates Simon (), an int argument should be provided. N is a new variable to which the value passed by the function call will be assigned. For example, the following function call assigns 3 to the variable n defined by the Simon () function header:

West Gate(3);
Copy the code

5. The user has a function that returns a value

int a(int s)
{
int *p=2 * s;
returnp; (indicates return)}Copy the code

Ex. :

#include <iostream>
using namespace std;
int stonetoplb(int);

int main(a)
{
    int stone;
    cout<<"Enter the weight in srone:";
    cin>>stone;
    int pounds=stonetoplb(stone);
    cout<<stone<<"stone=";
    cout<<pounds<<"pounds"<<endl;
    return 0;
}

int stonetoplb(int sts)
{
    return 14 * sts;// return; Back to the main
}
Copy the code

The stonetolb() function contains all of the function attributes:

  1. There is a function header and a function body;
  2. Accept a parameter;
  3. Returns a value;
  4. You need a prototype;

6. Using compilation instructions in multi-function programs

  1. Place using Namsespace STD before the function definition so that all functions in the file can use all elements of namespace STD.
  2. Define using namespace STD in a specific function that can use all elements of namespace STD.
  3. Using :: STD ::cout; This is also a compilation instruction, rather than using namespace STD; Enable the function to use the specified element, such as cout.
  4. Instead of using the compile directive using at all, the STD :: prefix is used on elements that require namespace STD.