C++ Primer5 第二章笔记整理(基本语句)

第二章 开始学习C++

2.1 进入C++

C++对大小写敏感

扩展名cpp

cout 可扩展

 1 2.1 myfirst.cpp
 2 #include <iostream>  //预处理编译器指令
 3 int main()           //函数头(返回类型)+形参列表 ()不接受任何信息
 4 {                    //函数体
 5   using namespace std; //名称空间 std::cout
 6   cout << "Come up and C++ me some time.";  //""字符串
 7   cout << endl;
 8   cout << "You won't regret it!" << endl;
 9   return 0;          //结束函数
10 }

 

main函数:函数头(调用接口)+函数体(计算机指令/语句/分号结束

int main(void)——不接受任何参数

在运行cpp程序时,通常从main()函数开始执行

//注释

/* */ C-风格注释

#include编译指令——导致预处理器将iostream文件的内容添加到程序中(预处理器操作:在源代码被编译前,替换或添加文本)

io指输入和输出,cin和cout要用到iostream中的定义

<<信息流动的路径 (运算符重载的例子),会根据其后的数据类型相应第调整其行为

控制符:endl——重起一行(iostream,std)

换行符:\n (转义序列)

2.2 C++语句

 1 2.2 carrot.cpp
 2 #include <iostream>
 3 int main()
 4 {
 5   using namespace std;
 6   int carrots;     //声明语句
 7   carrots = 25;
 8   cout << "I have " << carrots << " carrots." << endl;
 9   carrots = carrots - 1;
10   cout << "Crunch, crunch. Now I have " << carrots << " carrots." << endl;
11   return 0;
12 }

 

定义声明语句——在cpp中,所有变量都必须在首次使用前声明,指出要存储的数据类型和程序对存储在这里的数据使用的名称。将导致编译器为ie变量分配内存空间。

引用声明——使用在其它地方定义的变量

赋值语句将值赋给存储单元。赋值操作符=,可以连续使用,默认从右向左进行

2.3 其他C++语句

 1 2.3 getinfo.cpp
 2 #include <iostream>
 3 int main()
 4 {
 5   using namespace std;
 6   int carrots;
 7   cout << "How many carrots do you have?" << endl;
 8   cin >> carrots;
 9   cout << "Here are two more. ";
10   carrots = carrots + 2;
11   cout << "Now you have " << carrots << " carrots." << endl;
12   return 0;
13 }

 

cin使用>>操作符从输入流中抽取字符,在右侧需要提供一个变量接收信息

是cpp中面向编程(OPP)的核心概念之一。类是用户定义的一种数据类型,需要描述它能够表示什么信息和可以对数据执行的操作。

类描述了一种数据类型的全部属性,对象是根据这些描述创建的实体。

类可以来自于库。

2.4 函数

cpp函数分为有返回值和没有返回值两种。

有返回值的函数生成一个可被赋给变量的值,比如sqrt()函数返回平方根。参数是发送给函数的信息,返回值是从函数中发送回去的值。在使用之前,必须知道函数的参数类型和返回值类型。换言之,cpp程序应当为其中使用的每个函数提供原型(在首次使用前提供,通常放在main函数定义的前面)。(函数原型语句)

double sqrt(double);

(1)在源代码中输入函数原型 (2)包含头文件cmath

函数原型和函数定义不同。原型只描述接口,定义还包含代码。

 1 2.4 sqrt.cpp
 2 #include <iostream>
 3 #include <cmath>
 4 {
 5   using namespace std;
 6   double area;
 7   cout << "Enter the floor area, in square feet, of your home:";
 8   cin >> area;
 9   double side;
10   side = sqrt(area);
11   cout << "That is the equivalent of a square " << side << " feet to the side." << endl;
12   cout << "How fascinating!" << endl;
13   return 0;
14 }

 

linux系统中,使用gnu编译器要求在命令行结尾使用-lm

g++ sqrt.C -lm

C++允许在创建变量时对其赋值,称为初始化

double side = sqrt(area);

函数使用多个参数时中间用逗号分开:

double pow(double, double);  //x^y

有些函数不接受任何参数:

int rand(void);     //返回随机整数

有些函数没有返回值(直接输出在屏幕etc.),有时称为过程或子程序。

自己定义的无返回值的函数:

 1 2.5 ourfunc.cpp
 2 #include <iostream>
 3 void simon(int);  //函数原型 没有返回值,输入一个int参数
 4 int main()
 5 {
 6   using namespace std;
 7   simon(3);
 8   cout << "Pick an integer:";
 9   int count;
10   cin >> count;
11   simon(count);
12   cout << "Done!" << endl;
13   return 0;    //返回给操作系统,通常约定退出值为0意味着程序运行成功
14 }
15 void simon(int n) //自己定义函数的源代码在main函数后面
16 {
17   using namespace std;
18   cout << "Simon says touch your toes " << n << " times. " << endl;
19 }

 

函数定义不能嵌套在另一个函数定义中。

关键字是计算机语言中的词汇。这里用了 int void return double,不能作为变量名和函数名。

自己定义的有返回值的函数:

 1 2.6 convert.cpp
 2 #include <iostream>
 3 int stoneolb(int);
 4 int main()
 5 {
 6   using namespace std;
 7   int stone;
 8   cout << "Enter the weight in stone:";
 9   cin >> stone;
10   int pounds = stoneolb(stone);
11   cout << stone << "stone =" << pounds << "pounds." << endl;
12   return 0;
13 }
14 int stoneolb(int sts)
15 {
16   return 14*sts;
17 }

 

在多函数程序中使用using编译指令,可以放在函数外面,位于函数前面。

posted @ 2020-05-24 22:38  尘寰肆漓  阅读(150)  评论(0)    收藏  举报