Today's lab will introduce C++.

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

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
//Date:  November 2017
//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;
//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
//Date:  November 2017
//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.

Similarly,

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

In-class Quiz

During lab, there is a quiz. The password to access the quiz will be given during lab. To complete the quiz, log on to Blackboard (see Lab 1 for details on using Blackboard).

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:

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.