C++ Primer 学习笔记 第一章

目录

  第一章 快速入门

  1.1 编写简单的C++ 程序

  1.2 输入/输出

    1.2.1 标准输入与输出对象

    1.2.2 一个使用IO库的程序

   1.4 控制结构

  1.5  类的简介

    1.5.2 初窥成员函数

  1.6 C++程序


  Primer 意思为入门书,初级读本

  第一章 快速入门

  C++ Primer 源码下载地址

 

  1.1 编写简单的C++ 程序

  每个C++程序都包含一个或多个函数,而且必须有一个命名为main,main函数是唯一被操作系统显式调用的函数,返回0值表明程序成功执行完毕,返回值必须是int型,int类型是内置类型(由C++语言定义的也叫基本类型)

  调用GUN编译器的默认命令是g++,微软编译器采用命令c1来调用

  1.2 输入/输出

  C++并没有直接定义进行输入或输出的任何语句,这个功能 是由标准库提供 的,格式化输入和输出的是iostream库,表示输入输出流,流 "说明读入或写出的字符序列是随着时间顺序生成或消耗的    

    1.2.1 标准输入与输出对象

  cin标准输入,cout标准输出,cerr标准错误,clong产生程序执行的一般信息,大部分操作系统提供了重定向输入或输出流的方法,可以将这些流与选择的文件联系起来

    1.2.2 一个使用IO库的程序

  一个表达式 由 一个或几个操作数  和 通常是一个操作符 组成,每个表达式都会产生一个结果,通常是将操作符作用到其操作数所产生的值,当操作符是输出操作符时,结果是左操作数的值,即输出流本身

  endl 是一个特殊值,称为操纵符,将它写入输出流时,具有换行的效果,并刷新与设备相关联的缓冲区,忘记刷新输出流可能会造成输出停留在缓冲区

  使用命名空间,程序员可以避免无意中使用了库中所定义名字相同的名字,输入操作符 >> ,它接受一个istream对象作为其左操作数,接受一个对象作为其右操作数,它从istream操作数读取数据并保存到右操作数,输入操作符返回其左操作数作为结果

  程序中,大部分出现空格的地方可用换行符代替,但是,空格符不允许出现在预处理指示中字符串字面值中的空格符不能用换行符代替

   1.4 控制结构

  按编译错误报告的顺序改正错误是个好习惯,操作系统使用不同的值作为文件结束符,windows系统下我们同时键入"ctrl" 和 "z"键,来输入文件结束符  

  1.5  类的简介

  每个类定义一种类型,类型名与类名相同

  当使用自定义头文件 时,我们采用双引号把头文件括起来,标准库的头文件 用尖括号括起来,非标准库的头文件 用双引号括起来

    1.5.2 初窥成员函数

  成员函数,是类定义的函数,有时称为方法,成员函数只定义一次,被视为每个对象的成员 。我们将这些操作称为成员函数,它们在特定的对象上操作

  点操作符 通过它的左操作数取得右操作数,左操作数必须是类类型的对象,右操作数必须指定该类型的成员,成员函数作为点操作符的右操作数调用操作符(() )放在函数名之后

  1.6 C++程序

  Sales_item.h

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

// Definition of Sales_item class and related functions goes here
#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
    Sales_item() = default;
    Sales_item(const std::string &book): bookNo(book) { }
    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
    unsigned units_sold = 0; // explicitly initialized
    double revenue = 0.0;
};

// 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
View Code

  术语:argument(实参),buffer(缓冲区),edit-compile-debug(编辑-编译-调试),bulid-in type(内置类型,如: int ),end-of-file(文件结束符),class type(类类型),curly brace(花括号),library type(标准库类型),manipulator(操纵符,读或写时操纵流本身的对象,比如:endl),parameter list(形参表),string literal(字符串字面值),uninitialized variable(未初始化变量),preprocessor directive(预处理指示,#include是一个预处理器指示),ISBN (International Standard Book Number,国际标准书号)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值