C++ Primer 第1章 Getting Started 笔记

Section 1.1 Writing a Simple C++ Program

  • 每个 C++ 程序都包含一个或多个函数,其中必须有一个 main 函数
  • 操作系统通过调用 main 来执行一个 C++ 程序
int main()
{
	return 0;
}
  • 一个函数定义包含四部分:
  1. 返回值
  2. 函数名
  3. 参数列表(可能为空)
  4. 函数体
  • main 函数要求返回值类型为 int ,这是一个内置类型(built-in type)

  • 类型(Types):定义了数据元素的内容以及可以在数据上进行的操作

  • echo $? 可以得到程序执行的返回值

Section 1.2 A First Look at Input/Output

  • C++ 本身没有定义任何语句来进行 I/O,但是它包含了很多标准库来进行 I/O
  • 用的最多的是 iostream
  • 这个库里面有两个很重要的类型 istream, ostream,分别表示输入流和输出流
  • 一个流表示向 IO 设备读或写的一系列字符
  • 之所以用流来表示,其中暗含了字符是随着时间依次序生成(或消耗)的
  • 标准库定义了四个 IO 对象
  1. cinistream 类型的一个对象,表示标准输入
  2. coutostream 类型的一个对象,表示标准输出
  3. cerrostream 类型的一个对象,表示标准错误
  4. clogostream 类型的一个对象,主要生成执行的信息
  • 举一个例子:
#include<iostream>
int main(){
	std::cout << "Enter two numbers:" << std::endl;
	int v1 = 0, v2 = 0;
	std::cin >> v1 >> v2;
	std::cout << "The sum of " << v1 << " and " << v2
			  << " is " << v1 + v2 << std::endl;
	return 0;
}
  • 第一行 #include<iostream> 表示我们需要用到 iostream 库,这是一个头文件
  • main 里面的第一个语句执行了一个 expression (有一个或多个 operator 构成的一个计算单元)
  • << 运算符接受两个 operands,左边必须是 ostream 的一个对象,右边是一个需要输出的值,这个运算符将给定的值写到给定的 ostream 对象中去,得到的结果是其左边的 operand,所以得到的结果仍然是一个 ostream 的对象,因此等价于:
(std::cout << "Enter two numbers:") << std::endl;

以及

std::cout << "Enter two numbers:";
std::cout << std::endl;
  • 输出的第一个值是一个字符串常量(string literal),通过双引号将一堆字符围起来
  • 输出的第二个是 endl ,这个东西叫做运算子/操作符(manipulator),其作用是结束当前行,并刷新设备关联的缓冲区,刷新缓冲区(flushing the buffer)是为了保证程序输出的所有内容都已经写到了输出流中,而不是驻留在内存当中准备写入。

在这里插入图片描述

  • 前缀 std:: 表示 cout,endlstd 的命名空间中进行定义
  • 命名空间主要是为了避免命名冲突
  • int v1 = 0, v2 = 0 是在初始化两个变量

Section 1.3 A Word about Comments

  • 两种注释:/* *///

Section 1.4 Flow of Control

  • while
  • for
  • while(std::in >> value) 读入直到 end-of-file (ctrl+d,win 是 ctrl+c)
  • if

Section 1.5 Introducing Classes

  • 一个类定义了一个类型以及在其上可以进行的操作
  • C++ 类的设计主要集中在使得定义出来的类操作起来类似于 built-in type
  • 读写 Sales_item
#include<iostream>
#include "Sales_item.h"
int main()
{
	Sales_item book;
	std::cin >> book;
	std::cout << book << std::endl;
	return 0;
}
  • 添加 Sales_item
#include<iostream>
#include "Sales_item.h"
int main()
{
	Sales_item item1, item2;
	std::cin >> item1 >> item2;
	std::cout << item1 + item2 << std::endl;
	return 0;
}
  • 成员函数
#include<iostream>
#include "Sales_item.h"
int main()
{
	Sales_item item1, item2;
	std::cin >> item1 >> item2;
	if(item1.isbn() == item2.isbn()) {
		std::cout << item1 + item2 << std::endl;
		return 0;
	} else {
		std::cerr << "should refer to same ISBN" << std::endl;
		return -1;
	}
}

Section 1.6 The Bookstore Program

#include <iostream>
#include "Sales_item.h"
int main()
{
	Sales_item total;
	if(std::cin >> total) {
		Sales_item trans;
		while(std::cin >> trans) {
			if(total.isbn() == trans.isbn())
				total += trans;
			else {
				std::cout << total << std::endl;
				total = trans;
			}
		}
		std::cout << total << std::endl;
	} else {
		std::cerr << "no data?" << std::endl;
		return -1;
	}
}
posted @ 2020-08-11 21:43  winechord  阅读(83)  评论(0编辑  收藏  举报