【c++ Prime 学习笔记】第1章 开始

1.1 编写一个简单的程序

int main()
{
	return 0;
}

函数 包含4部分:

  • 返回类型(return type)
  • 函数名(function name)
  • 形参列表(parameter list,允许为空)
  • 函数体(function body)

main 函数

  • C++程序必有函数,操作系统通过main函数来运行C++ 程序
  • 返回类型必为int,被用于指示状态。0表明成功,非0值由系统定义,通常用来指出错误类型

类型

  • 一种类型不仅定义了数据元素的内容,还定义了这类数据上可进行的运算

1.1.1 编译、运行程序

编译

  • 如何编译程序依赖所使用的操作系统和编译器(Integrated Developed Environment,IDE)

程序源文件命名约定

  • 源文件名字以后缀结尾,常见后缀:.cc.cxx.cpp.cp.C

从命令行运行编译器

  • 编译器编译源文件,可生成一个可执行文件。
    windows系统中可执行后缀为.exe,运行时需提供可执行文件的文件名,可忽略后缀.exe;
    Unix系统中可执行后缀为.out,运行时不可忽略后缀

  • .\表示当前目录

  • 执行完一个程序后,可通过echo命令获取main的返回值
    Unix:echo $?
    Windows:echo %ERRORLEVEL%

GNU编译器 g++

  • 命令行g++ -o prog1 prog1.cc
    -o prog1是编译器参数,指定可执行文件的文件名prog1或prog1.exe;省略则生成名为a.out或a.exe的可执行文件

  • 根据GNU版本,需指定-std=c++0x参数开启C++11

  • -Wall 参数选项,能对有问题的程序结构发出警告

微软Visual Studio编译器 cl

  • 命令行 C:\Users\me\Programs> cl /EHsc prog1.cpp
    前半部为路径,/EHsc为编译器选项,用来打开标准异常处理。最后生成prog1.exe

  • /W4 参数选项,发出警告


1.2 初始输入输出

标准库(standard library)提供IO机制

  • 本书使用iostream库,包含输入流istream输出流ostream

标准输入输出对象

  • cin 标准输入

  • cout 标准输出

  • cerr 标准错误,输出警告和错误信息

  • clog 输出程序运行时的一般性信息

一个使用IO库的程序

  • #include <iostream> 头文件(header)

使用标准库中的名字

  • std::中,std为命名空间,命名空间可避免相同名字导致冲突,如同名函数。::作用域运算符,用来指出命名空间std中的名字cout

向流写入数据

  • std::cout<<
    输出运算符<<接受两个对象:左侧运算对象必须是一个ostream对象,右侧运算对象是要打印的值

  • endl
    写入endl操纵符的效果是换行,并将与设备关联的缓冲区中的内容刷到设备中。保证目前为止程序所产生的所以输出都真正写入输出流中,而不仅停留在内存中等待写入流。

从流读取数据

  • std::cin >>
    输入运算符>>,其左侧是istream对象,右侧是要被写入值的变量

1.3 注释简介

C++中注释的种类

  • 单行注释//
  • 界定符注释/**/,注释界定符不能嵌套

1.4 控制流

1.4.1 while 语句

while(val < 10){
	sum += val;
	++val;
}

1.4.2 for 语句

for (int val = 1; val <= 10; ++val)
	sum +=val;

1.4.3 读取数量不定的输入数据

//读取数据知道遇到文件尾,计算所有读入的值的和
while(std::cin >> value)
	sum +=value;
此循环条件实际检测的是`std::cin`。如果流有效,即未遇到错误,那么返回true;当遇到文件结束符(end-of-file),或遇到一个无效输入时(例如读入的值不是整数),istream对象状态会变为无效,返回false。

键盘输入文件结束符

  • Windows:Ctrl+Z,然后按Enter或Return建
  • Unix和Mac OS X :Ctrl+D

编译器能检查出的错误

  • 语法错误(syntax error)
  • 类型错误(type error)
  • 声明错误(declaration error)

1.4.4 if 语句

if(condition)
{
}

1.5 类简介

  • 在C++中我们通过定义一个类(class)来定义自己的数据结构
  • 一个类定义了一个类型,以及与其关联的一组操作
  • 类在头文件中定义,头文件一般根据类名命名。头文件后缀.h、.H.、hpp、.hxx

1.5.1 Sales_item 类

读写Sales_item

#include <iostream>
#include "Sales_item.h"
int main()
{
		Sales_item book;
    // 读入 ISBN 号、售出的册数以及销售价格
    std::cin >> book;
    // 写入 ISBN、售出的册数、总销售额和平均价格
    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;
}

使用文件重定向

addItems < infile >outfile

1.5.2 初始成员函数

成员函数(member function)

  • 成员函数是定义为类的一部分的函数,有时也叫方法( method )
  • () 调用运算符
if( item1.isbn() == item2.isbn() )
{
}

1.6 书店程序

#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;      // total 现在表示下一本书的销售额
            }
        }
        std::cout << total << std::endl;    // 打印最后一本书的结果
    } else {
        // 没有输入!警告读者
        std::cerr << "No data?!" << std::endl;
        return -1;  // 表示失败
    }
    return 0;
}

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(): units_sold(0), revenue(0.0) { }
    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
    unsigned units_sold;
    double revenue;
};

// 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
posted @ 2021-04-22 15:25  砥才人  阅读(462)  评论(0编辑  收藏  举报