Today's lab will introduce C++.
Software tools needed: web browser and the C++ compiler, g++ and a graphical editor, such as gEdit.
See Lab 1 for details on using Python, Gradescope, and Blackboard.
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.cppwhich opens the gEdit editor with the file hello.cpp
g++ hello.cpp -o hellowhich compiles hello.cpp and store the executable in the ouput file hello
./hellowhich runs the executable file, 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 //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:
//Name: Your name here //Email: Your email here //Date: November 2019 //My first program in C++These four 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, commands in C++ end 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;
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.)
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 //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.
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 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.
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 updateto update the package list, and then
sudo apt install g++to install g++ and associated libraries and packages.
cd /mntYou 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*/DocumentsYou 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".
If you finish the lab early, now is a great time to get a head start on the programming problems. 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.