在开始使用c++语言之前,您需要学习如何编写,编译和运行第一个c++程序。首先要安装gcc编译器:
安装gcc和g++:
yum -y install gcc automake autoconf libtool make
安装g++:
yum install gcc gcc-c++
要编写第一个c++程序,打开c++控制台或编辑器,并编写以下代码:
#include <iostream.h>
using namespace std;
int main() {
cout << "welcome to c++ programming.\n";
return 0;
}
把上述代码保存在 first.cpp
的文件中。
#include <iostream.h>
包括标准输入输出库函数。它提供cin
和cout
方法分别从输入和写入到输出。int main()
这里的main()
函数是c++语言中每个程序的入口点。 int
关键字指定它返回一个int
类型的值。
cout << "welcome to c++ programming."
。 用于打印字符串“welcome to c++ programming.
”在控制台上。
有关 如何编译和运行c++程序 问题,可参考:linux编译运行c++程序章节。
这里简单的编译并执行,如下命令 -
[h3@localhost cpp]$ g++ fist.cpp
[h3@localhost cpp]$ ./a.out
welcome to c++ programming.
[h3@localhost cpp]$