什么是C ++? (What is C++?)
- C++ is a general purpose programming language which has been used since the 1990’s C ++是自1990年代以来一直使用的通用编程语言
- It was designed by Bjarne Stroustrup under with the name “C with classes”. 它由Bjarne Stroustrup设计,名称为“ C with classes”。
- It is a version of C that includes Object-Oriented elements, including classes and functions. 它是C的一个版本,其中包含面向对象的元素,包括类和函数。
- It is considered one of the biggest programming languages, as you can see in the following image: 如以下图所示,它被认为是最大的编程语言之一:
source: Github
资料来源:Github
您的第一个C ++程序 (Your First Program in C++)
#include <iostream>
using namespace std;
int main()
{
cout << "Hello World" << endl;
return 0;
}
该程序的输出将简单地为: (The Output of this program will simply be :)
Hello World!
Now, let’s break down the code:
现在,让我们分解一下代码:
第1和2行 (Lines 1 and 2)
#include <iostream>
using namespace std;
- The first line tells the computer to use the “iostream” header file for this specific program . A header file is a seperate file with prewritten C++ code. There are many other header files which are requireed for a specific program to run properly. Some of them are : math , vector and string. Header files are generally represented by a “.h” extension (you don’t need to add .h when including C++ standard library files) 第一行告诉计算机将“ iostream”头文件用于此特定程序。 头文件是带有预先编写的C ++代码的单独文件。 要使特定程序正常运行,还需要许多其他头文件。 其中一些是:math,vector和string。 头文件通常以“ .h”扩展名表示(包括C ++标准库文件时无需添加.h)
iostream
stands for input-output stream . The “iostream” file contains code for allowing the computer to take input and generate an output, using the C++ language.iostream
代表输入输出流。 “ iostream”文件包含用于允许计算机使用C ++语言进行输入并生成输出的代码。The second line tells the computer to use the standard namespace which includes features of standard C++. You could write this program without this line, but you’d have to use
std::cout
instead ofcout
andstd::endl
instead ofendl
on line 4. It makes the code more readable and our lives as programmers easier.第二行告诉计算机使用标准名称空间,其中包括标准C ++的功能。 您可以在没有此行的情况下编写此程序,但是必须在第4行上使用
std::cout
代替cout
和std::endl
而不是endl
。这使代码更易读,并且我们更容易成为程序员。
3号线和4号线 (Line 3 and 4)
int main()
{
C++ starts execution of a program from the -main function-
int main()
. During execution , the computer starts running the code from every line from{
(opening bracket) till}
(closing bracket) NOTE : Every function starts with an opening curly brace ”{” and ends with a closing curly brace ”}“.C ++从-main function-
int main()
开始执行程序。 在执行过程中,计算机从{
(右括号)到}
(右括号)的每一行开始运行代码。 注意:每个函数都以一个大括号“ {”开头,以一个大括号“}”结尾。- Line 4 indicates the start of the main() function. 第4行指示main()函数的开始。
第5、6和7行 (Lines 5, 6 & 7)
cout << "Hello World" << endl;
return 0;
}
The word
cout
in C++ is used to output.C ++中的单词
cout
用于输出。It is followed by
<<
, the insertion operator .其后是插入运算符
<<
。Whatever is in the double quotes
""
is printed . Certain special characters have a different syntax for print statements双引号中的任何内容都将打印
""
。 某些特殊字符的打印语句语法不同Now to print any other kind of data , you have to add
<<
.现在要打印任何其他类型的数据,您必须添加
<<
。
Challenge: Try to change Hello World to any other sentence or word(s). What will be the output ?
挑战:尝试将“ Hello World”更改为任何其他句子或单词。 输出是什么?
endl
is a reserved word when using the C++ language to end this line and go to the next line during output . - cout stands for “console output”当使用C ++语言结束此行并在输出期间转到下一行时,
endl
是保留字。 -cout代表“控制台输出”Finally, finish the command with a semicolon
;
.最后,以分号结束命令
;
。
NOTE : Every command except the main function definition and the #include directive needs to be ended by the semicolon. Without a ”;” , you may encounter an error.
注意:除主函数定义和#include指令以外的所有命令都必须以分号结尾。 没有 ”;” ,您可能会遇到错误。
return 0;
safely terminates the current function i.e. ‘main()’ in this case and since no function follows after ‘main()’ the program is terminated.return 0;
在这种情况下,安全地终止当前函数,即“ main()”,并且由于在“ main()”之后没有函数跟随程序终止。Don’t forget to tell the computer that this is end of the main() function. To do this , you add the closing curly brace ”}“. You will encounter an error before program execution if you do not include the } .
不要忘记告诉计算机这是main()函数的结尾。 为此,可以添加右花括号“}”。 如果不包含},则会在程序执行前遇到错误。
代码应如下所示: (The code should look something like this:)
Programmers use a Hello World program (like this one) as a ritual on using a new programming language. It is a symbol of good luck.You have finished coding your first C++ program and have understood most of the code you have written/typed. CONGRATULATIONS!
程序员使用Hello World程序(如该程序)作为使用新编程语言的一种习惯。 这是好运的象征。 您已经完成了第一个C ++程序的编码,并且已经理解了您编写/键入的大多数代码。 恭喜!
Good Luck to all of you and happy coding! :)
祝大家好运,并祝您编程愉快! :)
Happy Coding ! :)
快乐编码! :)
Feel free to ask any questions on FreeCodeCamp’s GitHub page or FreeCodeCamp’s Forum.
随时在FreeCodeCamp的GitHub页面或FreeCodeCamp的论坛上提问。
You may need some software to write and execute C++ code. I recommend using CodeBlocks. There’s a download link below :
您可能需要一些软件才能编写和执行C ++代码。 我建议使用CodeBlocks。 下面有一个下载链接:
Download Link : Download Here
下载链接: 在这里下载
- Click the link with the GNU/GCC compiler for windows. This will not require an additional installation 单击用于Windows的GNU / GCC编译器的链接。 这将不需要额外的安装
Other alternatives could be visual studio, using a compiler or an online IDE such as Cloud9 or repl.it
其他替代方案可以是Visual Studio,使用编译器或在线IDE(例如Cloud9或repl.it)。
Link #2 for Mac : Download for Mac #2 here
Mac的链接2: 在此处下载Mac的2
翻译自: https://www.freecodecamp.org/news/the-c-plus-plus-programming-language/