C + + study notes-2. Preliminary knowledge of C + +

Source: Internet
Author: User
Tags function prototype

One, the simplest C + + program

1. Standard C + + requires that the main function must be declared as int, and if the program executes properly, return the value 0 to the operating system, otherwise return the value-1

2. All header files in the C language have suffixes. h, and as per C + + standards, the system-supplied header file does not have a suffix. h, the user-compiled header file can have a suffix. h, the C language can also be used to compile the system with the suffix. h header File

Classes and functions in the 3.c++ standard library are declared in the namespace STD, so if you need to use the content in the C + + standard library in your program, you need to use the using namespace Std;

Second, C + + to the expansion of

1.c++ Input and output

Stream name Meaning Hidden devices
Cin Standard input Keyboard
cout Standard output Screen
Cerr Standard error Output Screen
Clog Buffer form of Cerr Screen

Note: The C language requires that the declaration part of a variable must precede the execution of the statement, while C + + allows the declaration of the variable to be placed anywhere in the program (but before the variable must be used)

2. Define the symbolic constants in C language with the # define directive

C + + provides a method for defining a constant variable with const, for example: const FLOAT PI = 3.14

Difference: A const-defined constant variable has the properties of a variable, has a data type, occupies a storage unit, has an address

3. Function Prototype declaration: in C + +, if the position of the function call precedes the function definition, it is required that the called function must be declared as a function prototype before the function call

4. Overloading of functions: C + + allows you to define multiple functions with the same function name in the same scope, with different arguments and parameter types, which are used by functions with the same name to implement various functions

Note: The number and type of arguments are not allowed to be the same and only the return value types are different

5. Function Template: The number of parameters applicable to the function is different from the same type, and the function body is the same as the 1

Format: template <typename t> or template <class t>

Cases:

Template <typename t>t Max (t A, T B, t C) {    if(b > a)        = b;     if (C > a)         = C;     return A;}

6. Functions with default parameters

Example: Float volume (float h, float r = 12.5);

Function call: Volume (45.6); or volume (34.2, 10.4);

Note: (1) The combination of real participation parameters is left-to-right, so the parameter that specifies the default value must be placed in the right-most side of the formal parameter table column

(2) If you need to specify a default value, you should specify it before calling the function, and to avoid confusion, it is best to specify the default value only when the function declaration

(3) A function cannot be used both as an overloaded function and as a function with default parameters

7. References to variables

Format: TypeName &refname = VarName;

Note: (1) declaring a reference to a variable does not open an internal deposit element; When declaring a reference, it must be initialized at the same time, declaring which variable it represents

(2) After declaring a reference to a variable, during the execution of this function, the reference is always associated with the variable it represents, and can no longer be used as an alias for other variables, that is, int &b = A1; int &b = A2; It's wrong.

(3) A reference to a variable that it represents shares the same memory unit, and the system does not allocate storage space for the reference, in fact, the compilation system makes the reference and the variable it represents have the same address, that is, sizeof (a) = sizeof (b)

(4) For initialization of a reference, you can use a variable name or another reference

Use: (1) reference as a function parameter: The value of the variable is passed to the parameter when the variable is the argument, and the parameter value changes during the execution of the function does not pass back to the argument

Cases:

#include <iostream>using namespacestd;voidSwapint&a,int&b) {    inttemp; Temp=A; A=b; b=temp;}intMain () {inti =3, j =5;    Swap (I, j); cout<<"i ="<< I <<"j ="<< J <<Endl; return 0;}

Note: (1) cannot create a reference of type void

(2) cannot create a reference array because the array name only represents the address of the first element of the array and does not itself occupy storage space

(3) You can assign the address of a variable's reference to a pointer, and the pointer points to the original variable

(4) A reference to a pointer variable can be established

Example: int i = 5; int *p = &i; int* &pt = p; cout << *pt; Output 5 at this time

(5) A reference can be qualified with const and the value of the reference is not allowed to be changed, but it does not prevent changing the value of the variable that the reference represents

This feature is often used as a function parameter to protect the value of a parameter from being changed

(6) A reference can be initialized with a constant or an expression, but must be declared with a const

At this point the compiler processes: generates a temporary variable to hold the value of the expression, which is the variable alias

8. Built-in functions: Embed the code of the called function into the keynote function at compile time, also known as inline function

Generally small and frequent use of functions to greatly improve the speed of operation

Cases:

int max (intint int c) {    if(b > a)        = b;     if(C > a)        = C;     return A;}

9. Scope Operator: C + + provides scope operator::, it can specify the desired scope

Cases:

#include"iostream"using namespacestd;floatA =1;intMainintargcChar Const*argv[]) {    intA =2; cout<< a <<"\ t"<<:: A <<Endl; return 0;}

Output at this time: 2 1

10. String variables

A string class is declared in the C + + standard library, and when you use the String class feature, you must include a string header file in the C + + standard library at the beginning of this file, that is #include <string>

(1) Assigning a value to a string variable:

Example: String str, TMP = "China";

str = "Canada"; or str = tmp;

When defining a string variable without specifying a length, its length varies with the length of the string, and does not store the end of the

(2) Input and output of string variables: cout << string1; Cin >> string2;

(3) operation of string variables: Copy directly with equal sign, string concatenate with Plus, string comparison directly with relational operator

Example: str = string1 + string2;

(4) String array: Does not require that each string element have the same length

11. Dynamically allocate/REVOKE memory operator new and delete

Note: If space cannot be allocated properly due to insufficient memory, new will return a null pointer null

New and delete are operators, not functions, and new is used with delete

C + + study notes-2. Preliminary knowledge of C + +

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.