Go up to the Labs table of contents page
This laboratory is intended to get you up to speed quickly with both C++ and Unix.
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 lab due dates page. This will all be discussed in this lab.
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.
The tutorial for this lab is Tutorial 1: Introduction to UNIX, which will help you set up your UNIX environment.
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.
Complete Tutorial 1: Introduction to UNIX before proceeding. Remember that you should always complete the tutorial before starting the lab.
For the pre-lab, you will need to write a recursive
function called xton()
to compute x^n for
non-negative integers n. Assume that x^0=1. Put this
function in a program with a main()
function. Your program
should prompt the user for two integer values, and raise the first to
the power of the second by calling your xton()
function. To
keep the code simple, you can assume that your program will only be
called with valid inputs.
The file should be called xToN.cpp, and should be submitted to the pre-lab 1 assignment in the submission system – more details below.
Note that your program should take in exactly two inputs and nothing else. 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.
To help you out, we have provided an example C++ file below. You may
find this example helpful in writing your function to compute
x^n and the main()
function to call it.
#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;
}
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.
For this lab, only print out the result of your power function on the last line. Do not include any cout statements prompting the user for input. An example I/O for your program is shown below.
Input
2
3
Output
8
Lastly, take a look at the object life-cycle code (lifecycle.cpp (src)). Use it as a mechanism for understanding how various aspects of C++ work and try stepping through it by hand. Use the readings, the web, and any other C++ references to help you look up parts of the program you do not understand.
All assignments will be submitted through the CS2150 class page on gradescope, which can be found here or login through Collab.
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.
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.
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.
More information on open dates, due dates, and close dates can be found on the lab due dates page.
There are a number of rules that we will strictly follow:
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 ENTIRE assignment will have the late submission date. We only look at the most recent submission.
Note that you have to bring your computer to in-lab!
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.
clang++ lifecycle.cpp
) and
execute the program.clang++ LifeCycle.cpp
TestLifeCycle.cpp
(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:
getMaxMyObj()
. Recompile.
What happens and why? Talk to TA if you are unsure. Now un-comment the
prototype.clang++ svutil.cpp svtest.cpp
will compile the program.#include <iostream>
preprocessor
directive in the file svtest.cpp, and then rebuild the program. Was
there an error?using namespace std;
in svutil.h, and rebuild the program.
Was there an error? What objects are now undeclared and why?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 NOT 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: #include "lifecycle.h"
, 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!
Does your program not compile? Here are a few things to try – these are problems that previous students have encountered.
#include <iostream>
at the
beginning of each file (well, really each file that uses
cin
and cout
, but it doesn’t hurt to have it
at the beginning of each file).using namespace std;
at the
beginning of each file.Your task for this postlab is to create a bank account class using
C++. To begin, download TestBankAccount.cpp (src), then create two files,
bankAccount.cpp
and bankAccount.h
.
bankAccount.h will contain the headers 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.
Your bank account class will have the following methods and fields that you will need to implement:
bankAccount();
– A default constructor that will be
called when a program creates a bankAccount object. This constructor
should set the bankAccount balance to $0.00;bankAccount(double amount);
– A constructor that will
be called when a program creates a bankAccount object with an initial
balance.~bankAccount();
– 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 emptydouble withdraw(double amount);
– 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 amount >= 0
.double deposit(double amount);
– 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 amount >= 0
.double getBalance();
– A method that will return the
balance currently in the bank account.double balance;
– A private variable
that will hold the amount of money in the bank account.bankAccount.cpp will contain the bodies of the
methods that were declared in bankAccount.h. In order to do so, you must
#include "bankAccount.h"
in order to gain access to the
method headers.
Complete the code in bankAccount.h
and
bankAccount.cpp
, then test your code with test harness
provided in TestBankAccount.cpp
.
You can compile your bank account with clang++ bankAccount.cpp
TestBankAccount.cpp
. If you try to compile the bankAccount.h
file, it won’t work correctly.