Introduction to C++
Jonah Warrenjonah AT parsons DOT edu
http://www.feedtank.com/2005/cpp
Lab 1
In this lab, we will go over how to do the following.
- Login to the 'A' server and use basic UNIX commands.
- Create and edit a file from UNIX using the text editor 'nano' or 'emacs.'
- Write a simple program that will print to the screen.
- Compile your program using the g++ compiler.
- Run your program.
- Zip a number of files together, encrypting it, and posting it on the web.
Moving around on the 'A' server.
- Using a ssh program (putty, secure ssh, terminal) login to a.parsons.edu using your ftp username and password.
- You should see a prompt and cursor that should look like the following:
This is a unix prompt. From this prompt, you can execute UNIX commands. You only really need to be aware of a few of these commands.[name@a name]
Read this page on the UNIX commands you need to know.
You will notice a listing of the files on your web account.
You will notice that the command line has change to reflect your current directory.
[name@a name cprog]
Using nano (or emacs) to create a new file.
- Now we want write our first C++ program. We are going to use a UNIX text editor called nano to do this.
- (I actually prefer emacs, though nano is a bit simpler. Learn emacs here.)
- Type nano HelloWorld.cpp and press return to create a new file.
- This will open up the nano text editor.
- Type in the following line of code
// HelloWorld.cpp. My first C++ program.
- This code will not do anything. When you write //, it means that C++ will ignore the text you write on that line.
- Type CTL-X to exit nano. It will ask you if you want to save your changes, say 'yes.' It will ask you what you want to name your file. Type return.
- Type ls. You should see the HelloWorld.cpp, the file we just created.
Writing your first C++ program.
- Reopen the file we just created by typing nano HelloWorld.cpp.
- Type in the following code after the comment we wrote.
// HelloWorld.cpp. My first C++ program.#include <iostream> using namespace std; int main() { cout << "HelloWorld in C++.\n"; return 0; }
Compiling your first C++ program.
- To compile our program we are going to use the g++ compiler. Reference on g++ and gcc (the C compiler) here.
- To compile your programs, use the following format g++ filename.cpp -o filename
- Thus, to compile the code we just wrote, type the following command:
g++ HelloWorld.cpp -o HelloWorld
Running your first C++ program.
- Running the program once its compiled is easy. Use the following format ./ filename
- Thus, to compile the code we just wrote, type the following command:
./HelloWorld
HelloWorld in C++.
Preparing to post your files. Zipping and encrypting.
- To zip and encrypt your files use the zip command in UNIX. Use the following format zip -e destfile filename1 filename2...
- Thus, to zip the HelloWorld.cpp file just wrote and its executable file, type the following command:
zip -e lab_01 HelloWorld.cpp HelloWorld