The best C ++ basic grammar Tutorial In 2024, In this tutorial you can learn C ++ Program Structure,Compile & execute a C ++ program,C ++ semicolons & Block,C ++ Identifier,C ++ keywords,Trigraph,C ++ spaces,
C ++ programs can be defined as a collection of objects, these objects interact with each other by a method call. Now let's briefly look at what is classes, objects, methods, real-time variables.
Let's look at a simple piece of code, you can output the wordsHello World.
#include <iostream> using namespace std; // main() 是程序开始执行的地方 int main() { cout << "Hello World"; // 输出 Hello World return 0; }
Next we explain above, this procedure:
Let's look at how to save the source code in a file, and how to compile and run it. Below are simple steps:
$ g++ hello.cpp $ ./a.out Hello World
Make sure that your path is included g ++ compiler, and make sure to run it in the directory containing the source file hello.cpp.
You can also use the makefile to compile C / C ++ program.
In C ++, the semicolon is a statement terminator. That is, each statement must end with a semicolon. It indicates the end of a logical entity.
For example, here are three different statements:
x = y; y = y+1; add(x, y);
Block is a group of statements enclosed in curly braces logical connection. E.g:
{ cout << "Hello World"; // 输出 Hello World return 0; }
C ++ does not end with the end of the line as a symbol of identity, so you can place multiple statements on one line. E.g:
x = y; y = y+1; add(x, y);
Equivalent to
x = y; y = y+1; add(x, y);
C ++ identifier is used to identify the variables, functions, classes, modules, or any other user-defined project name. An identifier with the letters AZ or az or underscore _ Start, followed by zero or more letters, underscores and numbers (0-9).
Allowed punctuation characters, such as @, $ and% in the C ++ identifier. C ++ is a case-sensitive programming language. Thus, in C++, Manpower and manpowerare two different identifiers.
Here are some valid identifiers:
mohd zara abc move_name a_123 myname50 _temp j a23b9 retVal
The following table lists the C ++ reserved word. These words can not be reserved as a constant name, variable name, or other identifier names.
asm | else | new | this |
auto | enum | operator | throw |
bool | explicit | private | true |
break | export | protected | try |
case | extern | public | typedef |
catch | false | register | typeid |
char | float | reinterpret_cast | typename |
class | for | return | union |
const | friend | short | unsigned |
const_cast | goto | signed | using |
continue | if | sizeof | virtual |
default | inline | static | void |
delete | int | static_cast | volatile |
do | long | struct | wchar_t |
double | mutable | switch | while |
dynamic_cast | namespace | template |
Three-character group is a three-character sequence for another character representation, also known as the three-character sequence. Two three-character sequence is always a question mark at the beginning.
Three-character sequence is less common, but the C ++ standard allows certain characters to specify a three-character sequence. In order that there is no previous character on the keyboard, which is a method essential.
Three-character sequence can appear anywhere, including strings, character sequences, comments and preprocessor directives.
Listed below are the most commonly used three-character sequence:
三字符组 | 替换 |
---|---|
??= | # |
??/ | \ |
??' | ^ |
??( | [ |
??) | ] |
??! | | |
??< | { |
??> | } |
??- | ~ |
All compilers do not support the three groups of characters, in order to avoid confusion, do not recommend the use of three groups of characters.
Line contains only spaces, known as a blank line, possibly with comments, C ++ compiler to ignore it completely.
In C ++, the space for describing the blank, tabs, line breaks, and comments. Various parts of a space separate statements, so the compiler can identify the statement an element (such as int) where it ends, the next element where to start. Therefore, in the following statement:
int age;
Here, you must have at least one space character (usually a whitespace) between int and age, so the compiler to be able to distinguish between them. On the other hand, in the following statement:
fruit = apples + oranges; // 获取水果的总数
fruit and =, = or space character between apples and is not required, but in order to enhance readability, you can add some appropriate spaces as needed.