我是靠谱客的博主 善良向日葵,这篇文章主要介绍C++Primer第五版 7.3.4节练习,现在分享给大家,希望可以做个参考。

练习7.32:定义你自己的Screen和Window_mgr,其中clear是window_mgr的成员,是Screen的友元。
答:见云盘程序 练习7.32.cpp
练习7.32

复制代码
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
/* *练习7.32 *2015/7/13 *问题描述:练习7.32:定义你自己的Screen和Window_mgr,其中clear是window_mgr的成员,是Screen的友元。 *说明:仅说明写的程序用 ,写得不好 *作者:Nick Feng *邮箱:nickgreen23@163.com * */ #include <iostream> #include <string> #include <vector> using namespace std; class Window_mgr{ public: void clear(); }; void Window_mgr::clear(){ cout << "clearing..." << endl; } class Screen{ public: friend class Window_mgr; friend void Window_mgr::clear(); }; int main() { Window_mgr a; a.clear(); return 0; }

练习7.32-1

复制代码
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/* *练习7.32 *2015/7/14 *问题描述:练习7.32:定义你自己的Screen和Window_mgr,其中clear是window_mgr的成员,是Screen的友元。 *功能:做了点改进,便于理解。 *作者:Nick Feng *邮箱:nickgreen23@163.com * */ #include <iostream> #include <string> #include <vector> using namespace std; class Screen{ public: typedef std::string::size_type pos; Screen() = default; 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); Screen &set(char); Screen &set(pos, pos, char); Screen &display(std::ostream &os) { do_display(os); return *this; } const Screen &display(std::ostream &os) const { do_display(os); return *this; } //extern void Window_mgr::clear(ScreenIndex); //friend void Window_mgr::clear(ScreenIndex); //friend void Window_mgr::clear(ScreenIndex); //extern class Window_mgr; friend class Window_mgr; //extern void Window_mgr::clear(ScreenIndex); private: pos cursor = 0; pos height = 0, width = 0; std::string contents; void do_display(std::ostream &os) const { os << contents;} }; inline Screen &Screen::move(pos r, pos c) { pos row = r * width; cursor = row + c; return *this; } char Screen::get(pos r, pos c) const { pos row = r * width; return contents[row + c]; } inline Screen &Screen::set(char c) { contents[cursor] = c; return *this; } inline Screen &Screen::set(pos r, pos col, char ch) { contents[r * width + col] = ch; return *this; } //typedef std::vector<Screen>::size_type ScreenIndex; class Window_mgr { public: typedef std::vector<Screen>::size_type ScreenIndex; void clear(ScreenIndex); void clear(); private: std::vector<Screen> screens{Screen(24, 80,' ')}; }; void Window_mgr::clear(ScreenIndex i) { Screen &s = screens[i]; s.contents = string(s.height * s.width, ' '); } void Window_mgr::clear() { cout << "Nothing" << endl; } int main() { Screen test(5,5,'x'); test.display(cout); Window_mgr a; a.clear(0); cout << endl; return 0; }

最后

以上就是善良向日葵最近收集整理的关于C++Primer第五版 7.3.4节练习的全部内容,更多相关C++Primer第五版内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(50)

评论列表共有 0 条评论

立即
投稿
返回
顶部