2.1 从Hello World 谈起
2.1.1 函数
2.1.2 main()函数
2.1.3 (内建)类型
2.1.4 语句
2.1.5 注释
2.2 系统IO
2.2.1 添加头文件
利用 #include 来添加头文件时:
- 既可以用 < > 尖括号:从系统库文件中找,一般是标准库(没有.h这种后缀)
- 也可以用 ” “ 双引号:默认在当前目录下寻找头文件,一般引用自己写的头文件。
2.2.2 iostream
2.2.3 名字空间
2.2.4 C/C++系统IO比较
2.3 猜数字与控制流
2.3.1 if 语句
2.3.2 == 与 = 操作
2.3.3 while 语句
例子:猜数字
#include<iostream>
int main()
{
while(1)
{
int x=0;
std::cout << "Please input a number(0-10):";
std::cin >> x;
if (5==x)
{
std::cout << "You are right!\n\n";
break;
}
else
{
std::cout << "You are wrong!!\n\n";
}
}
system("pause");
}
运行结果:
2.4 结构体与自定义数据类型
例子:自定义Point类型
#include<iostream>
struct Point
{
int x;
int y;
void incfun()
{
x = x + 1;
y = y + 1;
}
};
Point fun(Point p)
{
p.x = p.x + 1;
return p;
}
int main()
{
Point p;
p.x = 10;
p.y = 10;
p = fun(p);
std::cout << p.x << " " << p.y << "\n";
p.incfun();
std::cout << p.x << " " << p.y << "\n";
system("pause");
}
运行结果: