-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdemo6.cpp
104 lines (93 loc) · 2.09 KB
/
demo6.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
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
97
98
99
100
101
102
103
104
#include <iostream>
using namespace std;
class Person
{
public:
// Person(const string &name, const string &addr): name(name),address(addr){} // 不出错
// Person(const string &name, const string &addr)
// {
// name = name; // 出错
// address = addr;
// }
Person(const string &name, const string &address)
{
this->name = name; // 不出错
this->address = address;
}
string getName() const
{ return name; }
string getAddress() const
{ return address; }
private:
string name;
string address;
};
class Screen
{
public:
typedef string::size_type index;
Screen(const index ht=0, const index wd=0): contents(ht*wd, 'A'),cursor(0),height(ht),width(wd){}
char get() const { return contents[cursor]; }
char get(const index r, const index c) const
{
index row = r * width;
return contents[row + c];
}
Screen& move(const index r, const index c);
Screen& set(const index r, const index c, const char ch);
Screen& set(const char ch);
const Screen& display(ostream &os) const
{
++ access_ctr;
do_display(os);
return *this;
}
Screen& display(ostream &os)
{
++ access_ctr;
do_display(os);
return *this;
}
private:
string contents;
index cursor;
index height, width;
mutable size_t access_ctr; // 计数
void do_display(ostream &os) const
{ os<<contents; }
};
Screen& Screen::move(const index r, const index c)
{
index row = r * width;
cursor = row + c;
return *this; // 必须用this指针
}
Screen& Screen::set(const index r, const index c, const char ch)
{
index row = r * width;
contents[row + c] = ch;
return *this;
}
Screen& Screen::set(const char ch)
{
contents[cursor] = ch;
return *this;
}
int main()
{
Person p("hello", "world");
cout<<p.getName()<<", "<<p.getAddress()<<endl;
Screen myScreen(5,3);
cout<<myScreen.get()<<endl;
myScreen.set(3, 2, 'B');
cout<<myScreen.get(3, 2)<<endl;
// 把下面两行合成一行:myScreen.move(2, 3).set('C')
// myScreen.move(2, 3);
// myScreen.set('C');
myScreen.move(2, 3).set('C').display(cout).set('K').display(cout);
cout<<endl;
cout<<myScreen.get()<<endl;
myScreen.display(cout);
cout<<endl;
return 0;
}