《C++ primer plus》——第 2 章 开始学习 C ++

2.1 进入 C++

1. 作为接口的函数头
 // 格式一
 int main(void)
 {
         return 0;
 }
 // 格式二
 int main()
 {
         return 0;
 }

在括号中使用关键词void明确指出,函数不接受任何参数。

2. 为什么 main() 不能使用其他名称

通常,一个 C++ 程序必须包括一个名为 main() 的函数。在运行 C++ 程序时,通常从 main() 函数开始执行。

因此,如果没有 main() ,程序将不完整,编译器将指出未定义 main() 函数。

当然存在一些例外的情况,后续出现再说明。

3. 注释

单行注释://

多行注释:/* */

4. C++ 预处理器
 #include <iostream>
 using namespace std;

iostream 代表文件,io 指的是输入(进入程序的信息)和 输出(从程序中发送出去的信息)。

注意:使用 cin 和 cout 进行输入 和 输出的程序必须包含文件 iostream。

5. 头文件名

像 iostream 这个的文件叫做包含文件(include file)——由于它们被包含再其他文件中;

也叫头文件(head file)——由于它们被包含再文件起始处。

头文件类型约定示例说明
C++ 旧式风格以 .h 结尾iostream.hC++ 程序可以使用
C 旧式风格以 .h 结尾math.hC、C++ 程序可以使用
C ++ 新式风格没有扩展名iostreamC++ 程序可以使用,使用 namespace std
转换后的 C加上前缀 c,没有扩展名cmathC++ 程序可以使用,可以使用不是 C 的特性,如namespace std
6. 名称空间

名称空间编译指令来使 iostream 中定义对程序可用:

 using namespace std;

这个叫 using 编译指令。

名称空间支持时一项 C++ 特性。用于指出使用某个厂商的产品库。

例如:

 Microflop::wanda("go dancing?");    // use Microflop namespace version
 Piscine::wanda("a fish named Desire");  // use Piscine namespace version

按照这种方式,类、函数 和 变量便是 C++ 编译器的标准组件,它们都被放置在名称空间 std 中。

当头文件没有扩展名名 h 时,情况才是如此。

例如:

 std::cout << "Come up and C++ me some time.";
 std::cout << std::endl;

然而,还有其他方法使用 std 名称空间的指定库:

例如:

 // using 声明实现
 using std::cout;
 using std::endl;
 using std::cin;
 ​
 // 空间编译指令 using namespace std
 #include <iostream>
 int main()
 {
     using namespace std;
     cout << "Come up and C++ me some time.";
     cout << endl;
     cout <<"You won't regret it!" << endl;
     return 0;
 }
7. 使用 cout 进行 C++ 输出
 #include <iostream>
 int main(void)
 {
         using namespace std;
         cout << "Come up and C++ me some time.";
         cout << endl;
         return 0;
 }

endl 是一个特殊的 C++ 符号,被称为控制符,表示:重起一行。

endl 也是再头文件 iostream 中定义的,且位于名称空间 std 中。

C++ 还提供一个输出指示换行的旧式方法:\n

 cout << "Come up and C++ me some time.\n";
 cout << "Come up and C++ me some time." << endl;

此外,如果要生成一个空行的话,使用最初的方法比较快捷。

 cout << endl;
8. C++ 源代码风格
  • 每条语句占一行。

  • 每个函数都有一个开始 和 结束的花括号。

  • 函数中的语句都相对于花括号进行缩进。

  • 与函数名称相关的圆括号周围没有空白。

2.2 C++ 语句

1. 声明语句 和 变量
 #include <iostream>
 int main(void)
 {
         using namespace std;
 ​
         int carrots;
 ​
         carrots = 25;
         cout << "I have ";
         cout << carrots;
         cout << " carrots.";
         cout << endl;
         carrots = carrots - 1;
         cout << "Crunch, crunch. Now I have " << carrots << " carrots." << endl;
 ​
         return 0;
 }

2.3 其他 C++ 语句

1. 使用 cin
 #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. ";
         carrots = carrots + 2;
         cout << "Now you have " << carrots << " carrots." << endl;
         return 0;
 }

用 cin 来读取键盘输入。 符号 << 和 >> 被选择用来指示信息流的方向。

2. 使用 cout 进行拼接
 // 方式一:
 cout << "Now you have " << carrots << " carrots." << endl;
 // 方式二:
 cout << "Now you have ";
 cout << carrots;
 cout << " carrots.";
 cout << endl;
 // 方式三:
 cout << "Now you have "
     << carrots
     << " carrots."
     << endl;

2.4 函数

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

1. 使用有返回值的函数
 x = sqrt(6.25); //returns the value 2.5 and assigns it to x

函数的变量声明——指出涉及的数据类型。

例如:(实际应用中,采用格式二,尽量去声明变量的数据类型)

 // 格式一:
 double sqrt(double);
 // 格式二:
 double x;   // declare x as a type double variable
 x = sqrt(6.25);

在程序中使用 sqrt() 时,也必须提供原型。可以用两种方法来实现:

  • 在源代码文件中输入函数原型;

  • 包含头文件 cmath (老系统 math.h),其中定义了原型。

 #include <iostream>
 #include <cmath>
 int main(void)
 {
         using namespace std;
 ​
         double area;
         double side;
 ​
         cout << "Enter the floor area, in square feet, of your home: ";
         cin >> area;
         side = sqrt(area);
         cout << "That's the equivalent of a square " << side
              << " feet to the side." << endl;
         cout << "How fascinating!" << endl;
 ​
         return 0;
 }  
2. 自定义函数

步骤:先定义函数体,再 main() 函数里面调用,最后在 main() 函数上面进行函数声明。

 // 无返回值
 #include <iostream>
 ​
 void simon(int n);
 ​
 int main(void)
 {
         using namespace std;
 ​
         simon(3);
 ​
         return 0;
 }
 ​
 void simon(int n)
 {
         using namespace std;
 ​
         cout << "Simon says touch your toes " << n << " time." << endl;
 ​
 }
 ​
 // 有返回值
 #include <iostream>
 int stonetolb(int sts);
 ​
 int main(void)
 {
         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;
 }

注意:关键字不能作他用,只能专用。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值