This shows you the differences between two versions of the page.
cpp_lesson01 [2019/02/08 23:31] |
cpp_lesson01 [2017/11/11 22:47] (current) |
||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ===== Lesson 1: Hello World! ===== | ||
+ | |||
+ | ==== Reading ==== | ||
+ | |||
+ | Before you begin, please read this article: [[http://www.cplusplus.com/doc/tutorial/program_structure/|Structure of a Program]] | ||
+ | |||
+ | ==== Writing the Program ==== | ||
+ | |||
+ | In the editor of your choice, please write the following code. **Note:** The bluefish editor version 2.02 allows for an automatic structuring of a c++ program. | ||
+ | |||
+ | <code> | ||
+ | // My first c++ program | ||
+ | #include <iostream> | ||
+ | using namespace std; | ||
+ | int main () | ||
+ | { | ||
+ | cout << "Hello World!"; | ||
+ | return 0; | ||
+ | } | ||
+ | </code> | ||
+ | |||
+ | **Note:** It is important to either read character by caracter or set your punctuation level to all in your Orca preferences for your editor. | ||
+ | |||
+ | ==== Explanations ==== | ||
+ | |||
+ | - The first line of the program is a comment. The program will stop reading the line when it encounters "%%//%%" (two slashes). | ||
+ | - We include input/output preprocessing. | ||
+ | - We declare standard global variables. | ||
+ | - We leave a blank line. This is not manditory but it is a convention for readability. | ||
+ | - We define the main function. This is where the program begins. **Note:** The word main is followed by a pair of brackets "()". These are rounded brackets. Americans call these parenthesies. | ||
+ | - We write a left-brace %%({%%). All functions are enclosed by braces. | ||
+ | - We print our message to the screen. Notice the two left pointing arrows (less than characters <<). These tell the program to insert the words "Hello World" into the standard output stream. **Note:** The statement ends with a semicolon (;). All statements end with a semicolon. | ||
+ | - We set a return code of zero (0) and exit the main function. | ||
+ | - We end the main function with a right-brace (}). | ||
+ | |||
+ | ==== Compiling the Program ==== | ||
+ | |||
+ | Although not absolutely necessary, save your program with the "cpp" extension. We assume, for this exercise, you have named your program | ||
+ | |||
+ | <code> | ||
+ | hello1.cpp | ||
+ | </code> | ||
+ | |||
+ | and that it is stored in your home folder. | ||
+ | |||
+ | To compile "hello1.cpp", at a terminal or console prompt, type: | ||
+ | |||
+ | <code> | ||
+ | c++ hello1.cpp | ||
+ | </code> | ||
+ | |||
+ | If your prompt returns with no messages, your program has compiled successfully. Otherwise, you will need to correct your code and compile the program again. | ||
+ | |||
+ | ==== Run Your Program ==== | ||
+ | |||
+ | Your compiled program is in a file called | ||
+ | |||
+ | <code> | ||
+ | a.out | ||
+ | </code> | ||
+ | |||
+ | To run your program type: | ||
+ | |||
+ | <code> | ||
+ | ./a.out | ||
+ | </code> | ||
+ | |||
+ | You may, of course, rename your program to something more meaningful than "a.out". It will also save your compiled program if you rename it; otherwise, the next compile you do will overwrite it. | ||
+ | |||
+ | **Note:** The "./" tells the operating system you understand you are executing a program. | ||
+ | |||
+ | ==== Formatting Your Output ==== | ||
+ | |||
+ | If we want to write more information to the screen, we may also want to format it. We can edit the line which begins "cout <<" to read as follows: | ||
+ | |||
+ | <code> | ||
+ | cout << "Hello World" << endl; | ||
+ | </code> | ||
+ | |||
+ | This will put an end-of-line character in our output stream. Now our prompt will appear on a separate line from our message to the world. | ||
+ | |||
+ | ==== Assignment ==== | ||
+ | |||
+ | - Write a new program which will display the following message on a line of its own:<code> | ||
+ | I am a c++ program. | ||
+ | </code> | ||
+ | - Read the article [[http://www.cplusplus.com/doc/tutorial/variables/|Variables, Data Types]] |