PDR: Laboratory 1: Introduction to C++

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.



  1. Complete Tutorial 1: Introduction to UNIX
  2. Write a recursive function to compute exponentiation
  3. Investigate the C++ object lifecycle
  4. Learn about the submission system
  5. Files to download: lifecycle.cpp (src)
  6. Files to submit: xToN.cpp


  1. Ask the TAs if you have any questions about the pre-lab code or Unix
  2. Separate the object lifecycle code to better follow C++ conventions
  3. Files to download: svtest.cpp (src), svutil.cpp (src), svutil.h (src), and lifecycle.cpp (src)
  4. Files to submit: LifeCycle.cpp, LifeCycle.h, and TestLifeCycle.cpp


  1. Write a simple bank account class
  2. Files to download: TestBankAccount.cpp (src)
  3. Files to submit: bankAccount.h, bankAccount.cpp, TestBankAccount.cpp


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;

Sample Execution Run

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.





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.

Assignment submission

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:

Resubmitting your assignment

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!


General In-lab Procedure

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.

Understanding C++

  1. Ask the TAs if you have questions about your x^n function.
  2. Ask the TAs if you have questions about using Unix.
  3. Object Lifecycle Program
  4. Using C++ vector container class with strings


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.


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.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.