forked from demon90s/CppStudy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexercise_7_23.cpp
56 lines (44 loc) · 1.23 KB
/
exercise_7_23.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/*
* 练习7.23:编写你自己的Screen类。
*/
#ifndef SCREEN_H
#define SCREEN_H
#include <string>
#include <iostream>
class Screen
{
public:
//typedef std::string::size_type pos;
using pos = std::string::size_type;
Screen() = default; // 因为Screen有另一个构造函数,所以本函数是必须的
// cursor被其类内初始值初始化为0
Screen(pos ht, pos wd, char c) : height(ht), width(wd), contents(ht * wd, c) {}
char get() const { return contents[cursor]; } // 读取光标处的字符,隐式内联
inline char get(pos ht, pos wd) const; // 显示内联
Screen &move(pos r, pos c); // 可以在之后设置为内联
private:
pos cursor = 0;
pos height = 0, width = 0;
std::string contents;
mutable size_t access_get_ctr; // 即使在一个const对象内也能被修改
};
inline
Screen& Screen::move(pos r, pos c)
{
pos row = r * width; // 计算行的位置
cursor = row + c; // 在行内将光标移动到指定的列
++access_get_ctr;
return *this;
}
char Screen::get(pos r, pos c) const
{
pos row = r * width; // 计算行的位置
return contents[row + c]; // 返回给定列的字符
}
int main()
{
Screen screen(24, 80, 'A');
std::cout << screen.get() << std::endl;
return 0;
}
#endif