Today's lab will introduce C++.
Software tools needed: web browser and the C++ compiler, g++ and a graphical editor, such as gEdit.
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:
gedit hello.cpp
g++ hello.cpp -o hello
./hello
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:
//Name: Your name here //Date: November 2017 //My first program in C++These three lines are comments, containing information about the program.
#include <iostream> //The built-in library for input/output using namespace std; //The names of standard definitionsThese two lines include commands that allow you to print to the screen and read input that the user has entered, using standard names and defintions. The #include statement is similar in functionality to the import statements in Python.
int main () //C++ programs all have a main() functionCode in C++ programs occurs in functions. Each program is expected to have a main() function that is called (invoked) when the program is run. The int before the function name is what type of data is returned when the function ends. In this case, the function will return an int or integer value.
{C++ uses the curly brackets ('{' and '}') to indicate blocks of code, instead of indenting. Indenting code is considered good style, but you have more freedom to decide how much to indent versus Python. For function definitions and multiple lines of code in a block, the use of the brackets is required.
cout << "Hello, World!"; //Print "Hello, World!" to the terminalcout << is used for printing to the terminal. Note that, as a general rule, command line in C++ ends in a semi-colon.
return 0; //Standard way to end functionWhen this function ends, it returns the integer 0, a common value for a program ending normally.
}The closing bracket for the definition of the function, main(). All opening brackets must have a matching closing bracket.
Once you have it typed in, try compiling the program:
g++ hello.cpp -o helloWhen 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.
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.)
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,
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.
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 hellofile 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.
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:
sudo apt-get updateto update the package list, and then
sudo apt-get install g++to install g++ and associated libraries and packages.
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.