2.1
基础知识整理:
#include : 预处理器编译指令【编译程序时自动运行,预处理操作:在源代码被编译之前,替换或添加文本】
int main():函数头
using namespace:编译指令
// :注释
/* */:注释(C 风格)
#include<iostream>与#include"iostream" 区别:
<iostream> 是C++标准库的头文件,编译器会直接在系统路径下搜索该头文件
“iostream” 是在当前工程或文件路径下的用户自定义头文件。编译器会先在当前路径下搜索该头文件,如果找到就包含进来。如果当前路径下没有找到该头文件,编译器会在系统路径下搜索该头文件。
不需要main()情况:
- DLL模块:动态链接库,不是独立的程序,不需要main()
- 用于专用环境
- 框架程序提供非标准函数
C/C++旧时风格的头文件扩展名 .h(math.h)
C++新式风格的头文件没有扩展名 (iostream)
对于新式风格,增加名称空间编译指令(using namespace XXX)来使 头文件中的定义对程序可用
C++特性:名称空间支持【让厂商能够将产品封装在一个叫做名称空间的单元中】
在新式风格下:
对于不同名称空间的同名函数,可以:
名称空间 :: 函数名 进行区分
如:rcsc :: test() | std :: test()
此外:
using namespace std;
cout << "Come";
cout << endl;
等同于
std::cout << "Come";
std::cout << std::endl;
cout << "Come";
【将字符串插入到输出流中】
<< : 插入运算符
C语言符号:【\n】
生成一个空行,与 endl 等效
区别:
endl :确保程序继续运行前刷新输出(将其立即显示在屏幕上)
\n :无法保证,有时可能在输入信息后才能显示
C++语句分割标准:仅有 分号 ;
2.2
代码2-2:
#include <iostream>
int main()
{
using namespace std;
int carrots=25; // 将声明语句与其他语句隔行分开, C风格
cout << "I have " << carrots << " carrots" << endl;
carrots = carrots - 1;
cout << "Crunch, crunch. Now I have " << carrots << " carrots." << endl;
// 仅按下回车键才能继续
cin.get();
return 0;
}
【int carrots 】这句声明变量语句提供了两条信息:
- 所需内存大小【int 类型】
- 所分配的内存名称【carrots】
声明区别:
在C中:所有变量声明通常位于函数或过程开始位置
在C++中:在首次使用变量前声明
连续赋值运算符:
从右往左进行
a = b = c = 88
cout 与 printf:
cout << "I have " << carrots; //存在格式转换 int to str
//无格式转换
printf("Type str : %s\n", "25");
printf("Type int : %d\n", 25);
2.3
代码2-3
#include <iostream>
int main()
{
using namespace std;
int carrots;
cout << "How many carrots are there?" << endl;
cin >> carrots;
cout << "Here two more" << endl;
carrots = carrots + 2;
cout << "Now you have:" << carrots << " carrots." << endl;
// 仅按下回车键才能继续
cin.get();// 由于此例中存在回车输入,(在输入 carrots 时按下回车被获取到), 会直接运行结束
return 0;
}
cin >> carrots;
从键盘获取输入,赋值给变量 carrots,>> 表示从输入流中抽取字符
cout << "Now you have:" << carrots << " carrots." << endl;
新特性:使用 cout 拼接合并输入
需要注意:
cout << "Here two more" << endl;//1
carrots = carrots + 2;
cout << "Now you have:" << carrots << " carrots." << endl;//2
注释1的输出语句与注释2的输出语句在同一行中,因为 cout 的输出紧跟在前一条 cout 的输出后面(在输出流中),忽略1,2间的其他语句。
类简介
cout 是一个 ostream 类对象【ostream 类定义描述了ostream 对象表示的数据以及可以对他执行的操作】
cin 是一个 istream 类对象
ostream 类与istream 类在 iostream 中被定义
注意:类描述一种数据类型的全部属性,包括可执行的操作,对象是根据这些描述创建的实体
C++提供两种发送消息的方式:
- 类方法(本质为函数调用)
- 重新定义运算符(cin cout采取此种方法)
2.4
函数分类:
- 有返回值
- 无返回值
x = sqrt(6.25)//sqrt(6.25) 称为函数调用,并返回值(函数中 return 语句)赋值给 x
函数定义:
double sqrt(double);//函数原型
原型结尾分号表明是一条语句,使其成为原型,而不是函数头
若省略分号,将被编译器理解为函数头,要求提供其函数体
提供函数原型方法:
- 源代码文件中输入函数原型
- 包含头文件 (cmath/math.h)
代码2-4:
#include <iostream>
#include <cmath>
int main()
{
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 is the equivalent of a square " << side << " feet to the side. " << endl;
cout << "How fascinating!" << endl;
return 0;
}
使用库函数:
C++库函数存储在库文件中。编译器编译程序时必须在库文件中搜索使用的函数
(编译器倾向于给函数名添加下划线前缀【_sqrt()】提示它们对程序具有最后发言权)
若编译器不能自动搜索数学库:
使用 -lm 链接数学库
UNIX:
CC sqrt.C -lm
Linux:
g++ sqrt.cpp -lm
函数变体:
double pow(double, double);// 使用多个参数
int rand(void);// 不接受任何参数
void bucks(double);// 无返回值
在有些语言中,有返回值的函数被称为函数,无返回值的函数被称为过程或子程序
但在C/C++中都称为函数
对于库函数,在使用前必须提供其原型,通常把原型放在 main() 定义之前
代码2-5:
// ourfunc.cpp -- defining your own function
#include <iostream>
void simon(int);
int main()
{
using namespace std;
simon(3);
cout << "Pick an integer : ";
int count;
cin >> count;
simon(count);
cout << "Done!" << endl;
return 0;
}
void simon(int n)
{
using namespace std;
cout << "Simon says touch your toes " << n << " times." << endl;
}
和C一样,C++不允许将函数定义嵌套在另一个函数定义中,每个函数定义都是独立,平等的
关键字:语言专用名称,无法声明一样的变量来使用,如不能声明 void 命名的变量
代码2-6:
// convert.cpp -- converts stone to pounds
#include <iostream>
int stonetolb(int);
int main()
{
using namespace std;
int stone;
cout << "Enter the weight in stone: ";
cin >> stone;
int pounds = stonetolb(stone);
cout << stone << " stone = ";
cout << pounds << " pounds." << endl;
return 0;
}
int stonetolb(int sts)
{
return 14 * sts;
}
函数原型描述了函数接口,即函数如何与程序的其他部分交互。
访问名称空间的方法:
- 将 using namespace std; 放在所有函数定义之前,使其具有全局属性
- 放在特定的函数中
- 在特定函数中使用 using std::cout 编译指令
- 完全不使用编译指令 using ,使用前缀 std::
如:std::cout << " 123456 " << std:;endl;
函数命名:
Myfunction( )
myfunction( )
myFunction( )
my_function( )
my_funct( )
最好使用驼峰命名法:myFunction( )