-
Notifications
You must be signed in to change notification settings - Fork 487
/
index.html
344 lines (342 loc) · 16.8 KB
/
index.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="" xml:lang="">
<head>
<meta charset="utf-8" />
<meta name="generator" content="pandoc" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes" />
<title>PDR: Laboratory 1: Introduction to C++</title>
<style>
code{white-space: pre-wrap;}
span.smallcaps{font-variant: small-caps;}
span.underline{text-decoration: underline;}
div.column{display: inline-block; vertical-align: top; width: 50%;}
div.hanging-indent{margin-left: 1.5em; text-indent: -1.5em;}
ul.task-list{list-style: none;}
.display.math{display: block; text-align: center; margin: 0.5rem auto;}
</style>
<link rel="stylesheet" href="../../markdown.css" />
<!--[if lt IE 9]>
<script src="//cdnjs.cloudflare.com/ajax/libs/html5shiv/3.7.3/html5shiv-printshiv.min.js"></script>
<![endif]-->
</head>
<body>
<h1 id="pdr-laboratory-1-introduction-to-c">PDR: Laboratory 1:
Introduction to C++</h1>
<p><a href="../index.html">Go up to the Labs table of contents
page</a></p>
<h3 id="objective">Objective</h3>
<p>This laboratory is intended to get you up to speed quickly with both
C++ and Unix.</p>
<h3 id="background">Background</h3>
<p>There will be a lab every week, which consists of three parts: a
pre-lab, an in-lab, and a post-lab. The due dates are all listed on the
<a href="../../uva/labduedates.html">lab due dates</a> page. This will
all be discussed in this lab.</p>
<h3 id="tutorial">Tutorial</h3>
<p>Most labs will have a tutorial. You are expected to complete this
tutorial before beginning the lab, as the lab will use concepts from
each tutorial.</p>
<p>The tutorial for this lab is <a
href="../../tutorials/01-intro-unix/index.html">Tutorial 1: Introduction
to UNIX</a>, which will help you set up your UNIX environment.</p>
<h3 id="recommended-readings">Recommended Readings</h3>
<p>We have attempted to compile a collection of readings that go over
topics covered in this course. Readings are always optional and are
there for you to use as you see fit.</p>
<ul>
<li>Introduction to C++ section on the <a
href="../../docs/readings.html">Readings page</a></li>
</ul>
<h2 id="procedure">Procedure</h2>
<h3 id="pre-lab">Pre-lab</h3>
<ol type="1">
<li>Complete <a href="../../tutorials/01-intro-unix/index.html">Tutorial
1: Introduction to UNIX</a></li>
<li>Write a recursive function to compute exponentiation</li>
<li>Investigate the C++ object lifecycle</li>
<li>Learn about the submission system</li>
<li>Files to download: <a href="lifecycle.cpp.html">lifecycle.cpp</a>
(<a href="lifecycle.cpp">src</a>)</li>
<li>Files to submit: xToN.cpp</li>
</ol>
<h3 id="in-lab">In-lab</h3>
<ol type="1">
<li>Ask the TAs if you have any questions about the pre-lab code or
Unix</li>
<li>Separate the object lifecycle code to better follow C++
conventions</li>
<li>Files to download: <a href="svtest.cpp.html">svtest.cpp</a> (<a
href="svtest.cpp">src</a>), <a href="svutil.cpp.html">svutil.cpp</a> (<a
href="svutil.cpp">src</a>), <a href="svutil.h.html">svutil.h</a> (<a
href="svutil.h">src</a>), and <a
href="lifecycle.cpp.html">lifecycle.cpp</a> (<a
href="lifecycle.cpp">src</a>)</li>
<li>Files to submit: LifeCycle.cpp, LifeCycle.h, and
TestLifeCycle.cpp</li>
</ol>
<h3 id="post-lab">Post-lab</h3>
<ol type="1">
<li>Write a simple bank account class</li>
<li>Files to download: <a
href="TestBankAccount.cpp.html">TestBankAccount.cpp</a> (<a
href="TestBankAccount.cpp">src</a>)</li>
<li>Files to submit: bankAccount.h, bankAccount.cpp,
TestBankAccount.cpp</li>
</ol>
<hr />
<h2 id="pre-lab-1">Pre-lab</h2>
<p>Complete <a href="../../tutorials/01-intro-unix/index.html">Tutorial
1: Introduction to UNIX</a> before proceeding. Remember that you should
always complete the tutorial before starting the lab.</p>
<p>For the pre-lab, you will need to write a <strong>recursive</strong>
function called <code>xton()</code> to compute <em>x^n</em> for
non-negative integers <em>n</em>. Assume that <em>x^0=1</em>. Put this
function in a program with a <code>main()</code> function. Your program
should prompt the user for two integer values, and raise the first to
the power of the second by calling your <code>xton()</code> function. To
keep the code simple, you can assume that your program will only be
called with valid inputs.</p>
<p>The file should be called xToN.cpp, and should be submitted to the
pre-lab 1 assignment in the submission system – more details below.</p>
<p>Note that your program should take in <strong>exactly two inputs and
nothing else</strong>. We are going to run it through an automated
grading script prior to the TAs grading it – if your program takes in a
different number of inputs, you will receive points off.</p>
<p>To help you out, we have provided an example C++ file below. You may
find this example helpful in writing your function to compute
<em>x^n</em> and the <code>main()</code> function to call it.</p>
<pre><code>#include <iostream>
using namespace std;
int sum (int a, int b) {
int tmp = a;
tmp += b;
return tmp;
}
int main () {
int x, y, z;
cin >> x;
cin >> y;
z = sum (x, y);
cout << x << " + " << y << " = " << z << endl;
return 0;
}</code></pre>
<h3 id="sample-execution-run">Sample Execution Run</h3>
<p>In order to make autograding easier, we expect you to follow the
output and output formats specified in these write-ups. If you don’t
follow these format specifications, your submission may not be graded
correctly becase the autograder won’t be able to locate your answers.
Some assignments use test harnesses and provided code that take care of
the output format for you, so you need to worry about handling the I/O
for those.</p>
<p>For this lab, only print out the result of your power function on the
last line. <strong>Do not</strong> include any cout statements prompting
the user for input. An example I/O for your program is shown below.</p>
<p>Input</p>
<pre><code>2
3</code></pre>
<p>Output</p>
<pre><code>8</code></pre>
<p>Lastly, take a look at the object life-cycle code (<a
href="lifecycle.cpp.html">lifecycle.cpp</a> (<a
href="lifecycle.cpp">src</a>)). Use it as a mechanism for understanding
how various aspects of C++ work and try stepping through it by hand. Use
the <a href="../../docs/readings.html">readings</a>, the web, and any
other C++ references to help you look up parts of the program you do not
understand.</p>
<h3 id="assignment-submission">Assignment submission</h3>
<p>All assignments will be submitted through the CS2150 class page on
gradescope, which can be found <a
href="https://www.gradescope.com/courses/138861">here</a> or login
through Collab.</p>
<p>Every file submitted, including text files, should include your name,
email ID, the date, and the name of the file in a header comment at the
beginning of the file.</p>
<p>There is no check to make sure you have submitted all of the correct
files – on the ‘Procedure’ page (always at the top of the lab document),
we clearly state which files should be submitted for each lab part. For
example, for this pre-lab, you should submit just the following file for
pre-lab 1: xToN.cpp.</p>
<p>Each assignment has 3 dates: an open date (when you can start
submitting the assignment), a due date (when it’s due), and a close date
(the last point that you can submit the assignment). The dates are
listed for the week of the lab – the lab week starts on a Sunday and
ends on a Saturday. In particular, the due date for the pre-labs, as
well as the open date for the in-labs and post-labs is when the first
lab section starts. The due date for the post-labs is the start of
Friday’s first lecture.</p>
<p>More information on open dates, due dates, and close dates can be
found on the <a href="../../uva/labduedates.html">lab due dates</a>
page.</p>
<p>There are a number of rules that we will strictly follow:</p>
<ul>
<li>Pre-labs are due at the same time for everybody, regardless of your
lab section; that time is the beginning of the FIRST Tuesday lab.</li>
<li>In-labs are due at the end of the day on Tuesday.</li>
<li>Any late lab part will receive 25% off (for just that part) for the
first 24 hours (or part thereof) that it is late, after which no credit
will be given. Note that a computer program does this deduction – so if
your lab is 1 second late, it still receives 25% off.</li>
<li><strong>No credit will be given for a lab component which does not
compile.</strong> If you are having problems with your code, you should
comment out parts so that it does compile – you will receive more credit
for a compilable program that has part of the code commented out than
you would for a program that does not compile. See the <a
href="../../docs/compilation.html">compilation</a> for hints as to how
to get your code to compile.</li>
</ul>
<h3 id="resubmitting-your-assignment">Resubmitting your assignment</h3>
<p>If you submit your assignment, and you realize you made a mistake
(didn’t submit all the files, etc.), you can resubmit your assignment as
many times as you want. The date of submission is the date you
re-submitted your assignment – so if you resubmit your assignment after
the due date to add one more file, the <strong>ENTIRE</strong>
assignment will have the late submission date. We only look at the most
recent submission.</p>
<p><strong><em>Note that you have to bring your computer to
in-lab!</em></strong></p>
<hr />
<h2 id="in-lab-1">In-lab</h2>
<h3 id="general-in-lab-procedure">General In-lab Procedure</h3>
<p>The purpose of the labs is to allow you to work through the lab
activity, and if you encounter questions or problems, ask for TA
assistance. Be sure to include your name, email ID, the date, and the
name of the file in a banner comment at the beginning of each file you
submit.</p>
<h3 id="understanding-c">Understanding C++</h3>
<ol type="1">
<li>Ask the TAs if you have questions about your <em>x^n</em>
function.</li>
<li>Ask the TAs if you have questions about using Unix.</li>
<li>Object Lifecycle Program
<ul>
<li>Download the <a href="lifecycle.cpp.html">lifecycle.cpp</a> (<a
href="lifecycle.cpp">src</a>) file</li>
<li>Compile with clang++ (i.e., <code>clang++ lifecycle.cpp</code>) and
execute the program.</li>
<li>Break lifecycle.cpp into multiple files. It may be helpful to look
at the <a href="../../slides/01-cpp.html">class slides</a>. Compile and
run this program. Note that to compile a C++ program with multiple
files, just put them on the same line: <code>clang++ LifeCycle.cpp
TestLifeCycle.cpp</code> (you usually leave out the .h files when
compiling). Add the descriptive header at the top of the file containing
your identifying information, and name them as follows:
<ul>
<li>LifeCycle.h</li>
<li>LifeCycle.cpp</li>
<li>TestLifeCycle.cpp</li>
</ul></li>
<li>Comment out the prototype for <code>getMaxMyObj()</code>. Recompile.
What happens and why? Talk to TA if you are unsure. Now un-comment the
prototype.</li>
<li>Look at the output of lifecycle.cpp. Write at least one question
about something in this program. Write your questions in a text file
named lifecycle.questions.txt. Remember to include the header comment at
the top of the file.</li>
</ul></li>
<li>Using C++ vector container class with strings
<ul>
<li>There are 3 files on this repo that demonstrate the use of
multi-file programs and the use of the vector collection class in C++:
<a href="svtest.cpp.html">svtest.cpp</a> (<a href="svtest.cpp">src</a>),
<a href="svutil.cpp.html">svutil.cpp</a> (<a href="svutil.cpp">src</a>),
and <a href="svutil.h.html">svutil.h</a> (<a href="svutil.h">src</a>).
The class in those files is similar to the ArrayList class in the Java
standard library (or Java’s own Vector class). Compile the program in
Unix and run it. The command <code>clang++ svutil.cpp svtest.cpp</code>
will compile the program.</li>
<li>Comment out the <code>#include <iostream></code> preprocessor
directive in the file svtest.cpp, and then rebuild the program. Was
there an error?</li>
<li>Now undo what you did in the previous step, but now comment out the
<code>using namespace std;</code> in svutil.h, and rebuild the program.
Was there an error? What objects are now undeclared and why?</li>
</ul></li>
</ol>
<h3 id="capitalization">Capitalization</h3>
<p>Under Windows, the case of a file name is ignored – thus,
lifecycle.cpp, LifeCycle.cpp, and LIFECYCLE.CPP all refer to the same
file. However, it is <strong>NOT</strong> true for Linux, which is what
we will use to test and grade your code. Thus, if your file is called
‘LifeCycle.h’, and you have (in your TestLifeCycle.cpp file) a line that
states: <code>#include "lifecycle.h"</code>, then your program will NOT
work under Linux (since case DOES matter with file names). Since your
code does not compile, you will get zero credit. So make sure your file
names match!</p>
<h3 id="troubleshooting">Troubleshooting</h3>
<p>Does your program not compile? Here are a few things to try – these
are problems that previous students have encountered.</p>
<ul>
<li>You must have <code>#include <iostream></code> at the
beginning of each file (well, really each file that uses
<code>cin</code> and <code>cout</code>, but it doesn’t hurt to have it
at the beginning of each file).</li>
<li>Likewise, you will need <code>using namespace std;</code> at the
beginning of each file.</li>
<li>Is your subroutine a method (i.e. part of a class) or a function
(i.e. not part of a class)? Make sure you know which is which! A method
has its name in the form classname::methodname, whereas a function
doesn’t have a class name or the double-colon.</li>
<li>Make sure that your subroutine names (whether they be function names
or method names) are consistent between the .h files and the .cpp
files</li>
<li>Have a compiler error that you don’t understand? We have the
translation! See the <a
href="../../docs/compilation.html">compilation</a> page, which lists
common compiler error messages, what they mean, and how to solve them.
If you don’t see yours listed there, let us know, and we’ll add it.</li>
</ul>
<hr />
<h2 id="post-lab-1">Post-lab</h2>
<p>Your task for this postlab is to create a bank account class using
C++. To begin, download <a
href="TestBankAccount.cpp.html">TestBankAccount.cpp</a> (<a
href="TestBankAccount.cpp">src</a>), then create two files,
<code>bankAccount.cpp</code> and <code>bankAccount.h</code>.</p>
<h4 id="bankaccount.h">bankAccount.h</h4>
<p>bankAccount.h will contain the <strong>headers</strong> of the
methods that will be implemented in bankAccount.cpp. We normally include
in .h files just the declarations (i.e., prototypes) of classes,
constants, function, etc., but not definitions of C++ methods (i.e. the
bodies of the methods). However, when implementing template classes,
this is something that is necessary to make the class compile
successfully.</p>
<p>Your bank account class will have the following methods and fields
that you will need to implement:</p>
<ul>
<li><code>bankAccount();</code> – A default constructor that will be
called when a program creates a bankAccount object. This constructor
should set the bankAccount balance to $0.00;</li>
<li><code>bankAccount(double amount);</code> – A constructor that will
be called when a program creates a bankAccount object with an initial
balance.</li>
<li><code>~bankAccount();</code> – A destructor that will be called when
an object is deleted from program memory. You must have this in your .h
and .cpp file, but you can leave the implementation empty</li>
<li><code>double withdraw(double amount);</code> – A method that
withdraws the specified amount of money from your bank account. The
method will return the new amount in the bank account after the withdraw
goes through. If the withdraw amount is too high, don’t withdraw any
amount and return the current balance in the account. You can assume
that <code>amount >= 0</code>.</li>
<li><code>double deposit(double amount);</code> – A method that deposits
the specified amount of money from your bank account. The method will
return the new amount in the bank account after the deposit goes
through. You can assume that <code>amount >= 0</code>.</li>
<li><code>double getBalance();</code> – A method that will return the
balance currently in the bank account.</li>
<li><code>double balance;</code> – A <strong>private</strong> variable
that will hold the amount of money in the bank account.</li>
</ul>
<h4 id="bankaccount.cpp">bankAccount.cpp</h4>
<p>bankAccount.cpp will contain the <strong>bodies</strong> of the
methods that were declared in bankAccount.h. In order to do so, you must
<code>#include "bankAccount.h"</code> in order to gain access to the
method headers.</p>
<p>Complete the code in <code>bankAccount.h</code> and
<code>bankAccount.cpp</code>, then test your code with test harness
provided in <code>TestBankAccount.cpp</code>.</p>
<p>You can compile your bank account with <code>clang++ bankAccount.cpp
TestBankAccount.cpp</code>. If you try to compile the bankAccount.h
file, it won’t work correctly.</p>
</body>
</html>