forked from uva-cs/pdr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lifecycle.cpp.html
179 lines (144 loc) · 23.5 KB
/
lifecycle.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
<!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>lifecycle.cpp</title>
</head>
<body style="background-color:white">
<pre><b><span style="color:#000080">#include</span></b> <span style="color:#FF0000"><iostream></span>
<b><span style="color:#000080">#include</span></b> <span style="color:#FF0000"><string></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">// Declare the MyObject class</span></i>
<b><span style="color:#0000FF">class</span></b> <span style="color:#008080">MyObject</span> <span style="color:#FF0000">{</span>
<b><span style="color:#0000FF">public</span></b><span style="color:#990000">:</span>
<b><span style="color:#0000FF">static</span></b> <span style="color:#009900">int</span> numObjs<span style="color:#990000">;</span>
<b><span style="color:#000000">MyObject</span></b><span style="color:#990000">();</span> <i><span style="color:#9A1900">// default constructor</span></i>
<b><span style="color:#000000">MyObject</span></b><span style="color:#990000">(</span><span style="color:#008080">string</span> n<span style="color:#990000">);</span> <i><span style="color:#9A1900">// constructor with name parameter</span></i>
<b><span style="color:#000000">MyObject</span></b><span style="color:#990000">(</span><b><span style="color:#0000FF">const</span></b> MyObject<span style="color:#990000">&</span> rhs<span style="color:#990000">);</span> <i><span style="color:#9A1900">// copy constructor</span></i>
<span style="color:#990000">~</span><b><span style="color:#000000">MyObject</span></b><span style="color:#990000">();</span> <i><span style="color:#9A1900">// destructor</span></i>
<span style="color:#008080">string</span> <b><span style="color:#000000">getName</span></b><span style="color:#990000">()</span> <b><span style="color:#0000FF">const</span></b><span style="color:#990000">;</span>
<span style="color:#009900">void</span> <b><span style="color:#000000">setName</span></b><span style="color:#990000">(</span><b><span style="color:#0000FF">const</span></b> string<span style="color:#990000">&</span> newName<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> output<span style="color:#990000">,</span> <b><span style="color:#0000FF">const</span></b> MyObject<span style="color:#990000">&</span> obj<span style="color:#990000">);</span>
<b><span style="color:#0000FF">private</span></b><span style="color:#990000">:</span>
<span style="color:#008080">string</span> name<span style="color:#990000">;</span>
<span style="color:#009900">int</span> id<span style="color:#990000">;</span>
<span style="color:#FF0000">}</span><span style="color:#990000">;</span>
<i><span style="color:#9A1900">// Define the member functions we declared above</span></i>
<i><span style="color:#9A1900">// Remember: member functions are defined as Class::method()</span></i>
<i><span style="color:#9A1900">// Constructors and destructors - no return types</span></i>
MyObject<span style="color:#990000">::</span><b><span style="color:#000000">MyObject</span></b><span style="color:#990000">()</span> <span style="color:#FF0000">{</span>
<i><span style="color:#9A1900">// Important: notice how we don't define 'name' or 'id' here</span></i>
<i><span style="color:#9A1900">// Instead, we're setting the variables that we declared in MyObject</span></i>
name <span style="color:#990000">=</span> <span style="color:#FF0000">"--default--"</span><span style="color:#990000">;</span>
id <span style="color:#990000">=</span> <span style="color:#990000">++</span>numObjs<span style="color:#990000">;</span>
cout <span style="color:#990000"><<</span> <span style="color:#FF0000">"MyObject Default constructor: "</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>
MyObject<span style="color:#990000">::</span><b><span style="color:#000000">MyObject</span></b><span style="color:#990000">(</span><span style="color:#008080">string</span> n<span style="color:#990000">)</span> <span style="color:#FF0000">{</span>
name <span style="color:#990000">=</span> n<span style="color:#990000">;</span>
id <span style="color:#990000">=</span> <span style="color:#990000">++</span>numObjs<span style="color:#990000">;</span>
cout <span style="color:#990000"><<</span> <span style="color:#FF0000">"MyObject Parameter constructor: "</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>
MyObject<span style="color:#990000">::</span><b><span style="color:#000000">MyObject</span></b><span style="color:#990000">(</span><b><span style="color:#0000FF">const</span></b> MyObject<span style="color:#990000">&</span> rhs<span style="color:#990000">)</span> <span style="color:#FF0000">{</span>
name <span style="color:#990000">=</span> rhs<span style="color:#990000">.</span>name<span style="color:#990000">;</span>
id <span style="color:#990000">=</span> <span style="color:#990000">++</span>numObjs<span style="color:#990000">;</span>
cout <span style="color:#990000"><<</span> <span style="color:#FF0000">"MyObject Copy constructor: "</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>
MyObject<span style="color:#990000">::~</span><b><span style="color:#000000">MyObject</span></b><span style="color:#990000">()</span> <span style="color:#FF0000">{</span>
cout <span style="color:#990000"><<</span> <span style="color:#FF0000">"MyObject Destructor: "</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>
<i><span style="color:#9A1900">// Regular methods, with return types</span></i>
<span style="color:#008080">string</span> MyObject<span style="color:#990000">::</span><b><span style="color:#000000">getName</span></b><span style="color:#990000">()</span> <b><span style="color:#0000FF">const</span></b> <span style="color:#FF0000">{</span>
<b><span style="color:#0000FF">return</span></b> name<span style="color:#990000">;</span>
<span style="color:#FF0000">}</span>
<span style="color:#009900">void</span> MyObject<span style="color:#990000">::</span><b><span style="color:#000000">setName</span></b><span style="color:#990000">(</span><b><span style="color:#0000FF">const</span></b> string<span style="color:#990000">&</span> newName<span style="color:#990000">)</span> <span style="color:#FF0000">{</span>
name <span style="color:#990000">=</span> newName<span style="color:#990000">;</span>
<span style="color:#FF0000">}</span>
<i><span style="color:#9A1900">// Let cout know how to print MyObjects</span></i>
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> output<span style="color:#990000">,</span> <b><span style="color:#0000FF">const</span></b> MyObject<span style="color:#990000">&</span> obj<span style="color:#990000">)</span> <span style="color:#FF0000">{</span>
<i><span style="color:#9A1900">// output in format: ("object name", id)</span></i>
<b><span style="color:#0000FF">return</span></b> output <span style="color:#990000"><<</span> <span style="color:#FF0000">"(</span><span style="color:#CC33CC">\"</span><span style="color:#FF0000">"</span> <span style="color:#990000"><<</span> obj<span style="color:#990000">.</span>name <span style="color:#990000"><<</span> <span style="color:#FF0000">"</span><span style="color:#CC33CC">\"</span><span style="color:#FF0000">, "</span> <span style="color:#990000"><<</span> obj<span style="color:#990000">.</span>id <span style="color:#990000"><<</span> <span style="color:#FF0000">")"</span><span style="color:#990000">;</span>
<span style="color:#FF0000">}</span>
<i><span style="color:#9A1900">// Prototypes for non-member functions we define later on</span></i>
<i><span style="color:#9A1900">// These are *non-member* functions because we didn't declare them as part of the MyObject class</span></i>
<span style="color:#008080">MyObject</span> <b><span style="color:#000000">getMaxMyObj</span></b><span style="color:#990000">(</span><b><span style="color:#0000FF">const</span></b> <span style="color:#008080">MyObject</span> o1<span style="color:#990000">,</span> <b><span style="color:#0000FF">const</span></b> <span style="color:#008080">MyObject</span> o2<span style="color:#990000">);</span>
<span style="color:#009900">int</span> <b><span style="color:#000000">cmpMyObj</span></b><span style="color:#990000">(</span><b><span style="color:#0000FF">const</span></b> <span style="color:#008080">MyObject</span> o1<span style="color:#990000">,</span> <b><span style="color:#0000FF">const</span></b> <span style="color:#008080">MyObject</span> o2<span style="color:#990000">);</span>
<span style="color:#009900">void</span> <b><span style="color:#000000">swapMyObj</span></b><span style="color:#990000">(</span>MyObject<span style="color:#990000">&</span> o1<span style="color:#990000">,</span> MyObject<span style="color:#990000">&</span> o2<span style="color:#990000">);</span>
<i><span style="color:#9A1900">// Static variables</span></i>
<span style="color:#009900">int</span> MyObject<span style="color:#990000">::</span>numObjs <span style="color:#990000">=</span> <span style="color:#993399">0</span><span style="color:#990000">;</span> <i><span style="color:#9A1900">// static member for all objects in class</span></i>
<b><span style="color:#0000FF">static</span></b> <span style="color:#008080">MyObject</span> <b><span style="color:#000000">staticObj</span></b><span style="color:#990000">(</span><span style="color:#FF0000">"I'm static, outside of main"</span><span style="color:#990000">);</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>
cout <span style="color:#990000"><<</span> <span style="color:#FF0000">"--PART 1: Start of main--"</span> <span style="color:#990000"><<</span> endl<span style="color:#990000">;</span>
cout <span style="color:#990000"><<</span> <span style="color:#FF0000">"--Defining o1, o2(</span><span style="color:#CC33CC">\"</span><span style="color:#FF0000">Bob</span><span style="color:#CC33CC">\"</span><span style="color:#FF0000">)--"</span> <span style="color:#990000"><<</span> endl<span style="color:#990000">;</span>
<span style="color:#008080">MyObject</span> o1<span style="color:#990000">,</span> <b><span style="color:#000000">o2</span></b><span style="color:#990000">(</span><span style="color:#FF0000">"Bob"</span><span style="color:#990000">);</span>
cout <span style="color:#990000"><<</span> <span style="color:#FF0000">"--Defining o3(o2)--"</span> <span style="color:#990000"><<</span> endl<span style="color:#990000">;</span>
<span style="color:#008080">MyObject</span> <b><span style="color:#000000">o3</span></b><span style="color:#990000">(</span>o2<span style="color:#990000">);</span>
cout <span style="color:#990000"><<</span> <span style="color:#FF0000">"--Defining array of 3 objects--"</span> <span style="color:#990000"><<</span> endl<span style="color:#990000">;</span>
<span style="color:#008080">MyObject</span> array<span style="color:#990000">[</span><span style="color:#993399">3</span><span style="color:#990000">];</span>
cout <span style="color:#990000"><<</span> endl<span style="color:#990000">;</span>
cout <span style="color:#990000"><<</span> <span style="color:#FF0000">"--PART 2: call function using call-by-value, return int--"</span> <span style="color:#990000"><<</span> endl<span style="color:#990000">;</span>
<b><span style="color:#000000">cmpMyObj</span></b><span style="color:#990000">(</span>o1<span style="color:#990000">,</span> o3<span style="color:#990000">);</span>
cout <span style="color:#990000"><<</span> <span style="color:#FF0000">"--call function using call-by-value, return MyObject--"</span> <span style="color:#990000"><<</span> endl<span style="color:#990000">;</span>
<b><span style="color:#000000">getMaxMyObj</span></b><span style="color:#990000">(</span>o1<span style="color:#990000">,</span> o3<span style="color:#990000">);</span>
cout <span style="color:#990000"><<</span> endl<span style="color:#990000">;</span>
cout <span style="color:#990000"><<</span> <span style="color:#FF0000">"--PART 3: o1: "</span> <span style="color:#990000"><<</span> o1 <span style="color:#990000"><<</span> endl<span style="color:#990000">;</span>
<span style="color:#FF0000">{</span>
cout <span style="color:#990000"><<</span> <span style="color:#FF0000">" --entering new block, define new o1(Sally)--"</span> <span style="color:#990000"><<</span> endl<span style="color:#990000">;</span>
<span style="color:#008080">MyObject</span> <b><span style="color:#000000">o1</span></b><span style="color:#990000">(</span><span style="color:#FF0000">"Sally"</span><span style="color:#990000">);</span>
o1<span style="color:#990000">.</span><b><span style="color:#000000">setName</span></b><span style="color:#990000">(</span><span style="color:#FF0000">"Sally"</span><span style="color:#990000">);</span>
cout <span style="color:#990000"><<</span> <span style="color:#FF0000">" o1: "</span> <span style="color:#990000"><<</span> o1 <span style="color:#990000"><<</span> <span style="color:#FF0000">"</span><span style="color:#CC33CC">\t</span><span style="color:#FF0000">o2: "</span> <span style="color:#990000"><<</span> o2 <span style="color:#990000"><<</span> endl<span style="color:#990000">;</span>
cout <span style="color:#990000"><<</span> <span style="color:#FF0000">" --call swap function using call-by-reference--"</span> <span style="color:#990000"><<</span> endl<span style="color:#990000">;</span>
<b><span style="color:#000000">swapMyObj</span></b><span style="color:#990000">(</span>o1<span style="color:#990000">,</span> o2<span style="color:#990000">);</span>
cout <span style="color:#990000"><<</span> <span style="color:#FF0000">" --were their values swapped?--"</span> <span style="color:#990000"><<</span> endl<span style="color:#990000">;</span>
cout <span style="color:#990000"><<</span> <span style="color:#FF0000">" o1: "</span> <span style="color:#990000"><<</span> o1 <span style="color:#990000"><<</span> <span style="color:#FF0000">"</span><span style="color:#CC33CC">\t</span><span style="color:#FF0000">o2: "</span> <span style="color:#990000"><<</span> o2 <span style="color:#990000"><<</span> endl<span style="color:#990000">;</span>
cout <span style="color:#990000"><<</span> <span style="color:#FF0000">" --leaving new block--"</span> <span style="color:#990000"><<</span> endl<span style="color:#990000">;</span>
<span style="color:#FF0000">}</span>
cout <span style="color:#990000"><<</span> <span style="color:#FF0000">"o1: "</span> <span style="color:#990000"><<</span> o1 <span style="color:#990000"><<</span> endl<span style="color:#990000">;</span>
cout <span style="color:#990000"><<</span> endl<span style="color:#990000">;</span>
cout <span style="color:#990000"><<</span> <span style="color:#FF0000">"--PART 4: Define reference var: { MyObject& oref=o1; } --"</span> <span style="color:#990000"><<</span> endl<span style="color:#990000">;</span>
<span style="color:#FF0000">{</span>
MyObject<span style="color:#990000">&</span> oref<span style="color:#990000">=</span>o1<span style="color:#990000">;</span>
<span style="color:#FF0000">}</span>
cout <span style="color:#990000"><<</span> <span style="color:#FF0000">"-- was anything constructed/destructed?--"</span> <span style="color:#990000"><<</span> endl<span style="color:#990000">;</span>
cout <span style="color:#990000"><<</span> endl<span style="color:#990000">;</span>
cout <span style="color:#990000"><<</span> <span style="color:#FF0000">"--PART 5: new and delete--"</span> <span style="color:#990000"><<</span> endl<span style="color:#990000">;</span>
cout <span style="color:#990000"><<</span> <span style="color:#FF0000">"--use new to create one object, then array of 2 objects--"</span> <span style="color:#990000"><<</span> endl<span style="color:#990000">;</span>
<span style="color:#008080">MyObject</span> <span style="color:#990000">*</span>op1 <span style="color:#990000">=</span> <b><span style="color:#0000FF">new</span></b> MyObject<span style="color:#990000">,</span> <span style="color:#990000">*</span>op2 <span style="color:#990000">=</span> <b><span style="color:#0000FF">new</span></b> MyObject<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">"--use delete to remove that one object--"</span> <span style="color:#990000"><<</span> endl<span style="color:#990000">;</span>
<b><span style="color:#0000FF">delete</span></b> op1<span style="color:#990000">;</span>
cout <span style="color:#990000"><<</span> <span style="color:#FF0000">"--use delete [] to remove that array of 2 objects --"</span> <span style="color:#990000"><<</span> endl<span style="color:#990000">;</span>
<b><span style="color:#0000FF">delete</span></b><span style="color:#990000">[]</span> op2<span style="color:#990000">;</span>
cout <span style="color:#990000"><<</span> endl<span style="color:#990000">;</span>
cout <span style="color:#990000"><<</span> <span style="color:#FF0000">"--LAST PART: End of main--"</span> <span style="color:#990000"><<</span> endl<span style="color:#990000">;</span>
<b><span style="color:#0000FF">return</span></b> <span style="color:#993399">0</span><span style="color:#990000">;</span>
<span style="color:#FF0000">}</span>
<i><span style="color:#9A1900">// Define the functions we declared earlier</span></i>
<span style="color:#009900">int</span> <b><span style="color:#000000">cmpMyObj</span></b><span style="color:#990000">(</span><b><span style="color:#0000FF">const</span></b> <span style="color:#008080">MyObject</span> o1<span style="color:#990000">,</span> <b><span style="color:#0000FF">const</span></b> <span style="color:#008080">MyObject</span> o2<span style="color:#990000">)</span> <span style="color:#FF0000">{</span>
<span style="color:#008080">string</span> name1 <span style="color:#990000">=</span> o1<span style="color:#990000">.</span><b><span style="color:#000000">getName</span></b><span style="color:#990000">(),</span> name2 <span style="color:#990000">=</span> o2<span style="color:#990000">.</span><b><span style="color:#000000">getName</span></b><span style="color:#990000">();</span>
<b><span style="color:#0000FF">if</span></b> <span style="color:#990000">(</span>name1 <span style="color:#990000">==</span> name2<span style="color:#990000">)</span> <span style="color:#FF0000">{</span>
<b><span style="color:#0000FF">return</span></b> <span style="color:#993399">0</span><span style="color:#990000">;</span>
<span style="color:#FF0000">}</span> <b><span style="color:#0000FF">else</span></b> <b><span style="color:#0000FF">if</span></b> <span style="color:#990000">(</span>name1 <span style="color:#990000"><</span> name2<span style="color:#990000">)</span> <span style="color:#FF0000">{</span>
<b><span style="color:#0000FF">return</span></b> <span style="color:#990000">-</span><span style="color:#993399">1</span><span style="color:#990000">;</span>
<span style="color:#FF0000">}</span> <b><span style="color:#0000FF">else</span></b> <span style="color:#FF0000">{</span>
<b><span style="color:#0000FF">return</span></b> <span style="color:#993399">1</span><span style="color:#990000">;</span>
<span style="color:#FF0000">}</span>
<span style="color:#FF0000">}</span>
<span style="color:#008080">MyObject</span> <b><span style="color:#000000">getMaxMyObj</span></b><span style="color:#990000">(</span><b><span style="color:#0000FF">const</span></b> <span style="color:#008080">MyObject</span> o1<span style="color:#990000">,</span> <b><span style="color:#0000FF">const</span></b> <span style="color:#008080">MyObject</span> o2<span style="color:#990000">)</span> <span style="color:#FF0000">{</span>
<span style="color:#008080">string</span> name1 <span style="color:#990000">=</span> o1<span style="color:#990000">.</span><b><span style="color:#000000">getName</span></b><span style="color:#990000">(),</span> name2 <span style="color:#990000">=</span> o2<span style="color:#990000">.</span><b><span style="color:#000000">getName</span></b><span style="color:#990000">();</span>
<b><span style="color:#0000FF">if</span></b> <span style="color:#990000">(</span>name1 <span style="color:#990000">>=</span> name2<span style="color:#990000">)</span> <span style="color:#FF0000">{</span>
<b><span style="color:#0000FF">return</span></b> o1<span style="color:#990000">;</span>
<span style="color:#FF0000">}</span> <b><span style="color:#0000FF">else</span></b> <span style="color:#FF0000">{</span>
<b><span style="color:#0000FF">return</span></b> o2<span style="color:#990000">;</span>
<span style="color:#FF0000">}</span>
<span style="color:#FF0000">}</span>
<span style="color:#009900">void</span> <b><span style="color:#000000">swapMyObj</span></b><span style="color:#990000">(</span>MyObject<span style="color:#990000">&</span> o1<span style="color:#990000">,</span> MyObject<span style="color:#990000">&</span> o2<span style="color:#990000">)</span> <span style="color:#FF0000">{</span>
<span style="color:#008080">MyObject</span> <b><span style="color:#000000">tmp</span></b><span style="color:#990000">(</span>o1<span style="color:#990000">);</span>
o1<span style="color:#990000">.</span><b><span style="color:#000000">setName</span></b><span style="color:#990000">(</span>o2<span style="color:#990000">.</span><b><span style="color:#000000">getName</span></b><span style="color:#990000">());</span>
o2<span style="color:#990000">.</span><b><span style="color:#000000">setName</span></b><span style="color:#990000">(</span>tmp<span style="color:#990000">.</span><b><span style="color:#000000">getName</span></b><span style="color:#990000">());</span>
<span style="color:#FF0000">}</span>
</pre>
</body>
</html>