C++primer 7.3.4节练习

练习7.32

头文件

 1 #pragma once
 2 #include <vector>
 3
 4
 5 class Screen {
 6     friend class Window_mgr;
 7     typedef std::string::size_type pos;
 8 public:
 9     //构造函数
10     Screen() = default;
11     Screen(pos h, pos w) : height(h), width(w), contents(h * w, ‘ ‘) {};
12     Screen(pos h, pos w, char c) : height(h), width(w), contents(h * w, c) {};
13     Screen(std::string &s);
14     //成员函数
15     Screen &move(pos r, pos c);
16     Screen &set(pos r, pos c, char ch);
17     char getChar(pos x, pos y) const;
18     char getChar()const {
19         return contents[(x_axis - 1)*y_axis + x_axis];
20     }
21     Screen &display(std::ostream &os)
22     {
23         os << contents;
24         return *this;
25     }
26
27
28 private:
29     std::string contents;
30     pos height = 0;
31     pos width = 0;
32     pos x_axis = 0;
33     pos y_axis = 0;
34     //void do_display(std::ostream &os) const { os << contents; }
35 };
36
37 class Window_mgr {
38 public:
39     void clear(int i);
40 private:
41     std::vector<Screen> screens{ Screen(3,4,‘#‘) };
42 };

源文件

 1 #include <iostream>
 2 #include <string>
 3 #include "screen.h"
 4
 5 using namespace std;
 6
 7 int main()
 8 {
 9     Screen myScreen(5, 5, ‘X‘);
10     myScreen.move(4, 0).set(3, 4, ‘#‘).display(cout);
11     cout << "\n";
12     myScreen.display(cout);
13     cout << "\n";
14     Window_mgr w1;
15     w1.clear(0);
16     system("pause");
17     return 0;
18 }
19
20 Screen::Screen(string &s)
21 {
22     (*this).contents = s;
23 }
24
25 inline Screen &Screen::move(pos r, pos c)
26 {
27     x_axis = r;
28     y_axis = c;
29     return *this;
30     // TODO: 在此处插入 return 语句
31 }
32
33 inline Screen &Screen::set(pos r, pos c, char ch)
34 {
35     contents[(r - 1) * c + r] = ch;
36     return *this;
37     // TODO: 在此处插入 return 语句
38 }
39
40 inline  char Screen::getChar(pos x, pos y) const
41 {
42     return contents[(x_axis - 1) * y_axis + x_axis];
43     // TODO: 在此处插入 return 语句
44 }
45
46 void Window_mgr::clear(int i)
47 {
48     Screen &s = screens[i];
49     s.contents = string(s.height * s.width, ‘ ‘);
50 }

其实这里是有问题的,问题在于如果使用window_mgr类的成员函数作为Screen类的友元,在定义顺序完全正确的情况下,由于在window_mgr内需要使用到Screen类型,但是此时并没有对Screen类进行声明定义,所以会发生错误,以当前的知识储备还不能解决之一问题,需要更多的学习

时间: 2024-08-26 06:18:33

C++primer 7.3.4节练习的相关文章

C++primer 15.7.3节练习

练习15.26 写的时候不小心写到了派生类Disc_quote,其实是一样的,主要明白原理即可 1 #include <iostream> 2 #include <string> 3 #include <utility> 4 #include <memory> 5 #include <vector> 6 7 using namespace std; 8 9 class Quote { 10 public: 11 Quote() : bookNo(

C++primer 15.2.2节练习

练习15.4 a)错误,一个类不能派生它本身 b)正确,Derived从他的基类Base派生,且规定派生类从基类继承的数据成员对于派生类的用户是不可见. c)错误,派生类的声明与其他类相差不大,声明中包含类名但是不包含他的派生列表: 练习15.5 1 #include <iostream> 2 #include <string> 3 #include <utility> 4 #include <memory> 5 #include <vector>

C++primer 14.2.1节练习

练习14.6 1 #include <iostream> 2 #include <string> 3 #include <memory> 4 #include <vector> 5 #include <algorithm> 6 #include <numeric> 7 8 using namespace std; 9 10 class Sales_data { 11 friend istream& operator >&

C++primer 15.7.4节练习

练习15.27 1 #include <iostream> 2 #include <string> 3 #include <utility> 4 #include <memory> 5 #include <vector> 6 7 using namespace std; 8 9 class Quote { 10 public: 11 Quote() : bookNo(""), price(0.0) { cout <<

C++ Primer 6.5.3节练习

练习 6.47: 改写6.3.2节(第205页)练习中使用递归输出vector内容的程序,使其有条件地输出与执行过程有关的信息.例如,每次调用时输出vector对象的大小.分别在打开和关闭调试器的情况下编译并执行这个程序. ///这一题需要在前面输出vector内容的程序中,添加新的功能---->有条件地输出与执行过程有关的信息. 为了简便解答该题,我们采用vector引用,vector的类型是string型,在过程中不改变容器大小. 经过测试得到一个现象,编译器将会按照 #define DEB

C++primer 11.3.5节练习

练习11.27 对于multimap来说统计关键字出现的次数用count会很好,而对于map来说寻找关键字来说更加妥当: 练习11.28 1 #include <iostream> 2 #include <string> 3 #include <set> 4 #include <map> 5 #include <algorithm> 6 #include <vector> 7 #include <algorithm> 8

C++primer 11.2.3节练习

练习11.12 1 #include<iostream> 2 #include<string> 3 #include <iostream> 4 #include <vector> 5 #include <algorithm> 6 #include <list> 7 #include <functional> 8 #include <iterator> 9 #include <map> 10 #inc

C++primer 7.5.3节练习

练习7.43 网上的答案 1 #include <iostream> 2 using std::cout; 3 using std::endl; 4 5 class Nodefault 6 { 7 public: 8 Nodefault(int i) 9 { 10 val = i; 11 } 12 int val; 13 }; 14 15 class C 16 { 17 public: 18 Nodefault nd; 19 C(int i = 0) : nd(i) { } 20 }; 21

C++primer 7.1.3节练习

练习7.6 1 #include <iostream> 2 #include <string> 3 #include "factmain.h" 4 using namespace std; 5 6 struct Sales_data { 7 string isbn() const { return bookNo; } 8 Sales_data &combine(const Sales_data&); 9 string bookNo; 10 uns

c++ primer 6.5.1节练习答案

练习6.40 a)正确 b)错误,一旦某个形参被赋予了默认值,他后面的所有形参都必须有默认值. 练习6.41 a)错误,ht没有默认实参,而a的实参列表里也没有给出实参: b)合法,调用init(24 ,10 ,' '): c)虽然合法,但是与程序猿的设计初衷不符,*会转换成十进制的数43,相当于调用init(14, 43, ' '): 练习6.42 1 string make_plural(size_t ctr, const string &word, const string &end