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

【c++ Prime 学习笔记】第1章 开始的更多相关文章

  1. Stealth视频教程学习笔记(第二章)

    Stealth视频教程学习笔记(第二章) 本文是对Unity官方视频教程Stealth的学习笔记.在此之前,本人整理了Stealth视频的英文字幕,并放到了优酷上.本文将分别对各个视频进行学习总结,提 ...

  2. Stealth视频教程学习笔记(第一章)

    Stealth视频教程学习笔记(第一章) 本文是对Unity官方视频教程Stealth的学习笔记.在此之前,本人整理了Stealth视频的英文字幕,并放到了优酷上.本文将分别对各个视频进行学习总结,提 ...

  3. 20145330《Java学习笔记》第一章课后练习8知识总结以及IDEA初次尝试

    20145330<Java学习笔记>第一章课后练习8知识总结以及IDEA初次尝试 题目: 如果C:\workspace\Hello\src中有Main.java如下: package cc ...

  4. java JDK8 学习笔记——第16章 整合数据库

    第十六章 整合数据库 16.1 JDBC入门 16.1.1 JDBC简介 1.JDBC是java联机数据库的标准规范.它定义了一组标准类与接口,标准API中的接口会有数据库厂商操作,称为JDBC驱动程 ...

  5. CSS3秘笈第三版涵盖HTML5学习笔记1~5章

    第一部分----CSS基础知识 第1章,CSS需要的HTML HTML越简单,对搜索引擎越友好 div是块级元素,span是行内元素 <section>标签包含一组相关的内容,就像一本书中 ...

  6. 《DOM Scripting》学习笔记-——第三章 DOM

    <Dom Scripting>学习笔记 第三章 DOM 本章内容: 1.节点的概念. 2.四个DOM方法:getElementById, getElementsByTagName, get ...

  7. Programming Entity Framework-dbContext 学习笔记第五章

    ### Programming Entity Framework-dbContext 学习笔记 第五章 将图表添加到Context中的方式及容易出现的错误 方法 结果 警告 Add Root 图标中的 ...

  8. The Road to learn React书籍学习笔记(第三章)

    The Road to learn React书籍学习笔记(第三章) 代码详情 声明周期方法 通过之前的学习,可以了解到ES6 类组件中的生命周期方法 constructor() 和 render() ...

  9. [HeadFrist-HTMLCSS学习笔记]第五章认识媒体:给网页添加图像

    [HeadFrist-HTMLCSS学习笔记]第五章认识媒体:给网页添加图像 干货 JPEG.PNG.GIF有何不同 JPEG适合连续色调图像,如照片:不支持透明度:不支持动画:有损格式 PNG适合单 ...

  10. [HeadFrist-HTMLCSS学习笔记]第三章构建模块:Web页面建设

    [HeadFrist-HTMLCSS学习笔记]第三章构建模块:Web页面建设 敲黑板!! <q>元素添加短引用,<blockquote>添加长引用 在段落里添加引用就使用< ...

随机推荐

  1. CSS实用技巧(中)

    前言 我们经常使用CSS,但是却不怎么了解CSS,本文主要对vertical-align.BFC.position中开发过程不怎么注意的特性进行简要总结,从本文中,你将了解到以下内容: vertica ...

  2. Java创建线程池的方法

    Executors创建四种线程池: CachedThreadPool:可缓存的线程池,该线程池中没有核心线程,非核心线程的数量为Integer.max_value,当有需要时创建线程来执行任务,没有需 ...

  3. MySQL——字符集

    -- 字符集:是一个系统支持的所有抽象字符的集合 MySQL数据库的字符集(包括两个部分): 1.字符集:character 2.校对规则:collation MySQL中常见的字符集: utf8 l ...

  4. openswan协商流程之(五):main_inR2_outI3()

    主模式第五包:main_inR2_outI3 文章目录 主模式第五包:main_inR2_outI3 1. 序言 2.函数调用关系 3. 第五个报文流程图 4. main_inR2_outI3()源码 ...

  5. AspectJ基于xml和基于注解

    一.基于xml 执行的切入点中具体方法有返回值,则方法结束会立即执行后置通知,然后再执行环绕通知的放行之后的代码: 2.连接点即所有可能的方法,切入点是正真被切的方法,连接点方法名: 其中,只有环绕通 ...

  6. Typora + PicGo做个人知识库

    最近在做个人知识库,考察了一圈各种平台和工具,发现还是直接用文件系统管理Markdown文件更符合我当前的需求.以Markdown文件作为文字载体,以文件目录作为分类结构,承载以计算机知识为主的学习笔 ...

  7. 支持Cron表达式、间隔时间的工具(TaskScheduler)

    后台任务如何支持间隔时间.Cron表达式两种方式? 分享一个项目TaskScheduler,这是我从Furion项目中拷出来的 源码:https://gitee.com/dot-net-core/ta ...

  8. Nginx:进程调度

    Blog:博客园 个人 Nginx采用的是固定数量的多进程模型,由一个主进程(MasterProcess)和数量与主机CPU核数相同的工作进程协同处理各种事件. 主管理进程负责工作进程的配置加载.启停 ...

  9. JAVA修饰符优先级先后顺序规范

    在实际的开发中,会遇到定义静态常量时,有的人使用的修饰符顺序不一致,例如 ... static final ... 或者 ... final static ... 于是找到了下规范,分享下 优先级 修 ...

  10. dede织梦会员模板调用template下模板head.htm方法及解析变量

    1.找到dedecms会员中心的的目录 member ,然后在目录下用编辑器打开config.php 加入对dede模板解释函数如下:   //php脚本开始 //引入arc.partview.cla ...