目录
1.1 C++简介
1.2 C++简史
1.2.1 C语言
1.2.2 C语言的编译原理
1.2.3 面向对象编程
1.2.4 C++和范型编程
1.2.5 C++的起源
1.3 可移植性和标准
1.3.1 C++的发展
1.3.2 本书遵循的C++标准
1.4 程序创建的技巧
1.4.1 创建源代码文件
1.4.2 编译和链接
1.5 总结
2.1 进入C++
2.1.1 main()函数
2.1.2 C++注释
2.1.3 C++预处理器和iostream文件
2.1.4 头文件名
2.1.5 名称空间
2.1.6 使用cout进行C++输出
2.1.7 C++源代码的格式化
2.2 C++语句
2.2.1 声明语句和变量
2.2.2 赋值语句
2.2.3 cout的新花样
2.3 其他C++语句
2.3.1 使用cin
2.3.2 使用cout进行拼接
2.3.3 类简介
2.4 函数
2.4.1 使用有返回值的函数
2.4.2 函数变体
2.4.3 用户定义的函数
2.4.4 用户定义的有返回值的函数
2.4.5 在多函数程序中使用using编译指令
2.5 总结
C语言和C++的发展历史和基本原理
过程性编程和面向对象编程
C++是如何在C语言的基础上添加面向对象概念的
C++是如何在C语言的基础上添加泛型编程概念的
编程语言标准
创建程序的技巧
1.1 C++简介
C语言代表的过程性语言
C++在C语言基础上添加的类代表的面向对象语言
C++模版支持的泛型编程
1.2 C++简史
1.2.1 C语言
汇编语言依赖特定计算机处理器的内部机器语言,要将汇编程序移植到另一种计算机上,必须使用不同的汇编语言重新编写程序。
针对不同硬件,一种被称为编译器的特殊程序将高级语言翻译成特定计算机的内部语言。
将低级语言的效率、硬件访问能力和高级语言的通用性、可移植性融合在一起,开发了C语言。
1.2.2 C语言的编译原理
过程性语言。强调算法方面,即菜谱指定厨师做蛋糕时遵循的步骤
结构性编程。良好的结构分支,如for、while、if else..
自顶向下。开发函数来表示各个任务模块
1.2.3 面向对象编程
OOP试图语言来满足问题的要求,其理念是设计与问题的本质特性相对应的数据格式。
OOP将数据和方法合并为类定义。类是规范,规定了可使用哪些数据来表示对象以及可以对这些数据执行哪些操作。对象是根据这种规范构造的特定数据结构。
1.2.4 C++和泛型编程
重用代码和抽象通用概念的技术更简单,OOP强调的是编程的数据方面,泛型强调独立于特定数据类型。
1.2.5 C++的起源
1.3 可移植性和标准
两个障碍:
1.硬件特定的程序是不可移植的
2.语言上的差异
ANSI
C++98表示C++98/C++2003
C++11
1.3.1 C++的发展
1.3.2 本书遵循的C++标准
1.4 程序创建的技巧
1.4.1 创建源代码文件
1.4.2 编译和链接
1.UNIX编译和链接
CC
2.Linux编译和链接
g++
3.Windows命令行编辑器
Cygwin MinGW
4.Windows编译器
5.Macintosh上的C++
1.5 总结
创建C++程序
C++程序的一般格式
#include编译指令
main()函数
使用cout对象进行输出
在C++程序中加入注释
何时以及如何使用endl
声明和使用变量
使用cin对象进行输入
定义和使用简单函数
2.1 进入C++
// myfirst.cpp--displays a message
#include <iostream> // a PREPROCESSOR directive
int main() // function header
{ // start of function body
using namespace std; // make definitions visible
cout << "Come up and C++ me some time."; // message
cout << endl; // start a new line
cout << "You won't regret it!" << endl; // more output
// If the output window closes before you can read it,
// add the following code:
// cout << "Press any key to continue." <<endl;
// cin.get();
return 0; // terminate main()
}
[wlsh@wlsh-MacbookPro] chapter_2$ g++ myfirst.cpp
[wlsh@wlsh-MacbookPro] chapter_2$ ./a.out
Come up and C++ me some time.
You won't regret it!
2.1.1 main()函数
C++句法要求main()函数的定义以函数头int main()开始
特殊的是main()被启动代码调用,而启动代码是由编译器添到程序中的,是程序和操作系统之间的桥梁。事实上,该函数头描述的是main()和操作系统之间的接口。
C++标准格式
2.1.2 C++注释
2.1.3 C++预处理器和iostream文件
#include <iostream>
using namespace std;
目的:简化程序
预处理器将iostream文件内容添加到程序中。
2.1.4 头文件名
C++对老式的头文件保留了扩展名,而C++头文件则没有扩展名
2.1.5 名称空间
C++特性之名称空间,让厂商能够将将其产品封装在一个叫做名称空间的单元中,用名称空间的名称来指出想使用哪个厂商的产品。如Microflp::wanda()、Pisscine::wanda()
名称空间std,为了不必麻烦,using编译指令,可以使用std名称空间中定义的名称,而不必使用std::前缀
using namespace std
进一步更好的做法
using std::cout;
2.1.6 使用cout进行C++输出
cout << "lalaplus.";
"" 双引号括起的是字符串
<< 该语句将把这个字符串发给cout,该符号指出信息流动的路径
cout是预定义的对象,优点不用了解对象的内部情况,就可以使用它,只需要知道它的接口,,即如何使用它
编译器通过上下文识别运算符重载
控制符(manipulator)
cout << “lalalaplus.\n”;
cout << “lalalaplus." << endl;
老式的换行风格。
end:重起一行,在输出流中插入endl将导致屏幕光标移到下一行还头。
差别:endl确保程序继续运行前刷新输出(将其立即显示在屏幕上)而“\n”不能保证
2.1.7 C++源代码的格式化
在C和C++中,不能把空格、制表符或回车放在元素中间,也不能把回车放在字符串中间。
不可分割的元素叫标记(token),通常使用空格、制表符将两个标记分开。
return0; 错误
return(0);
intmain();错误
int main ();
2.2 C++语句
// carrots.cpp -- food processing program
// uses and displays a variable
#include <iostream>
int main()
{
using namespace std;
int carrots; // declare an integer variable
carrots = 25; // assign a value to the variable
cout << "I have ";
cout << carrots; // display the value of the variable
cout << " carrots.";
cout << endl;
carrots = carrots - 1; // modify the variable
cout << "Crunch, crunch. Now I have " << carrots << " carrots." << endl;
// cin.get();
return 0;
}
[wlsh@wlsh-MacbookPro] chapter_2$ g++ carrots.cpp
[wlsh@wlsh-MacbookPro] chapter_2$ ./a.out
I have 25 carrots.
Crunch, crunch. Now I have 24 carrots.
2.2.1 声明语句和变量
int carrots;
信息:需要的内存及内存单元的名称。编译器负责分配和标记内存的细节。
即carrots来标识存储在该内存单元中的值。为变量,值可以修改
C++所有变量都必须声明。在首次使用变量前声明就可以了。
2.2.2 赋值语句
2.2.3 cout的新花样
cout << carrots
cout将carrots替换为其当前值23,然后把值转换为合适的输出字符。
整数25存储为二进制数,字符串25存储的是字符2和字符5的编码,这是cout的智能之处
源自C++的面向对象特性,插入运算符(<<)将根据其后的数据类型相应地调整行为,这是一个运算符重载的例子。
与C语言的复杂性不通,必须用特殊代码指出是打印字符串还是整数,假如想打印字符串,而又提供整数,printf()不够精密,发现不了错误。
print("Printing a string: %s\n","25");
print("Printing a integer: %d\n",25);
2.3 其他C++语句
// getinfo.cpp -- input and output
#include <iostream>
int main()
{
using namespace std;
int carrots;
cout << "How many carrots do you have?" << endl;
cin >> carrots; // C++ input
cout << "Here are two more. ";
carrots = carrots + 2;
// the next line concatenates output
cout << "Now you have " << carrots << " carrots." << endl;
// cin.get();
// cin.get();
return 0;
}
[wlsh@wlsh-MacbookPro] chapter_2$ g++ getinfo.cpp
[wlsh@wlsh-MacbookPro] chapter_2$ ./a.out
How many carrots do you have?
12
Here are two more. Now you have 14 carrots.
2.3.1 使用cin
cin.get()在您输入数字并按Enter键时读取输入。
信息流从cin流向carrots,C++将输出看作是流出程序的字符流一样,它将输入看作是流入程序的字符流。
iostream将cin定义为一个表示流的对象,在运算符右侧插入一个变量,在接受抽取的信息用符号(<<>>)指示信息流的方向。
2.3.2 使用cout进行拼接
cout << "Now you have " << carrots << " carrots." << endl;
cout语句的输出紧跟在前一条cout语句的输出后面,即使两条cout语句之前有其他语句。
这些对象都很智能化,cin知道如何将输入流中的信息转化为double类型,cout知道如何将double类型的插入到输出流中。
2.3.3 类简介
类是用户定义的数据类型。
类描述的是数据格式及其用法,而对象是根据数据格式规范创建的实体。
类之于对象 = 类型之于变量。
cout是一个ostream类对象
cin是一个istream类对象
这些类定义位于iostream文件中,没有内置到编译器中。
总之,类描述了一种数据类型的全部属性(它执行的操作),对象是根据这些描述创建的实体。
2.4 函数
2.4.1 使用有返回值的函数
在使用函数之前,C++编译器必须知道函数的参数类型和返回值类型。
函数原型之于函数就像变量声明之于变量--指出涉及的类型。
double sqrt(double);
分号表明它是一条语句,这使得它是一个原型,而不是函数头。
提供原型的两种方法:
1.源代码文件中输入函数原型
2.包含头文件cmath中定义了原型。
函数原型 不等于 函数定义。 原型只是描述接口,描述的是发送给函数的信息和返回的信息;而定义包含函数的代码。
另外,C++允许在程序的任何地方声明新变量,还允许在创建变量时对它进行复制,这叫初始化(initialization)
double side = sqrt(area);
// sqrt.cpp -- using the sqrt() function
#include <iostream>
#include <cmath> // or math.h
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's the equivalent of a square " << side
<< " feet to the side." << endl;
cout << "How fascinating!" << endl;
// cin.get();
// cin.get();
return 0;
}
[wlsh@wlsh-MacbookPro] chapter_2$ g++ getinfo.cpp
[wlsh@wlsh-MacbookPro] chapter_2$ ./a.out
How many carrots do you have?
12
Here are two more. Now you have 14 carrots.
[wlsh@wlsh-MacbookPro] chapter_2$ g++ sqrt.cpp
[wlsh@wlsh-MacbookPro] chapter_2$ ./a.out
Enter the floor area, in square feet, of your home: 10
That's the equivalent of a square 3.16228 feet to the side.
How fascinating!
[wlsh@wlsh-MacbookPro] chapter_2$ ./a.out
Enter the floor area, in square feet, of your home: 16
That's the equivalent of a square 4 feet to the side.
How fascinating!
2.4.2 函数变体
double pow(double , double);
answer = pow(5.0 , 8.0);
2.4.3 用户定义的函数
// ourfunc.cpp -- defining your own function
#include <iostream>
void simon(int); // function prototype for simon()
int main()
{
using namespace std;
simon(3); // call the simon() function
cout << "Pick an integer: ";
int count;
cin >> count;
simon(count); // call it again
cout << "Done!" << endl;
// cin.get();
// cin.get();
return 0;
}
void simon(int n) // define the simon() function
{
using namespace std;
cout << "Simon says touch your toes " << n << " times." << endl;
} // void functions don't need return statements
[wlsh@wlsh-MacbookPro] chapter_2$ g++ ourfunc.cpp
[wlsh@wlsh-MacbookPro] chapter_2$ ./a.out
Simon says touch your toes 3 times.
Pick an integer: 512
Simon says touch your toes 512 times.
Done!
2.4.4 用户定义的有返回值的函数
// convert.cpp -- converts stone to pounds
#include <iostream>
int stonetolb(int); // function prototype
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;
// cin.get();
// cin.get();
return 0;
}
int stonetolb(int sts)
{
return 14 * sts;
}
[wlsh@wlsh-MacbookPro] chapter_2$ g++ convert.cpp
[wlsh@wlsh-MacbookPro] chapter_2$ ./a.out
Enter the weight in stone: 15
15 stone = 210 pounds.
将函数比作一个由出入她们的信息所指定的黑盒子(block boxes)
2.4.5 在多函数程序中使用using编译指令
2.5 总结