#ifndef HELLO_H #define HELLO_H int main(); #endif
#include "hello.h" #include <iostream> #include <string> using namespace std; int main() { string sName; cout << "Hello World!" << endl; cout << "What is your name?" << endl << "> "; getline( cin, sName ); cout << "Hello " << sName << "." << endl; return 0; }
Download "hello.cpp" and "hello.h".
Open up a shell (command line) to the directory where these two files are and run the following command:
g++ -c -o hello.o hello.cpp
You should now have what is called an object file named "hello.o". The "-c" option tells g++ not to go all the way through with linking the executable (we will do this manually next). The "-o" option allows you to specify what the output file should be named, in this case "hello.o". You then list all of the source files to be compiled, in this case, just "hello.cpp".
To generate the executable:
g++ -o hello hello.o
You should now have an executable file named "hello" (on Windows, it will be named "hello.exe"). The options are very similar. "-o" gives the output name "hello". Then, you pass all the object files that will be linked together.
Running the executable you just generated should give something in the form:
$ ./hello Hello World! What is your name? > David Hello David. $
C++ code (and C for that matter) tends to be broken up into two files. The first, known as a "header" file (extension ".h", or ".hpp") is for code that may need to be seen (included) by other source files. The other one (extension ".cpp") is normally where the actual code is placed.
Let us start with the header file:
#ifndef HELLO_H #define HELLO_H /* ... header code here ... */ #endif
Any line in C that begins with a pound sign (#) is what is known as a pre-processor directive. You will find the above three directives in almost all header files out there. They ensure that the code in your header file does not get duplicated, included multiple times.
int main();
The purpose of this line is to tell all the code after it that there *will be* a function of this name (main) with these parameters (none) that returns this type (int). The compiler will make sure you hold up your end of this promise.
On to the source file:
#include "hello.h" #include <iostream> #include <string>
You should get used to always including your own header file first, then any system headers you may need. In this case, we include <iostream>, which will give us the "cin" and "cout" streams allowing us to exchange data with the user, and <string>, which will let us store a string of characters in our program's memory.
using namespace std;
This line is a bit of a mixed blessing. Without it, everything that is in the namespace "std" would have to be prefixed with that namespace in order to be used. This includes "std::string", "std::cin", and "std::cout". Someday, however, you will forget to include this line, and spend some time wondering why all of a sudden your compiler doesn't know what "cout" is.
int main() { /* ... code goes here ... */ }
This is the actual definition of the "main" function that you declared in the header file. Between the curly brackets ( { } ), you will place the actual code that gets executed inside your function.
"main" is a special function in c and c++, it is the so-called entry point of your program when run as an executable. Basically, this is where you will start.
string sName;
This line will set aside some (static) memory where we can place a string of characters.
cout << "Hello World!" << endl; cout << "What is your name?" << endl << "> ";
Think of "cout" as the terminal that is the user of your program. "<<" is a stream operator indicating you are sending something to that terminal, and "Hello World!" is a literal string. You can stack up those operators on a single line. "endl" is a special variable that will send a newline ("\n") to "cout".
getline( cin, sName );
The "getline" function will acquire input from a stream ("cin") and place it in a string ("sName") until it reaches a new line (the user presses enter). Thus, whatever the user types in there prompt, until they hit enter, will get stored in our string variable "sName".
cout << "Hello " << sName << "." << endl;
You can send string variables to cout as well as literals.
return 0;
"return" is a keyword that will send whatever expression follows it back to the function that called the current one. In this case, the integer "0" will be sent back to the operating system saying that this program was successful. Send something other than zero to indicate there was a problem.
You can now send and receive data to and from the console. In addition, this is the basic form which most of your c++ files will take.
Please check back for more advanced tutorials.