开始学习C++
第一个C++程序
#include <iostream>
using namespace std;
int main() //function header
{
cout << "Come up and C++ ";
cout << endl; //start a new line
cout << "You won't regret it!";
return 0; //terminate main()
}
注意:使用cin和cout进行输入和输出的程序必须包含iostream文件(在有些的系统是iostream.h)
关于命名空间(using namespace std)
以下是这本书的最原始的说法
关于cout输出
在C++中用双引号括起来的一系列的字符叫做字符串,因为它是由一系列若干字符组合而成的。 << 符号表示该语句将这个字符串发送给cout,该符号指出了信息的流动路径。
cout是什么? 是一个预定义的对象,直到如何显示字符串、数字和单个字符等(先暂时稍微了解一下对象是一个类的特定的实例,而类定义了数据的存储和使用方式)
与其说是程序显示了一条信息,不如说是它将一个字符串插入到流(输出流)中。
关于endl
简单的说就是拥有重启一行的作用,当然C++还提供了换行的就是方法:C语言中的’\n’
C++语句的说明
#include <iostream>
using namespace std;
int main()
{
int carrot; //declare an integer variable
carrot = 25; //asign a value to the variable
cout << "I have";
cout << carrot; //display the value of the variable
cout << "carrots" << endl;
carrot = carrot - 1; //modify the variable
cout << "Crunch,crunch.Now I have " << carrot << "carrots" << endl;
return 0;
}
关于声明语句和变量
声明通常指出了要存储的数据类型和程序对存储在这里的数据使用的名称
至于变量为什么必须声明,我想下面这个给出了充分的理由