C++ Primer 1_开始

C++ Primer 1_开始

自此,开始正式学习C++语言,之前学过的基础性的东西我将不会在此再做重复,如果有一些我之前从未注意过或从未深思过的事情以及一些重点,我都将会在此写下。

iostream 库包含两个基础类型 istream 和 ostream,分别表示输入流和输出流。
术语“流”(stream)想要表达的是,随着时间的推移,字符是顺序生成或消耗的。
标准库定义了 4 个 IO 对象。

关键字类型描述
cinistream标准输入
coutostream标准输出
cerrostream标准错误
clogostream(输出程序运行时的一般性信息)

endl 是一个被称为操纵符(manipulator)的特殊值。写入 endl 的效果是结束当前行,并将与设备关联的缓冲区 (buffer)中的内容刷到设备中。缓冲刷新操作可以保证到目前为止程序所产生的所有输出都真正写入输出流中,而不是仅停留在内存中等待写入流。

程序员常常在调试时添加打印语句。这类语句应该保证“一直”刷新流。否则,如果程序崩溃,输出可能还留在缓冲区中,从而导致关于程序崩溃位置的错误推断。

关于注释:
注释对 /* */ 不能嵌套

/* 注释对 /* */ 不能嵌套 */

我们通常需要在调试期间注释掉一些代码。由于这些代码可能包含界定符对形式的注释,所以可能导致注释嵌套错误,因此最好的方式是用单行注释方式注释掉代码段的每一行

// /*
// *单行注释中的任何内容都会被忽略
// *包括嵌套的注释对也一样会被忽略
// */

当从键盘向程序输入数据时,对于如何指出文件结束,不同操作系统有不同的约定。在 Windows系统中,输入文件结束符的方法是敲 Ctrl + Z(按住Ctrl键的同时按Z键)然后按 Enter 或 Return 键。在 UNIX 系统中,包括 Mac OS X系统中,文件结束符输入是用 Ctrl + D 。

按照报告的顺序来逐个修正错误,是一种好习惯。
因为一个单个错误常常会具有传递效应,导致编译器在其后报告比实际数量多得多的错误信息。所以:另一个好习惯是在每修正一个错误后就立即重新编译代码,或者最多是修正了一小部分明显的错误后就重新编译。这就是所谓的“编辑-编译-调试”(edit-compile-debug)周期。

书店程序:
Sales_item.h

/*
* This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
* Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
* copyright and warranty notices given in that book:
*
* "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
*
*
* "The authors and publisher have taken care in the preparation of this book,
* but make no expressed or implied warranty of any kind and assume no
* responsibility for errors or omissions. No liability is assumed for
* incidental or consequential damages in connection with or arising out of the
* use of the information or programs contained herein."
*
* Permission is granted for this code to be used for educational purposes in
* association with the book, given proper citation if and when posted or
* reproduced.Any commercial use of this code requires the explicit written
* permission of the publisher, Addison-Wesley Professional, a division of
* Pearson Education, Inc. Send your request for permission, stating clearly
* what code you would like to use, and in what specific way, to the following
* address:
*
*     Pearson Education, Inc.
*     Rights and Permissions Department
*     One Lake Street
*     Upper Saddle River, NJ  07458
*     Fax: (201) 236-3290
*/

/* This file defines the Sales_item class used in chapter 1.
* The code used in this file will be explained in
* Chapter 7 (Classes) and Chapter 14 (Overloaded Operators)
* Readers shouldn't try to understand the code in this file
* until they have read those chapters.
*/

#ifndef SALESITEM_H
// we're here only if SALESITEM_H has not yet been defined 
#define SALESITEM_H

#include <iostream>
#include <string>

class Sales_item {
	// these declarations are explained section 7.2.1, p. 270 
	// and in chapter 14, pages 557, 558, 561
	friend std::istream& operator>>(std::istream&, Sales_item&);
	friend std::ostream& operator<<(std::ostream&, const Sales_item&);
	friend bool operator<(const Sales_item&, const Sales_item&);
	friend bool
		operator==(const Sales_item&, const Sales_item&);
public:
	// constructors are explained in section 7.1.4, pages 262 - 265
	// default constructor needed to initialize members of built-in type
#if defined(IN_CLASS_INITS) && defined(DEFAULT_FCNS)
	Sales_item() = default;
#else
	Sales_item() : units_sold(0), revenue(0.0) { }
#endif
	Sales_item(const std::string& book) :
		bookNo(book), units_sold(0), revenue(0.0) { }
	Sales_item(std::istream& is) { is >> *this; }
public:
	// operations on Sales_item objects
	// member binary operator: left-hand operand bound to implicit this pointer
	Sales_item& operator+=(const Sales_item&);

	// operations on Sales_item objects
	std::string isbn() const { return bookNo; }
	double avg_price() const;
	// private members as before
private:
	std::string bookNo;      // implicitly initialized to the empty string
#ifdef IN_CLASS_INITS
	unsigned units_sold = 0; // explicitly initialized
	double revenue = 0.0;
#else
	unsigned units_sold;
	double revenue;
#endif
};

// used in chapter 10
inline
bool compareIsbn(const Sales_item& lhs, const Sales_item& rhs) {
	return lhs.isbn() == rhs.isbn();
}

// nonmember binary operator: must declare a parameter for each operand
Sales_item operator+(const Sales_item&, const Sales_item&);

inline bool
operator==(const Sales_item& lhs, const Sales_item& rhs) {
	// must be made a friend of Sales_item
	return lhs.units_sold == rhs.units_sold &&
		lhs.revenue == rhs.revenue &&
		lhs.isbn() == rhs.isbn();
}

inline bool
operator!=(const Sales_item& lhs, const Sales_item& rhs) {
	return !(lhs == rhs); // != defined in terms of operator==
}

// assumes that both objects refer to the same ISBN
Sales_item& Sales_item::operator+=(const Sales_item& rhs) {
	units_sold += rhs.units_sold;
	revenue += rhs.revenue;
	return *this;
}

// assumes that both objects refer to the same ISBN
Sales_item
operator+(const Sales_item& lhs, const Sales_item& rhs) {
	Sales_item ret(lhs);  // copy (|lhs|) into a local object that we'll return
	ret += rhs;           // add in the contents of (|rhs|) 
	return ret;           // return (|ret|) by value
}

std::istream&
operator>>(std::istream& in, Sales_item& s) {
	double price;
	in >> s.bookNo >> s.units_sold >> price;
	// check that the inputs succeeded
	if (in)
		s.revenue = s.units_sold * price;
	else
		s = Sales_item();  // input failed: reset object to default state
	return in;
}

std::ostream&
operator<<(std::ostream& out, const Sales_item& s) {
	out << s.isbn() << " " << s.units_sold << " "
		<< s.revenue << " " << s.avg_price();
	return out;
}

double Sales_item::avg_price() const {
	if (units_sold)
		return revenue / units_sold;
	else
		return 0;
}

#endif

bookstore.cpp

#include <iostream>
#include <stdlib.h>
#include "Sales_item.h"

using namespace std;
int main() {
    Sales_item total;
    if (cin >> total) {
        Sales_item trans;
        while (cin >> trans) {
            if (total.isbn() == trans.isbn())
                total += trans;
            else {
                cout << total << endl;
                total = trans;
            }
        }
        cout << total << endl;
    }
    else {
        cerr << "No data?!" << endl;
        system("pause");
        return -1;
    }

	system("pause");
	return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值