forked from aaronbloomfield/pdr
-
Notifications
You must be signed in to change notification settings - Fork 228
/
cpptest.cpp.html
199 lines (174 loc) · 22.9 KB
/
cpptest.cpp.html
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="GENERATOR" content="GNU source-highlight
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite">
<title>cpptest.cpp</title>
</head>
<body style="background-color:white">
<pre><i><span style="color:#9A1900">/*</span></i>
<i><span style="color:#9A1900"> * This program is designed to show some nuances of C++ syntax,</span></i>
<i><span style="color:#9A1900"> * reference usage, and the various methods in a class (copy</span></i>
<i><span style="color:#9A1900"> * constructor, operator=(), etc.).</span></i>
<i><span style="color:#9A1900"> *</span></i>
<i><span style="color:#9A1900"> * The addresses you get when running this program will most likley</span></i>
<i><span style="color:#9A1900"> * differ; that is expected. In fact, the addresses will differ upon</span></i>
<i><span style="color:#9A1900"> * successive executions of the program. If you are compiling this on</span></i>
<i><span style="color:#9A1900"> * a 64-bit machine, you may want to use the -m32 flag to g++ to</span></i>
<i><span style="color:#9A1900"> * compile it in 32-bit mode -- this keeps the pointers to 32 bits (as</span></i>
<i><span style="color:#9A1900"> * opposed to 64 bits).</span></i>
<i><span style="color:#9A1900"> *</span></i>
<i><span style="color:#9A1900"> * Copyright (c) 2012 by Aaron Bloomfield (</span></i><u><span style="color:#0000FF">aaron@virginia.edu</span></u><i><span style="color:#9A1900">). All</span></i>
<i><span style="color:#9A1900"> * rights reesrved.</span></i>
<i><span style="color:#9A1900"> */</span></i>
<b><span style="color:#000080">#include</span></b> <span style="color:#FF0000"><iostream></span>
<b><span style="color:#0000FF">using</span></b> <b><span style="color:#0000FF">namespace</span></b> std<span style="color:#990000">;</span>
<i><span style="color:#9A1900">//--------------------------------------------------</span></i>
<i><span style="color:#9A1900">// this would be in test.h</span></i>
<b><span style="color:#0000FF">class</span></b> <span style="color:#008080">test</span> <span style="color:#FF0000">{</span>
<b><span style="color:#0000FF">static</span></b> <span style="color:#009900">int</span> idcount<span style="color:#990000">;</span>
<b><span style="color:#0000FF">const</span></b> <span style="color:#009900">int</span> id<span style="color:#990000">;</span>
<span style="color:#009900">int</span> value<span style="color:#990000">;</span>
<b><span style="color:#0000FF">public</span></b><span style="color:#990000">:</span>
<b><span style="color:#000000">test</span></b><span style="color:#990000">();</span>
<b><span style="color:#000000">test</span></b><span style="color:#990000">(</span><span style="color:#009900">int</span> v<span style="color:#990000">);</span>
<b><span style="color:#000000">test</span></b><span style="color:#990000">(</span><b><span style="color:#0000FF">const</span></b> test<span style="color:#990000">&</span> x<span style="color:#990000">);</span>
<span style="color:#990000">~</span><b><span style="color:#000000">test</span></b><span style="color:#990000">();</span>
test<span style="color:#990000">&</span> <b><span style="color:#0000FF">operator</span></b><span style="color:#990000">=(</span><b><span style="color:#0000FF">const</span></b> test<span style="color:#990000">&</span> other<span style="color:#990000">);</span>
<b><span style="color:#0000FF">friend</span></b> ostream<span style="color:#990000">&</span> <b><span style="color:#0000FF">operator</span></b><span style="color:#990000"><<(</span>ostream<span style="color:#990000">&</span> out<span style="color:#990000">,</span> <b><span style="color:#0000FF">const</span></b> test<span style="color:#990000">&</span> f<span style="color:#990000">);</span>
<span style="color:#FF0000">}</span><span style="color:#990000">;</span>
<i><span style="color:#9A1900">//--------------------------------------------------</span></i>
<i><span style="color:#9A1900">// this would be in test.cpp</span></i>
<span style="color:#009900">int</span> test<span style="color:#990000">::</span>idcount <span style="color:#990000">=</span> <span style="color:#993399">0</span><span style="color:#990000">;</span>
test<span style="color:#990000">::</span><b><span style="color:#000000">test</span></b><span style="color:#990000">()</span> <span style="color:#990000">:</span> <b><span style="color:#000000">id</span></b> <span style="color:#990000">(</span>idcount<span style="color:#990000">++),</span> <b><span style="color:#000000">value</span></b><span style="color:#990000">(</span><span style="color:#993399">0</span><span style="color:#990000">)</span> <span style="color:#FF0000">{</span>
cout <span style="color:#990000"><<</span> <span style="color:#FF0000">"calling test(); object created is "</span> <span style="color:#990000"><<</span> <span style="color:#990000">*</span><b><span style="color:#0000FF">this</span></b>
<span style="color:#990000"><<</span> <span style="color:#FF0000">"; address is "</span> <span style="color:#990000"><<</span> <b><span style="color:#0000FF">this</span></b> <span style="color:#990000"><<</span> endl<span style="color:#990000">;</span>
<span style="color:#FF0000">}</span>
test<span style="color:#990000">::</span><b><span style="color:#000000">test</span></b><span style="color:#990000">(</span><span style="color:#009900">int</span> v<span style="color:#990000">)</span> <span style="color:#990000">:</span> <b><span style="color:#000000">id</span></b> <span style="color:#990000">(</span>idcount<span style="color:#990000">++),</span> <b><span style="color:#000000">value</span></b><span style="color:#990000">(</span>v<span style="color:#990000">)</span> <span style="color:#FF0000">{</span>
cout <span style="color:#990000"><<</span> <span style="color:#FF0000">"calling test("</span> <span style="color:#990000"><<</span> v <span style="color:#990000"><<</span> <span style="color:#FF0000">"); object created is "</span> <span style="color:#990000"><<</span> <span style="color:#990000">*</span><b><span style="color:#0000FF">this</span></b>
<span style="color:#990000"><<</span> <span style="color:#FF0000">"; address is "</span> <span style="color:#990000"><<</span> <b><span style="color:#0000FF">this</span></b> <span style="color:#990000"><<</span> endl<span style="color:#990000">;</span>
<span style="color:#FF0000">}</span>
test<span style="color:#990000">::</span><b><span style="color:#000000">test</span></b><span style="color:#990000">(</span><b><span style="color:#0000FF">const</span></b> test<span style="color:#990000">&</span> x<span style="color:#990000">)</span> <span style="color:#990000">:</span> <b><span style="color:#000000">id</span></b><span style="color:#990000">(</span>x<span style="color:#990000">.</span>id<span style="color:#990000">),</span> <b><span style="color:#000000">value</span></b><span style="color:#990000">(</span>x<span style="color:#990000">.</span>value<span style="color:#990000">)</span> <span style="color:#FF0000">{</span>
cout <span style="color:#990000"><<</span> <span style="color:#FF0000">"calling test(const test&) on "</span> <span style="color:#990000"><<</span> <span style="color:#990000">*</span><b><span style="color:#0000FF">this</span></b> <span style="color:#990000"><<</span> <span style="color:#FF0000">"; address is "</span> <span style="color:#990000"><<</span> <b><span style="color:#0000FF">this</span></b> <span style="color:#990000"><<</span> endl<span style="color:#990000">;</span>
<span style="color:#FF0000">}</span>
test<span style="color:#990000">::~</span><b><span style="color:#000000">test</span></b><span style="color:#990000">()</span> <span style="color:#FF0000">{</span>
cout <span style="color:#990000"><<</span> <span style="color:#FF0000">"calling ~test() on "</span> <span style="color:#990000"><<</span> <span style="color:#990000">*</span><b><span style="color:#0000FF">this</span></b> <span style="color:#990000"><<</span> endl<span style="color:#990000">;</span>
<span style="color:#FF0000">}</span>
test<span style="color:#990000">&</span> test<span style="color:#990000">::</span><b><span style="color:#0000FF">operator</span></b><span style="color:#990000">=(</span><b><span style="color:#0000FF">const</span></b> test<span style="color:#990000">&</span> other<span style="color:#990000">)</span> <span style="color:#FF0000">{</span>
cout <span style="color:#990000"><<</span> <span style="color:#FF0000">"calling operator=("</span> <span style="color:#990000"><<</span> other <span style="color:#990000"><<</span> <span style="color:#FF0000">")"</span> <span style="color:#990000"><<</span> endl<span style="color:#990000">;</span>
<span style="color:#008080">test</span> <span style="color:#990000">*</span>tmp <span style="color:#990000">=</span> <b><span style="color:#0000FF">new</span></b> <b><span style="color:#000000">test</span></b><span style="color:#990000">(</span>other<span style="color:#990000">);</span>
<b><span style="color:#0000FF">return</span></b> <span style="color:#990000">*</span>tmp<span style="color:#990000">;</span>
<span style="color:#FF0000">}</span>
ostream<span style="color:#990000">&</span> <b><span style="color:#0000FF">operator</span></b><span style="color:#990000"><<(</span>ostream<span style="color:#990000">&</span> out<span style="color:#990000">,</span> <b><span style="color:#0000FF">const</span></b> test<span style="color:#990000">&</span> f<span style="color:#990000">)</span> <span style="color:#FF0000">{</span>
out <span style="color:#990000"><<</span> <span style="color:#FF0000">"test[id="</span> <span style="color:#990000"><<</span> f<span style="color:#990000">.</span>id <span style="color:#990000"><<</span> <span style="color:#FF0000">",v="</span> <span style="color:#990000"><<</span> f<span style="color:#990000">.</span>value <span style="color:#990000"><<</span> <span style="color:#FF0000">"]"</span><span style="color:#990000">;</span>
<b><span style="color:#0000FF">return</span></b> out<span style="color:#990000">;</span>
<span style="color:#FF0000">}</span>
<i><span style="color:#9A1900">//--------------------------------------------------</span></i>
<i><span style="color:#9A1900">// this would be in main.cpp</span></i>
<span style="color:#008080">test</span> <b><span style="color:#000000">bar</span></b><span style="color:#990000">(</span><span style="color:#008080">test</span> param<span style="color:#990000">)</span> <span style="color:#FF0000">{</span>
<b><span style="color:#0000FF">return</span></b> <b><span style="color:#000000">test</span></b><span style="color:#990000">(</span><span style="color:#993399">10</span><span style="color:#990000">);</span>
<span style="color:#FF0000">}</span>
<span style="color:#009900">int</span> <b><span style="color:#000000">main</span></b><span style="color:#990000">()</span> <span style="color:#FF0000">{</span>
<i><span style="color:#9A1900">// this does NOT create a test object; instead, it creates a function</span></i>
<i><span style="color:#9A1900">// called a() that has no body (and thus C++ defaults it to return</span></i>
<i><span style="color:#9A1900">// 1). Note that there is no output statement from the default</span></i>
<i><span style="color:#9A1900">// constructor.</span></i>
<span style="color:#008080">test</span> <b><span style="color:#000000">a</span></b><span style="color:#990000">();</span>
cout <span style="color:#990000"><<</span> <span style="color:#FF0000">"attempted to create a: "</span> <span style="color:#990000"><<</span> a <span style="color:#990000"><<</span> endl<span style="color:#990000">;</span>
<i><span style="color:#9A1900">// output:</span></i>
<i><span style="color:#9A1900">// attempted to create a: 1</span></i>
<i><span style="color:#9A1900">// this does create a test object, calling the default constructor.</span></i>
<i><span style="color:#9A1900">// Since putting parenthesis in there for creating an object and</span></i>
<i><span style="color:#9A1900">// calling the default constructor has another meaning in C++</span></i>
<i><span style="color:#9A1900">// (specifically, creating a function), we have to omit the</span></i>
<i><span style="color:#9A1900">// parenthesis.</span></i>
cout <span style="color:#990000"><<</span> <span style="color:#FF0000">"----------------------------------------"</span> <span style="color:#990000"><<</span> endl<span style="color:#990000">;</span>
<span style="color:#008080">test</span> aa<span style="color:#990000">;</span>
cout <span style="color:#990000"><<</span> <span style="color:#FF0000">"created aa: "</span> <span style="color:#990000"><<</span> aa <span style="color:#990000"><<</span> endl<span style="color:#990000">;</span>
<i><span style="color:#9A1900">// output:</span></i>
<i><span style="color:#9A1900">// ----------------------------------------</span></i>
<i><span style="color:#9A1900">// calling test(); object created is test[id=0,v=0]; address is 0xff852a50</span></i>
<i><span style="color:#9A1900">// created aa: test[id=0,v=0]</span></i>
<i><span style="color:#9A1900">// this creates a test object, calling the specific constructor that</span></i>
<i><span style="color:#9A1900">// takes in a single int value</span></i>
cout <span style="color:#990000"><<</span> <span style="color:#FF0000">"----------------------------------------"</span> <span style="color:#990000"><<</span> endl<span style="color:#990000">;</span>
<span style="color:#008080">test</span> <b><span style="color:#000000">b</span></b><span style="color:#990000">(</span><span style="color:#993399">1</span><span style="color:#990000">);</span>
cout <span style="color:#990000"><<</span> <span style="color:#FF0000">"created b: "</span> <span style="color:#990000"><<</span> b <span style="color:#990000"><<</span> endl<span style="color:#990000">;</span>
<i><span style="color:#9A1900">// output:</span></i>
<i><span style="color:#9A1900">// ----------------------------------------</span></i>
<i><span style="color:#9A1900">// calling test(1); object created is test[id=1,v=1]; address is 0xff852a48</span></i>
<i><span style="color:#9A1900">// created b: test[id=1,v=1]</span></i>
<i><span style="color:#9A1900">// this creates two test objects via pointers and new</span></i>
cout <span style="color:#990000"><<</span> <span style="color:#FF0000">"----------------------------------------"</span> <span style="color:#990000"><<</span> endl<span style="color:#990000">;</span>
<span style="color:#008080">test</span> <span style="color:#990000">*</span>c <span style="color:#990000">=</span> <b><span style="color:#0000FF">new</span></b> <b><span style="color:#000000">test</span></b><span style="color:#990000">(</span><span style="color:#993399">2</span><span style="color:#990000">);</span>
cout <span style="color:#990000"><<</span> <span style="color:#FF0000">"created *c: "</span> <span style="color:#990000"><<</span> <span style="color:#990000">*</span>c <span style="color:#990000"><<</span> <span style="color:#FF0000">" at "</span> <span style="color:#990000"><<</span> c <span style="color:#990000"><<</span> endl<span style="color:#990000">;</span>
<span style="color:#008080">test</span> <span style="color:#990000">*</span>d <span style="color:#990000">=</span> <b><span style="color:#0000FF">new</span></b> test<span style="color:#990000">;</span>
cout <span style="color:#990000"><<</span> <span style="color:#FF0000">"created *d: "</span> <span style="color:#990000"><<</span> <span style="color:#990000">*</span>d <span style="color:#990000"><<</span> <span style="color:#FF0000">" at "</span> <span style="color:#990000"><<</span> d <span style="color:#990000"><<</span> endl<span style="color:#990000">;</span>
<i><span style="color:#9A1900">// output:</span></i>
<i><span style="color:#9A1900">// ----------------------------------------</span></i>
<i><span style="color:#9A1900">// calling test(2); object created is test[id=2,v=2]; address is 0xa009008</span></i>
<i><span style="color:#9A1900">// created *c: test[id=2,v=2] at 0xa009008</span></i>
<i><span style="color:#9A1900">// calling test(); object created is test[id=3,v=0]; address is 0xa009018</span></i>
<i><span style="color:#9A1900">// created *d: test[id=3,v=0] at 0xa009018</span></i>
<i><span style="color:#9A1900">// subroutine invocation. The copy constructor is invoked when the</span></i>
<i><span style="color:#9A1900">// actual parameter is copied into the formal parameter. The</span></i>
<i><span style="color:#9A1900">// subroutine then creates test[id=4,v=10]. The parameter that was</span></i>
<i><span style="color:#9A1900">// copied into the subroutine (via the copy constructor) is then</span></i>
<i><span style="color:#9A1900">// destructed upon termination of the subroutine.</span></i>
cout <span style="color:#990000"><<</span> <span style="color:#FF0000">"----------------------------------------"</span> <span style="color:#990000"><<</span> endl<span style="color:#990000">;</span>
cout <span style="color:#990000"><<</span> <span style="color:#FF0000">"about to invoke subroutine..."</span> <span style="color:#990000"><<</span> endl<span style="color:#990000">;</span>
<span style="color:#008080">test</span> e <span style="color:#990000">=</span> <b><span style="color:#000000">bar</span></b><span style="color:#990000">(*</span>c<span style="color:#990000">);</span>
cout <span style="color:#990000"><<</span> <span style="color:#FF0000">"finished invoking subroutine..."</span> <span style="color:#990000"><<</span> endl<span style="color:#990000">;</span>
<i><span style="color:#9A1900">// output:</span></i>
<i><span style="color:#9A1900">// ----------------------------------------</span></i>
<i><span style="color:#9A1900">// about to invoke subroutine...</span></i>
<i><span style="color:#9A1900">// calling test(const test&) on test[id=2,v=2]; address is 0xff852a38</span></i>
<i><span style="color:#9A1900">// calling test(10); object created is test[id=4,v=10]; address is 0xff852a40</span></i>
<i><span style="color:#9A1900">// calling ~test() on test[id=2,v=2]</span></i>
<i><span style="color:#9A1900">// finished invoking subroutine...</span></i>
<i><span style="color:#9A1900">// because this assignment happens in the same statement as the</span></i>
<i><span style="color:#9A1900">// declaration, it invokes the copy constructor</span></i>
cout <span style="color:#990000"><<</span> <span style="color:#FF0000">"----------------------------------------"</span> <span style="color:#990000"><<</span> endl<span style="color:#990000">;</span>
<span style="color:#008080">test</span> f <span style="color:#990000">=</span> b<span style="color:#990000">;</span>
<i><span style="color:#9A1900">// output:</span></i>
<i><span style="color:#9A1900">// ----------------------------------------</span></i>
<i><span style="color:#9A1900">// calling test(&test) on test[id=1,v=1]; address is 0xff852a30</span></i>
<i><span style="color:#9A1900">// we are only deleting one of the dynamically created test objects</span></i>
cout <span style="color:#990000"><<</span> <span style="color:#FF0000">"----------------------------------------"</span> <span style="color:#990000"><<</span> endl<span style="color:#990000">;</span>
cout <span style="color:#990000"><<</span> <span style="color:#FF0000">"about to delete a test object..."</span> <span style="color:#990000"><<</span> endl<span style="color:#990000">;</span>
<b><span style="color:#0000FF">delete</span></b> c<span style="color:#990000">;</span>
<i><span style="color:#9A1900">// output:</span></i>
<i><span style="color:#9A1900">// ----------------------------------------</span></i>
<i><span style="color:#9A1900">// about to delete a test object...</span></i>
<i><span style="color:#9A1900">// calling ~test() on test[id=2,v=2]</span></i>
<i><span style="color:#9A1900">// because this assignment does NOT happen in the same statement as</span></i>
<i><span style="color:#9A1900">// the declaration, the operator=() subroutine is called.</span></i>
cout <span style="color:#990000"><<</span> <span style="color:#FF0000">"----------------------------------------"</span> <span style="color:#990000"><<</span> endl<span style="color:#990000">;</span>
cout <span style="color:#990000"><<</span> <span style="color:#FF0000">"assignment..."</span> <span style="color:#990000"><<</span> endl<span style="color:#990000">;</span>
aa <span style="color:#990000">=</span> b<span style="color:#990000">;</span>
<i><span style="color:#9A1900">// output:</span></i>
<i><span style="color:#9A1900">// ----------------------------------------</span></i>
<i><span style="color:#9A1900">// assignment...</span></i>
<i><span style="color:#9A1900">// calling operator=(test[id=1,v=1])</span></i>
<i><span style="color:#9A1900">// calling test(&test) on test[id=1,v=1]; address is 0xa009008</span></i>
<i><span style="color:#9A1900">// upon termination of the main() function, all statically created</span></i>
<i><span style="color:#9A1900">// test objects are deallocated.</span></i>
cout <span style="color:#990000"><<</span> <span style="color:#FF0000">"----------------------------------------"</span> <span style="color:#990000"><<</span> endl<span style="color:#990000">;</span>
cout <span style="color:#990000"><<</span> <span style="color:#FF0000">"about to leave main..."</span> <span style="color:#990000"><<</span> endl<span style="color:#990000">;</span>
<i><span style="color:#9A1900">// output:</span></i>
<i><span style="color:#9A1900">// ----------------------------------------</span></i>
<i><span style="color:#9A1900">// about to leave main...</span></i>
<i><span style="color:#9A1900">// calling ~test() on test[id=1,v=1]</span></i>
<i><span style="color:#9A1900">// calling ~test() on test[id=4,v=10]</span></i>
<i><span style="color:#9A1900">// calling ~test() on test[id=1,v=1]</span></i>
<i><span style="color:#9A1900">// calling ~test() on test[id=0,v=0]</span></i>
<i><span style="color:#9A1900">// Note that d was created via dynamic memory allocation, so it is</span></i>
<i><span style="color:#9A1900">// not deallcoated by the program. The operating system will clean</span></i>
<i><span style="color:#9A1900">// up the used memory, but it does not call the destructor on d.</span></i>
<span style="color:#FF0000">}</span>
</pre>
</body>
</html>