1、Ubuntu安装g++编译器
sudo apt-get update
sudo apt-get install g++
2、创建第一个文件main.cpp
#include <iostream>
using namespace std;
int main()
{
cout << "Hello, world!" << endl;
return 0;
}
3、编译
// 经过编译之后会在同目录下出现一个 a.out 文件
g++ main.cpp
4、运行文件
$ ./a.out
Hello, world!
5、使用-o选择指定生成的可执行程序的文件名
$ g++ helloworld.cpp -o helloworld
$ ./helloworld
Hello, World!
6、C++基本数据类型
// 1个字节
char // -128~127或0~255
unsigned // 0~255
signed // -128~127
// 2个字节
short int
unsigned short int
signed short int
// 4个字节
int
unsigned int
signed int
float
// 8个字节
long int
signed long int
unsigned long int
double
// 16个字节
long double
wchar_t // 2个或4个字节
7、取别名,typedef声明
// 突然想起了初学C时候老是会打错,就用这个例子吧
typedef true ture; // 表示代码中出现的ture表示true
8、枚举类型
// 变量的值可能有几个,这个变量也只可能是定义的这几个值
// 其中enum表示枚举函数,color表示枚举名,red,green表示枚举的值,通过为0、1、2递增
// c表示枚举变量
enum color { red, green, blue } c;
c = blue;
enum color { red, green=5, blue };
#include<iostream>
int main(){
enum color {red,green,blue} c;
c = blue;
if (c== blue){
std::cout<<"1";
}
return 0;
}
// out:1
#include<iostream>
int main(){
enum color {red,green,blue} c;
c = blue;
if (c== blue){
std::cout<<"blue' value is 2";
}
return 0;
}
// out: blue' value is 2
9、# define预处理器
#include <iostream>
using namespace std;
#define LENGTH 10
#define WIDTH 5
#define newline'100'
int main()
{
int area;
area = LENGTH * WIDTH;
cout << area;
cout << newline;
return 0;
}
// output:501
10、const关键字
#include <iostream>
using namespace std;
int main()
{
const int LENGTH = 10;
const int WIDTH = 5;
const char NEWLINE = '\n';
int area;
area = LENGTH * WIDTH;
cout << area;
cout << NEWLINE;
return 0;
}
// 50
11、static存储类
// static修饰的局部变量可以在函数调用之间保持局部变量的值
// static用于全局变量时,会使变量的作用域限制在声明它的文件内
// 当 static 用在类数据成员上时,会导致仅有一个该成员的副本被类的所有对象共享