补充Sales_data没有体现出的其他类特性

Screen.h

 1 #include <string>
2 #include <iostream>
3
4 class Screen {
5 public:
6 typedef std::string::size_type pos;
7 #if defined(IN_CLASS_INITS) && defined(DEFAULT_FCNS)
8 Screen() = default; // needed because Screen has another constructor
9 #else
10 Screen(): cursor(0), height(0), width(0) { }
11 #endif
12 // cursor initialized to 0 by its in-class initializer
13 Screen(pos ht, pos wd, char c): height(ht), width(wd),
14 contents(ht * wd, c) { }
15 friend class Window_mgr;
16 Screen(pos ht = 0, pos wd = 0):
17 cursor(0), height(ht), width(wd), contents(ht * wd, ' ') { }
18 char get() const // get the character at the cursor
19 { return contents[cursor]; } // implicitly inline
20 inline char get(pos ht, pos wd) const; // explicitly inline
21 Screen &clear(char = bkground);
22 private:
23 static const char bkground = ' ';
24 public:
25 Screen &move(pos r, pos c); // can be made inline later
26 Screen &set(char);
27 Screen &set(pos, pos, char);
28 // other members as before
29 // display overloaded on whether the object is const or not
30 Screen &display(std::ostream &os)
31 { do_display(os); return *this; }
32 const Screen &display(std::ostream &os) const
33 { do_display(os); return *this; }
34 private:
35 // function to do the work of displaying a Screen
36 void do_display(std::ostream &os) const {os << contents;}
37 // other members as before
38 private:
39 #ifdef IN_CLASS_INITS
40 pos cursor = 0;
41 pos height = 0, width = 0;
42 #else
43 pos cursor;
44 pos height, width;
45 #endif
46 std::string contents;
47 };
48
49 Screen &Screen::clear(char c)
50 {
51 contents = std::string(height*width, c);
52 return *this;
53 }
54
55 inline // we can specify inline on the definition
56 Screen &Screen::move(pos r, pos c)
57 {
58 pos row = r * width; // compute the row location
59 cursor = row + c; // move cursor to the column within that row
60 return *this; // return this object as an lvalue
61 }
62
63 char Screen::get(pos r, pos c) const // declared as inline in the class
64 {
65 pos row = r * width; // compute row location
66 return contents[row + c]; // return character at the given column
67 }
68
69 inline Screen &Screen::set(char c)
70 {
71 contents[cursor] = c; // set the new value at the current cursor location
72 return *this; // return this object as an lvalue
73 }
74 inline Screen &Screen::set(pos r, pos col, char ch)
75 {
76 contents[r*width + col] = ch; // set specified location to given value
77 return *this; // return this object as an lvalue
78 }

useScreen.cpp

 1 #include <iostream>
2 using std::cout; using std::endl;
3
4 #include <string>
5 using std::string;
6
7 #include "Screen.h"
8
9 int main()
10 {
11 Screen myScreen(5,3);
12 // move the cursor to a given position, and set that character
13 myScreen.move(4,0).set('#');
14
15 Screen nextScreen(5, 5, 'X');
16 nextScreen.move(4,0).set('#').display(cout);
17 cout << "\n";
18 nextScreen.display(cout);
19 cout << endl;
20
21 const Screen blank(5, 3);
22 myScreen.set('#').display(cout); // calls nonconst version
23 cout << endl;
24 blank.display(cout); // calls const version
25 cout << endl;
26
27 myScreen.clear('Z').display(cout); cout << endl;
28 myScreen.move(4,0);
29 myScreen.set('#');
30 myScreen.display(cout); cout << endl;
31 myScreen.clear('Z').display(cout); cout << endl;
32
33 // if move returns Screen not Screen&
34 Screen temp = myScreen.move(4,0); // the return value would be copied
35 temp.set('#'); // the contents inside myScreen would be unchanged
36 myScreen.display(cout);
37 cout << endl;
38 }

[笔记] 《c++ primer》显示器程序 Chapter7的更多相关文章

  1. Lua学习笔记4. coroutine协同程序和文件I/O、错误处理

    Lua学习笔记4. coroutine协同程序和文件I/O.错误处理 coroutine Lua 的协同程序coroutine和线程比较类似,有独立的堆栈.局部变量.独立的指针指令,同时又能共享全局变 ...

  2. 微信小程序开发:学习笔记[7]——理解小程序的宿主环境

    微信小程序开发:学习笔记[7]——理解小程序的宿主环境 渲染层与逻辑层 小程序的运行环境分成渲染层和逻辑层. 程序构造器

  3. Linux进程线程学习笔记:运行新程序

    Linux进程线程学习笔记:运行新程序                                         周银辉 在上一篇中我们说到,当启动一个新进程以后,新进程会复制父进程的大部份上下 ...

  4. 个人学习笔记:C语言程序结构

    个人笔记:C语言程序 函数 语句 输入输出对象 标识符 关键字 函数 一个C语言源程序,是由一个或多个函数定义顺序组成的,其中必须有一个函数名为main的主函数.C语言源程序中的函数是指完成特定数据处 ...

  5. [笔记] 《c++ primer》书店程序 Chapter7

    Sales_data.h 1 #ifndef SALES_DATA_H 2 #define SALES_DATA_H 3 4 #include "Version_test.h" 5 ...

  6. C++ Primer 学习笔记_95_用于大型程序的工具 --多重继承与虚继承

    用于大型程序的工具 --多重继承与虚继承 引言: 大多数应用程序使用单个基类的公用继承,可是,在某些情况下,单继承是不够用的,由于可能无法为问题域建模,或者会对模型带来不必要的复杂性. 在这些情况下, ...

  7. C++ Primer 学习笔记_88_用于大型程序的工具 --异常处理[续1]

    用于大型程序的工具 --异常处理[续1] 四.又一次抛出 有可能单个catch不能全然处理一个异常.在进行了一些校正行动之后,catch可能确定该异常必须由函数调用链中更上层的函数来处理,catch能 ...

  8. C++ Primer 学习笔记_87_用于大型程序的工具 --异常处理

    用于大型程序的工具 --异常处理 引言: C++语言包括的一些特征在问题比較复杂,非个人所能管理时最为实用.如:异常处理.命名空间和多重继承. 相对于小的程序猿团队所能开发的系统需求而言,大规模编程[ ...

  9. C++ Primer 学习笔记_91_用于大型程序的工具 --命名空间

    用于大型程序的工具 --命名空间 引言: 在一个给定作用域中定义的每一个名字在该作用域中必须是唯一的,对庞大.复杂的应用程序而言,这个要求可能难以满足.这样的应用程序的全局作用域中一般有很多名字定义. ...

随机推荐

  1. 配置redis 4.0.11 集群

    配置redis 4.0.11 集群 准备redis 软件和redis配置文件 启动Redis服务 /data/soft/redis/src/redis-check-aof --fix /log/red ...

  2. SQL语句练习(进阶版)

    学生数据库中有三个基本表(关系)如下: 学生表S(Sno,Sname,Age,Sex,SD) 课程表C(Cno,Cname, Teacher) 选课表SC(Sno,Cno,Grade) 请用SQL语言 ...

  3. Python数据分析入门(十七):绘制条形图

    条形图的绘制方式跟折线图非常的类似,只不过是换成了plt.bar方法.plt.bar方法有以下常用参数: x:一个数组或者列表,代表需要绘制的条形图的x轴的坐标点. height:一个数组或者列表,代 ...

  4. 【ProLog - 4.0 List】

    [简介] 列表是Prolog编程中常用的一种重要的递归数据结构 列表是一个有限的元素序列 实例: 所有Prolog术语都可以是列表的元素,一个非空的List应该含有两个元素:头元素(Head)和尾元素 ...

  5. OO 第二单元

    前言 ​ 第二单元 OO 作业的主题是多线程,课程组通过了电梯调度这个经典问题考察了多线程的调度. ​ 从第五次作业到第七次作业的迭代为,单部多线程可捎带电梯,多部多线程可捎带调度电梯(电梯属性相同) ...

  6. (十三)struts2的输入校验

    输入校验是web应用必须处理的问题,要防止用户的误输入和恶意非法输入.struts2给我们提供了非常强大的输入校验体系. 输入校验分为客户端校验和服务器端校验.一般开发中两者都用,但是服务端校验必须使 ...

  7. Java JVM 启动参数

    JVM 启动参数 java -Xmx4096m // 设置JVM最大可用内存为4096m. -Xms4096m // 设置JVM促使内存为4096m.此值可以设置与-Xmx相同,以避免每次垃圾回收完成 ...

  8. 曾侯乙编钟引发的遐想之Java设计模式:状态模式

    目录 示例 简单例子 改进代码 状态模式 定义 意图 主要解决问题 何时使用 优缺点 曾侯乙编钟 状态模式-命令模式-策略模式 示例 一个类对外提供了多个行为,同时该类对象有多种状态,不同状态下对外的 ...

  9. 7.bug生命周期

    new:测试发现并提交bug,状态为new/active; open: 分配bug到开发人员,状态为open: fixed:开发人员处理完bug,将状态改为fixed: closed/reopen:测 ...

  10. 【Oauth2.0】Oauth2.0

    一.什么是Oauth2.0? 1.Oauth2.0即(Open Authorization ),Oauth2.0是一个用于第三方授权的开放标准,是Oauth1.0的升级版本,相比1.0版本易于使用: ...