Today's lab will introduce C++.

Software tools needed: web browser and the C++ compiler, g++ and a graphical editor, such as gEdit.

Quizzes & Code Reviews

See the quiz & code review page for details of the work due this week.

Using Python, Gradescope, and Blackboard

See Lab 1 for details on using Python, Gradescope, and Blackboard.

Using gcc

We will be using the 'Gnu Compiler Collection' (aka 'Gnu C Compiler'), or gcc, for our C++ programs. Originally designed for C programs, it was extended to include C++ programs and is one of the most common compilers for C/C++. The command, g++ calls gcc and automatically specifies the C++ library and treats all files as C++ (instead of the default as C files).

For C/C++ programs, we will do the following steps, at the command line:

Hello World

Let's write our first program that will say "Hello, World!". You can copy or type the program below into a editor window (either launch gEdit from the command line or open via the menu). Text after the '//' is a comment (just like the '#' in Python). You should keep the first 3 lines of comments for gradescope:

//Name:  Your name here
//Email:  Your email here
//Date:  November 2019
//My first program in C++

#include <iostream>        //The built-in library for input/output
using namespace std;       //The names of standard definitions

int main()                 //C++ programs all have a main() function
{
  cout << "Hello, World!"; //Print "Hello, World!" to the terminal
  return 0;                //Standard way to end function
}

Let's go through line-by-line:

Once you have it typed in, try compiling the program:

g++ hello.cpp -o hello
When it returns without an error, you can run it by typing:
./hello

When it compiles, and runs correctly (i.e. printing "Hello, World!" to the screen), see the Programming Problem List.

Simple I/O & Variables in C++

Unlike Python, you must declare variables before using them. For example, if you want a variable to hold the year, you would first declare it (i.e. request space for it):
int year;

Once we declare the variable, we can use it in the subsequent lines of the program:

//Another C++ program, demonstrating variables
#include <iostream>
using namespace std;

int main ()
{
  int year;
  cout << "Enter a number: ";
  cin >> year;
  cout << "Hello" << year << "!!\n\n";
  return 0;
}

For each variable, we need to specify what type of variable it is (whole number, real number, character, etc.) before we use it. For real numbers, we can use float, as in Python. We will introduce more data types next lab, but if you are curious, here is a chart of some common types. Our program above:

To summarize: cin is for input to the program, and cout is output from the program.

How could you modify this program to ask the user for a real (floating-point number)? Most arithmetic works the same in C++ as it does in Python. With this in mind, modify the above program to convert kilometers to miles. (Hint: see the Programming Problem List.)

Definite Loops in C++

C++ has for-loops that have are similar, but not identical to for-loops in Python. The basic format is:

int i;

for (i = 0; i < 10; i++)
{
   command1
   command2
   ...
}

For the loop above,

  1. The variable i first takes on the value 0.
  2. Next, check if the variable i < 10?
  3. If no, leave the loop.
  4. If yes, do the commands in the body of the loop.
  5. Add one to the variable i (abbreviated: i++).
  6. Go to #2.

Let's use a loop to write "Hello, World!" to the screen 10 times:

//Name:  Your name here
//Email:  Your email here
//Date:  November 2019
//Prints "Hello, World!" 10 times, using a loop

#include <iostream>
using namespace std;

int main ()
{
  int i;   //The index variable for the loop

  //Loop will repeat 10 times:
  for (i = 0; i < 10; i++)
  {
    cout << "Hello, World!\n";
  }
  return 0;
}

Note that cout doesn't automatically put output on a newline, so, we need to include the new line character ("\n") at the end of the line.

Follow the steps above to compile and run your program.

Next, modify the program so that it will print 10 times: the Hunter College motto ("Mihi cura futuri" which translates to: "The care of the future is mine"). When it compiles, and runs correctly, see the Programming Problem List.

More Useful Unix Commands

Now that we are creating executable programs, it's often useful to figure out what kind of file is in your directory. For example, if you were not sure what type of file, hello is, you could type at the command line:

file hello
file is the name of the command, and hello is the input parameter to the command. If you would like to find out the type of several files, you could type each separately:
file hello hello.cpp hello.py
(assuming all of those files are your working directory).

Or, you could use a "wildcard" that matches all files whose names match a pattern. For example:

file hello*
will tell you all type of every file that begins with the hello followed by 0 or more other characters.

Similarly,

file *
will tell you all type of every file in your current working directory.

It's often useful to figure out which version of a program you're using (since there could be multiple ones on your computer). To do that, there's a command, which that will tell which version of the program it's using by default. For example,

which g++
will show the location of the g++ program.

Getting g++ on Your Machine

We are using the gcc compiler for the C/C++ programming languages, which is freely available. We include directions below for downloading on your machine:

Accessing Your Files in the Windows Subsystem for Linux

Accessing where your files are stored may be a little tricky. To access your files in Windows, you must first change into the directory /mnt, which you can do with the command:
cd /mnt
You can now use cd to access your files in Windows. If any file or folder contains a space in it, you must put it in quotation marks e.g. "CS Projects". For example, if your files are stored in your documents folder you can access them by typing the command:
 cd /mnt/c/Users/*enter your username here*/Documents 
You want to enter the username set for your account after Users. This is likely different than the username you created for Ubuntu. If you don't know your username, search cmd in the Windows search bar and open it. Then type the command:
echo %username% 
This should be the username you want to put for *enter your username here*. If your username contains a space, you must put it in quotation marks e.g. "Alan Turing".

These instructions were written by Owen Kunhardt. You can find the full guide here.

What's Next?

If you finish the lab early, now is a great time to get a head start on the programming problems due early next week. There's instructors to help you, and you already have Python up and running. The Programming Problem List has problem descriptions, suggested reading, and due dates next to each problem.