C++ Primer 5th Edition自学笔记(1)

好吧,第一次写东西...如何下手呢...(请无视)

--------------------------------------------------------------

Chapter 1. Getting Started

1.1  Write a Simple C++ Program

函数组成:返回类型(type)、函数名、参数列表、函数体

  *函数体中的return返回类型要与函数的返回类型相匹配。

  *类型Type:包含了数据元素的内容和对其可能的操作。

源文件(Source files):一般指程序文件,常见扩展名:.cc, .cxx, .cpp, .cp, .C

1.2 A First Look at Input/Output

IO函数由标准库(standard library)提供,使用前开头加上相关头文件,如<iostream.h>

标准输入:cin

标准输出:cout

  *cerr用以输出警告和错误消息;clog用以输出程序执行的信息。

**表达式:会得到一个结果并与多个操作域和一个操作符相联系(an expression yields a result and is composed of one or more operands and (usually) an operator.)

  *endl(a manipulator)用以结束当前行并对缓存器进行刷新。

std::cin   std::cout   前缀std称为命名空间(namespace),可以保证不必要的名字冲突。

            ::操作符,表明cin函数和cout函数是用std命名空间里的。

Exercise Section 1.2

1 void Exericse_1_3()
2 {
3     std::cout << "Hello, World" << std::endl;
4 }
Exericse 1.3
void Exericse_1_4()
{
    int num1 = 0, num2 = 0;
    std::cout << "Input two numbers: ";
    std::cin >> num1 >> num2;
    std::cout << "The multiplication of these two numbers is " << num1 * num2 << std::endl;
}
Exericse 1.4

 1.3 A Word about Comments

注释(comment):注意随时更新!

  方式://  和   /*...*/

1.4 Flow of Control

1.4.1 The “while” Statement

  while循环:先判断条件,为真执行循环体。

*混合操作符 +=:  sum += val;   //等价于  sum = sum + val;

递增操作 ++:   ++val;  //等价于  val = val + 1;

Exercise Section 1.4.1

 1 void Exercise_1_9()
 2 {
 3     //Write a program that uses a while to sum the numbers from 50 to 100
 4     int sum = 0, num = 50;
 5     while (num <= 100)
 6     {
 7         sum += num;
 8         num ++;
 9     }
10     std::cout << "The sum from 50 to 100 is " << sum << std::endl; 
11 }
Exericse1.9
 1 void Exercise_1_10()
 2 {
 3     int sum = 0, num = 10;
 4     while(num >= 0)
 5     {
 6         sum += num;
 7         num--;
 8     }
 9     std::cout << "The sum from 10 to 1 is " << sum << std::endl;
10 }
Exericse1.10

1.4.2 The "for" Statement

for 循环包括两部分:a header and a body

  header:初始化 ;条件;表达式

Exericse Section 1.4.2

1 void Exercise_1_13()
2 {
3     int sum = 0, num = 0;
4     for (; num <= 10; num++)
5     {
6         sum += num;
7     }
8     std::cout << "The sum from 1 to 10 is " << sum << std::endl;
9 }
Exericse 1.13

1.4.3 Reading an Unknown Number of Input

while( std::cin >> val )

{...}

当输入一个无效的命令时,cin返回一个错误值,while跳出循环,如ctrl + z(windows),ctrl + d(Unix or Mac OS X)

*错误类型:

  1.语法错误(syntax error):如末尾未加分号,字符串未用双引号括起等

  2.类型错误(type error):如将整数复制给了string类型的变量

  3.声明错误(declaration error):变量必须先声明后使用。常犯的两种错误是缺少std命名空间和变量拼写错误。、

Exericse Section 1.4.3

1 void Exercise_1_16()
2 {
3     int sum = 0, num = 0;
4     while (std::cin >> num)
5     {
6         sum += num;
7     }
8     std::cout << "The sum of numbers you input is " << sum << std::endl;
9 }
Exercise1.16

 1.4.4 The if Statement

if (std::cin >> currVal) {...} 用以确保输入不为空

if()  {...}  如果括号中的返回值为Ture,运行此块中的命令

else {...} 否则运行此块中的命令

  *=操作符表示赋值,==操作符表示判断是否相等

*缩进和格式:关键是考虑它的可读性和可理解性,一旦选择一种风格,便坚持使用。

1.5 类 Introducing Classes

一种数据类型,对象的抽象

 书店实例:Class Sales_item

实例化:Sales_item item;

成员: ISBN, SUM, revenue

行为: 函数isbn:取出一个实例

    操作符>>,<<:读写实例

    操作符=:将一个实例赋值给另一个

    操作符+:将两个相同ISBN的实例相加,求出相应的SUM和revenue

    操作符+=:给一个实例增加另一个

  *头文件<>表示从系统中先搜索,""表示从项目中先搜索

Exercise Section 1.5.1

  之后写好类Sales_item再作答

1.5.2 初探成员函数

成员函数被定义为类的一部分,有时也被成为类的方法(methods)

使用成员函数的方法是利用“.”操作符和"()"操作符  如 item1.isbn()

1.6 The Bookstore Program

关键在于对成员函数 成员变量的理解

 

 

 

posted @ 2013-09-20 23:00  朝拜SUNS  阅读(352)  评论(0编辑  收藏  举报