C++ Primer Plus(第6版)学习笔记 第2章

预处理器编译指令:
#include <iostream>

主函数头应使用:
int main()
在主函数末尾如果没有遇到返回语句,则默认加入:
return 0;
作为结尾。

如果使用iostream作为头文件,则应加入名称空间编译指令来使定义对程序可用:
using namespace std;

如果string是一个字符串,则下面的代码将显示该字符串:
cout << string

cout与cin的综合运用:
int carrots;
cout << "How many carrots do you have?" << endl; //endl为转行控制符
cin >> carrots;
cout << "Here are two more.\n";
carrots = carrots + 2;
cout << "Now you have " << carrots << " carrots." << endl;
cin.get(); //输入数字并按回车键时读取输入
cin.get(); //使程序暂停,直到按下回车键

运行结果:
How many carrots do you have?
12
Here are two more.
Now you have 14 carrots.

用户定义函数:
1.在程序前端先声明所要定义的函数:
int size (int, int) ;
返回值的类型 函数名 函数输入1的类型 函数输入2的类型 语句结束
2.在程序中调用时:
x = size (6, 8) ;
函数返回的值被赋给x 函数名 传递给函数的参数信息,注意逗号后的空格         语句结束
3.在main函数后给出所声明的函数的定义:
int size (int n, int m)
{
return n * m;
}
4.额外注意事项:
函数头为void时不需要return返回语句;
如果在定义中出现了cout等语句时,定义的开头应加上:
using namespace std;

猜你喜欢

转载自blog.csdn.net/SilverAkito/article/details/80060795