Cplusplus 简明教程
C++ Basic Syntax
当我们考虑一个 C++ 程序时,可以将其定义为通过调用彼此的方法进行通信的对象集合。现在让我们简要了解一下类、对象、方法和即时变量的含义。
When we consider a C++ program, it can be defined as a collection of objects that communicate via invoking each other’s methods. Let us now briefly look into what a class, object, methods, and instant variables mean.
-
Object − Objects have states and behaviors. Example: A dog has states - color, name, breed as well as behaviors - wagging, barking, eating. An object is an instance of a class.
-
Class − A class can be defined as a template/blueprint that describes the behaviors/states that object of its type support.
-
Methods − A method is basically a behavior. A class can contain many methods. It is in methods where the logics are written, data is manipulated and all the actions are executed.
-
Instance Variables − Each object has its unique set of instance variables. An object’s state is created by the values assigned to these instance variables.
C++ Program Structure
让我们看一个简单的代码,它将打印单词 Hello World。
Let us look at a simple code that would print the words Hello World.
#include <iostream>
using namespace std;
// main() is where program execution begins.
int main() {
cout << "Hello World"; // prints Hello World
return 0;
}
让我们来看看上面程序的不同部分 −
Let us look at the various parts of the above program −
-
The C++ language defines several headers, which contain information that is either necessary or useful to your program. For this program, the header <iostream> is needed.
-
The line using namespace std; tells the compiler to use the std namespace. Namespaces are a relatively recent addition to C++.
-
The next line '// main() is where program execution begins.' is a single-line comment available in C++. Single-line comments begin with // and stop at the end of the line.
-
The line int main() is the main function where program execution begins.
-
The next line cout << "Hello World"; causes the message "Hello World" to be displayed on the screen.
-
The next line return 0; terminates main( )function and causes it to return the value 0 to the calling process.
Compile and Execute C++ Program
我们来看看如何保存文件、编译和运行程序。请按照下面给出的步骤操作 −
Let’s look at how to save the file, compile and run the program. Please follow the steps given below −
-
Open a text editor and add the code as above.
-
Save the file as: hello.cpp
-
Open a command prompt and go to the directory where you saved the file.
-
Type 'g++ hello.cpp' and press enter to compile your code. If there are no errors in your code the command prompt will take you to the next line and would generate a.out executable file.
-
Now, type 'a.out' to run your program.
-
You will be able to see ' Hello World ' printed on the window.
$ g++ hello.cpp
$ ./a.out
Hello World
确保 g++ 在你的路径中,并且你在包含文件 hello.cpp 的目录中运行它。
Make sure that g++ is in your path and that you are running it in the directory containing file hello.cpp.
你可以使用 makefile 编译 C/C++ 程序。有关更多详细信息,你可以查看我们的 'Makefile Tutorial' 。
You can compile C/C++ programs using makefile. For more details, you can check our 'Makefile Tutorial'.
Semicolons and Blocks in C++
在 C++ 中,分号是一个语句终止符。也就是说,每个单独的语句都必须以分号结尾。它表示一个逻辑实体的结尾。
In C++, the semicolon is a statement terminator. That is, each individual statement must be ended with a semicolon. It indicates the end of one logical entity.
例如,以下三个不同的语句 −
For example, following are three different statements −
x = y;
y = y + 1;
add(x, y);
一个块是一组逻辑上连接的语句,这些语句被开放和闭合花括号包围。例如 −
A block is a set of logically connected statements that are surrounded by opening and closing braces. For example −
{
cout << "Hello World"; // prints Hello World
return 0;
}
C++ 不将行尾视为终止符。因此,你将语句放在一行的什么位置并不重要。例如 −
C++ does not recognize the end of the line as a terminator. For this reason, it does not matter where you put a statement in a line. For example −
x = y;
y = y + 1;
add(x, y);
与以下内容一致
is the same as
x = y; y = y + 1; add(x, y);
C++ Identifiers
C++ 标识符是用于标识变量、函数、类、模块或任何其他用户定义项的名称。标识符以字母 A 到 Z 或 a 到 z 或下划线 (_) 开头,后面跟零个或多个字母、下划线和数字(0 到 9)。
A C++ identifier is a name used to identify a variable, function, class, module, or any other user-defined item. An identifier starts with a letter A to Z or a to z or an underscore (_) followed by zero or more letters, underscores, and digits (0 to 9).
C 语言不允许标识符中出现诸如 @、$ 和 % 等标点字符。C 语言是一种区分大小写的编程语言。因此,在 C++ 中, Manpower 和 manpower 是两个不同的标识符。
C does not allow punctuation characters such as @, $, and % within identifiers. C is a case-sensitive programming language. Thus, Manpower and manpower are two different identifiers in C++.
以下是一些可接受标识符的示例−
Here are some examples of acceptable identifiers −
mohd zara abc move_name a_123
myname50 _temp j a23b9 retVal
C++ Keywords
以下列表显示了 C++ 中的保留字。这些保留字不能用作常量、变量或任何其他标识符名称。
The following list shows the reserved words in C++. These reserved words may not be used as constant or variable or any 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 |
Trigraphs
几个字符具有称为三字符序列的备用表示形式。三字符序列是一个表示单个字符的三字符序列,该序列始终以两个问号开头。
A few characters have an alternative representation, called a trigraph sequence. A trigraph is a three-character sequence that represents a single character and the sequence always starts with two question marks.
三字符序列在它们出现的任何地方进行扩展,包括在字符串文字和字符文字、注释和预处理程序指令中。
Trigraphs are expanded anywhere they appear, including within string literals and character literals, in comments, and in preprocessor directives.
以下是使用最频繁的三字符序列−
Following are most frequently used trigraph sequences −
Trigraph |
Replacement |
??= |
# |
??/ |
\ |
??' |
^ |
??( |
[ |
??) |
] |
??! |
|
??< |
|
{ |
??> |
} |
??- |
所有编译器都不支持三字符序列,并且不建议使用它们,因为它们容易混淆。
All the compilers do not support trigraphs and they are not advised to be used because of their confusing nature.
Whitespace in C++
仅包含空白(可能带注释)的一行被称为空白行,C++ 编译器完全忽略它。
A line containing only whitespace, possibly with a comment, is known as a blank line, and C++ compiler totally ignores it.
空白是在 C++ 中用于描述空白、制表符、换行符和注释的术语。空白将语句的一部分与另一部分分开,并使编译器能够识别语句中一个元素(如 int)的结尾和下一个元素的开头。
Whitespace is the term used in C++ to describe blanks, tabs, newline characters and comments. Whitespace separates one part of a statement from another and enables the compiler to identify where one element in a statement, such as int, ends and the next element begins.
Statement 1
int age;
在上面的语句中,int 和 age 之间必须至少有一个空白字符(通常是空格),编译器才能区分它们。
In the above statement there must be at least one whitespace character (usually a space) between int and age for the compiler to be able to distinguish them.
Statement 2
fruit = apples + oranges; // Get the total fruit
在上述语句 2 中,fruit 和 = 之间或 = 和 apples 之间不需要空白字符,但是如果你希望出于可读性目的,可以自由地包括一些。
In the above statement 2, no whitespace characters are necessary between fruit and =, or between = and apples, although you are free to include some if you wish for readability purpose.