-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmi-6.cpp
128 lines (118 loc) · 2.79 KB
/
mi-6.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#include <cstring> // strcpy, strlen
#include <iostream>
using namespace std;
class Base {
char string[31];
int where;
public:
Base() {
strcpy(string, "ace");
where = 2;
cout << "Base ctor\n";
}
Base(char s[]) {
strcpy(string, s);
where = 0;
cout << "Base* ctor\n";
}
~Base() {
cout << "~Base dtor\n";
}
void show() {
cout << string << '\n';
}
void next() {
increment(where);
if (where >= (int) strlen(string))
where = 0;
}
virtual void increment(int &n) { n++; }
void access(char val) { string[where] = val; }
char access() { return string[where]; }
};
class Getter: virtual public Base {
public:
Getter() {
cout << "Getter ctor\n";
}
Getter(char *s) : Base(s) {
cout << "Getter* ctor\n";
}
~Getter() {
cout << "~Getter dtor\n";
}
void increment(int &n) {
n += 2;
cout << n << " Getter increment\n";
}
char get() {
char c = access();
next();
return c;
}
};
class Setter: virtual public Base {
public:
Setter() {
cout << "Setter ctor\n";
}
Setter(char *s) : Base(s) {
cout << "Setter* ctor\n";
}
~Setter() {
cout << "~Setter dtor\n";
}
void increment(int &n) {
n += 3;
cout << n << " Setter increment\n";
}
void set(char c) {
access(c);
next();
}
};
class Better: public Setter, public Getter {
int toggle;
public:
Better() {
toggle = 0;
cout << "Better ctor\n";
}
Better(char *s) : Base(s) {
toggle = 0;
cout << "Better* ctor\n";
}
~Better() {
cout << "~Better dtor\n";
}
void increment(int &n) {
if (toggle)
Setter::increment(n);
else
Getter::increment(n);
toggle = !toggle;
}
};
int main() {
cout << "\nmain creating 'x' - calling Better x(\"012345678901234567890\");\n";
Better x( (char*) "012345678901234567890");
cout << "main calling x.show();\n";
x.show();
cout << "\nmain calling x.set('f');\n";
x.set('f');
cout << "main calling x.show();\n";
x.show();
cout << "\nmain calling x.set(x.get());\n";
x.set(x.get());
cout << "main calling x.show();\n";
x.show();
cout << "\nmain calling x.set('a');\n";
x.set('a');
cout << "main calling x.show();\n";
x.show();
cout << "\nmain calling x.set('A');\n";
x.set('A');
cout << "main calling x.show();\n";
x.show();
cout << "\nmain class instance 'x' going out of scope - dtor's will be called --- NOTE order!\n";
}