Chapter 2 begins learning C++
1. In c + +
/* the first C++ program */
#include <iostream>
using namespace std; /* Define a visualization */
int main(void)
{
cout<<"Come up an C++"<<endl;
cout<<"You won't regret it"<<endl;
return 0;
}
Copy the code
The main elements for a C++ program are:
- Comments: by prefix
//
Or is it/ * * /
logo - Preprocessor compiles instructions
#include
- Function:
int main()
- Compile instructions:
using namespace
- Function body: use
{}
enclosed - The use of c + +
cout
The statement for the tool to display the message - The end of the
main()
Function of thereturn
statements
1.1,main()
The function header
Main () is called by the startup code, which the compiler adds to the program.
The function header describes the direct interface between main() and the OS (UNIX/Linux, Windows, MAC OS, and so on).
Main () in empty parentheses takes no arguments.
int main(void)
{
statement
return 0;
}
Copy the code
The main() function describes the behavior of the function. The first line of int main() is called function heading, and the part contained in curly braces ({and}) is called the function body.
Function body: A computer instruction that indicates what a function should do.
In C++, each complete instruction is called a statement. All statements end with a semicolon.
The last statement in main(), called a return statement, ends main().
⚠️ note: C++ programs usually start with the main() function. If they don’t, the program is incomplete and the compiler will indicate that main() is not defined.
Case must be accurate
Special cases where the main() function is not required:
- In the Windows
Dynamic linking (DLL)
The module.- Microcontroller or robot chip
1.2. C++ comments
Comments in C++ begin with a double slash (//). End with the line.
Comments provide explanation for a program, making it easy to understand.
Usually identifies a part of a program or an aspect of code.
Note: The compiler does not run and simply ignores comments.
C++ also recognizes COMMENTS in C
C language style comments
Multiline comment
Symbol:/ *
and* /
Between, in order to* /
To end this comment.Single-line comments
To:Double slash (//)
To start,End of each line
To close.
1.3. Preprocessors and headers
If a program wants to use a C++ input or output tool, it must use two lines of code:
#include <iostream>
using namespace std;
Copy the code
Use #include as a precompiled instruction, known as a preprocessor instruction.
Preprocessor action: Replacing or adding text before the source code is compiled.
Such as:
#include <iostream>
Copy the code
Files like iostream are called include files ———— are also included in other files, so they are also called header files **.
Header file naming convention
Header file type | convention | The sample | instructions |
---|---|---|---|
C++ old-style | In order to.h At the end |
iostream.h | C++ programs can be used |
C Old style | In order to.h At the end |
math.h | C, C++ programs can be used |
New style of C++ | No extension | iostream | C++ programs can be used, using namespace STD; |
C after transformation | Prefix c, no extension | cmath | C++ programs can use features that are not C, such as namespace STD; |
1.4. Namespace (Namespace)
If you are using iostream in your program, you need to use namespace compilation instructions to make it available to your program.
using namespace std;
Copy the code
Also called a using compilation directive.
1.5. Use cout for C++ output
cout<<"Come up an C++"<<endl;
cout<<"You won't regret it"<<endl;
Copy the code
The part enclosed in double quotes is the message to be printed.
In C++, a series of characters enclosed in double quotes is called a string because several characters are combined.
<< indicates the path through which information flows, and Cout is a predefined object.
Operator overloading for the first time
<< can be either an insert operator or a left-shift operator.
The typical case of operator overloading, whereby the same operator represents different meanings. The compiler determines the meaning based on the context.
Common examples of operator overloading
&
—-> represents both the address operator AND the bitwise AND operator.*
—-> represents both multiplication and dereferencing of Pointers.
Control charactersendl
cout<<endl;
Copy the code
Endl is a special symbol in C++ that repeats a line.
Inserting endL in the output stream causes the screen cursor to move to the beginning of the next line.
Endl is defined in the header file iostream and is in the namespace STD.
A newline
C++ also provided an early C way of expressing line breaks; C language symbol \n.
\n is treated as a character named newline, which is a repeat of endl in C++.
When displaying a string, include a newline character in the string instead of adding endl to the end to reduce the amount of input.
/* Start a new line */
cout<<"\n";
cout<<endl;
Copy the code
1.6 C++ source code style
The style of source code in C++ follows the following rules:
- Each statement is on one line.
- Each function has one
Opening brace
And aClosing curly brace
, two curly braces on each line. - Statements in functions are indented with respect to curly braces.
- Associated with the function name
There is no white space around the parentheses
.
2. C + + statements
Examples of program code:
#include<iostream>
using namespace std;
int main(a)
{
int carrots; // Declare an integer variable
carrots = 25; // Assign a value to a variable
cout<<"I have"<<carrots<<"Carrots."<<endl;
carrots = carrots - 1; // Modify the variable
cout<<"Look! Look! Now, I have."<<carrots<<"Carrots!"<<endl;
return 0;
}
Copy the code
2.1 Declare statements and variables
In C++, variables must be declared the first time they are used. You can avoid spelling mistakes that are difficult to find.
The declaration usually indicates the type of data to be stored and the name the program uses for the data stored in memory.
A declaration statement in a program is called a definition declaration statement, or definition for short. Definition causes the compiler to allocate memory for variables.
⚠️ Note: Declarations are not necessarily definitions.
2.2 Assignment Statements
An assignment statement assigns a value to a storage unit.
The symbol = is called the assignment operator. 👉 Tips: continuous assignment operators are allowed in C++.
Assignments are made from right to left.
Other C++ statements
3.1 cin and cout
Cin extracts characters from the input stream using the >> operator.
A list of characters entered through the keyboard (that is, input) can be converted into a form that can be accepted by the variable receiving the information.
The object property of COUT contains an insert operator <<, which inserts the information on the right into the output stream.
The << operator concatenates multiple output statements.
3.2 Introduction to classes
Classes are one of the core concepts of C++ object-oriented programming (OOP).
What is a class?
A class is a user-defined data type.
To define a class, you need to describe what information it can represent and what operations it can perform on the data.
A class definition describes a data format and its usage, while an object is an entity created according to the data format specification.
#### Two classes CIN and Cout
cin
Class:Cost object
cout
Class:Ostream class object
.Ostream class definition
Describes theOstream object
The saiddata
And what’s executed on itoperation
.
Two classes are not built into the compiler.
Note: Classes describe all the properties of a data type (including the operations performed with it), while objects are entities created from that description.
The way information is sent in C++
- Using class methods (function calls, etc.)
- Redefine the operator
4. The function
Two C++ functions
- Returns a value
- There is no return value
4.1 A function with a return value
A function that returns a value will generate a value that will be assigned to a variable or other expression for use.
- Called function: the function that is called
- Calling function: Contains the function called
- Return value: the value sent back
Parameters are the information sent to the function, and the return value is the value sent back from the function.
👉 Tips: for a C++ compiler, the argument types and return values of functions must be the same.
⚠️ note: C++ programs should provide prototypes for every function used in the program.
Function prototypes must end with a semicolon (;) The end. If the semicolon is omitted, the compiler considers it a function header and asks for the body that defines the function.
Do not confuse function prototypes with function definitions
Function prototypes only describe function interfaces.
The function definition contains the code for the function.
👉 Tips: Provide a prototype before using a function for the first time, usually before the main() function definition.
4.2 Function variants
- The keyword void is used in the prototype to specify the return type, indicating that the function does not return a value.
void bucks(double); Copy the code
- The void keyword does not take any arguments. If void is omitted and the parentheses are left blank, C++ interprets it as an implicit declaration that takes no arguments.
int rand(void); Copy the code
4.3 User-defined Functions
For library functions, you must provide prototypes before using them, usually before the main() definition.
- Function format
The function format is a function header with curly braces. CPP type functionname(arguementlist) {statements} C++ does not allow function definitions to be nested in another function; each function definition is independent.
- The function header
For example, the main() function header.
⚠️ Note: the keyword is a special word and cannot be used for other purposes.
Return cannot be used as a variable name, and double cannot be used as a function name.
4.4 User-defined functions that return values
A function that returns a value, using the keyword return to supply the return value and terminate the function.
Properties of a function
- There is a function header and a function body
- Accept a parameter
- Return a value
- You need a prototype
4.5 Using compiler instructions in multi-function programs
Four ways to give programs access to namespace STD
- will
using namespace std;
On theBefore the function is defined
, so that all functions of the file can be usedNamespace STD
All of the elements in. - will
using namespace std;
On theBefore a particular function is defined
To make the function usableIn the STD namespace
All elements of. - Use similar in specific functions
using std::cout
; Compile the command instead ofusing namespace std;
Let the function use the specified element, such as cout. - Out of use
Compile instruction using
, while in need of useNamespace STD
The element is, use prefixstd::
.
Github address: github.com/SolerHo/cpp…