本章主要内容:
- C++程序一般格式
- cin 和cout
- 声明和使用变量
- 定义和调用简单函数
//程序清单2.1
//打印一条消息
#include <iostream> //头文件
int mian(void) //函数头
{ //{}花括号括起来为函数体
using namespace std;
cout << "Come up and C++ me some time." << endl;
cout << "You won't regret it!" << endl;
return 0; //结束函数
}
- 注释,前缀//表示,到行尾结束;此外还有/*开始到*/结束,跨越多行
- 预处理器编译指令#include,将头文件(包含文件)iostream文件添加到程序中
- 函数头:int main(void),必须有
- 编译指令 using namespace std,名称空间避免重名分不清
- 函数体,用{和}括起
- cout 显示消息;cin输入消息
- endl:换行;此外还有C符号:\n
- return 结束main()函数,只适用于main函数
- 语句以分号;结束
- C++中回车、空格和制表符作用一样
//程序清单2.2
//使用和显示变量
#include <iostream>
int mian(void)
{
using namespace std;
int carrots; //声明一个整型变量
carrots = 25; //变量赋值
cout << "I have " << carrots << " carrots." << endl; //打印变量的值
carrots = carrots - 1;
cout << "Crunch, crunch. Now I have " << carrots << " carrots.\n";
return 0;
}
- 首次使用时声明变量类型,让编译器为变量分配内存空间
- = 叫做赋值运算符,赋值从右向左进行
- 声明和赋值可以一起进行:int carrots = 25;
- cout 知道将carrots替换为当前值25进行输出
//程序清单2.3
//输入和输出
#include <iostream>
int main(void)
{
using namespace std;
int carrots;
cout << "How many carrots do you have?" << endl;
cin >> carrots;
cout << "Here are two more." << endl;
carrots = carrots + 2;
cout << "Now you have " << carrots << " carrots.\n";
return 0;
}
- cin 读取键盘输入,遇到空格、换行、制表符等停止
- 类之于对象就像类型之于变量,类抽象,对象具体,好比类代表所有著名演员,对象代表陈道明
- cout 是ostream类对象;cin是istream类对象
- C++提供两种发送消息的方式:一种是类方法(本质是函数调用);另一种是重新定义运算符(运算符重载),比如cin,cout
//程序清单2.4
//使用sqrt()函数
#include <iostream>
#include <cmath> sqrt()函数在cmath库中
int main(void)
{
using namespace std;
double area; //变量声明
cout << "Enter the floor area, in square feet, of your home: ";
cin >> area;
double side; //函数原型
side = sqrt(area);
cout << "That's the equivalent of a square " << side << " feet to the side.\n";
return 0;
}
- C++编译器知道函数的参数类型和返回值类型的方式是:函数原型
- 函数原型之于函数就像变量声明之于变量
- 原型结尾的分号;表明它是一条语句,而不是函数头
- 程序使用sqrt()函数市也必须提供原型,一种在源代码输入函数原型,另一种包含头文件cmath,最好使用头文件
- 不要混淆函数原型和函数定义,原型描述函数接口(发送和返回的信息),定义包含函数代码
- 应在首次使用函数之前提供原型
- double 双精度浮点型
double side;
side = sqrt(area);
//也可写为
double side = sqrt(area);
//这个过程叫做:初始化
//程序清单2.5
//自己定义函数
#include <iostream>
using namespace std; //两个函数都需要using编译指令,放在程序最前面
void simon(int); //函数原型,有参无返
int main(void)
{
simon(3); //函数调用
cout << "Pick an integer: ";
int count; //变量声明
cin >> count;
simon(count);
cout << "Done!" << endl;
return 0;
}
void simon(int n) //函数头,定义Simon函数
{
cout << "Simom says touch your toes " << n << " times." << endl;
} //函数没有返回值,不需要返回语句
- 每个C++程序必须有main()函数
- 可以使用函数名调用调用用户定义的函数
- 通常把函数原型放main()之前,函数代码放main()之后
- C++不允许函数嵌套,每个函数独立且平等
- Simon没有返回值,不能这样使用:simple = Simon(3)
- void simon(int n)函数头表明该函数接受一个int参数,不返回任何值
- 有返回值的函数,应使用return 0;提供返回值,并结束函数
//程序清单2.6
//英石和英镑转换
#include <iostream>
int stonetolb(int); //函数原型
int main(void)
{
using namespace std;
int stone; //变量声明
cout << "Enter the weight in stone: ";
cin >> stone;
int pounds = stonetolb(stone); //初始化
cout << stone << " stone = " << pounds << " pounds." << endl;
return 0;
}
int stonetolb(int sts)
{
return 14 * sts;
}
- 在main()中,程序使用cin给整型变量stone提供一个值,这个值作为参数传递给stonetolb()函数。在stonetolb()中,这个值被赋给变量sts,然后return 将14 * sts返回给main()。
- stonetolb()表达式也可写为:
int stonetolb(int sts) { int pounds = 14 * sts; return pounds; }
第一种简单,避免创建新变量,第二种更容易理解和修改
-
函数原型描述函数接口,及函数与程序其他部分如何交互。参数列表(形参列表)指出何种信息将被传递给函数,函数类型指出返回值类型。