First set up an empty Win32 Console Application project and add a C++ file (main.cpp) like in lesson 2.
Write the following code and press F5 to run it:
#include <iostream>
using namespace std;
int main(){
cout << "Hello world!" << endl;
system("pause");
}
The first line includes the iostream library which provides the input and output functions. Without this function we can`t write out the result of the program. Notice that wen a library is included #include is used.using namespace std;
int main(){
cout << "Hello world!" << endl;
system("pause");
}
The second line tell the compiler to use the std namespace, namespace allows to create groups of functions or objects under a name. Don`t worry, I will write more about namespace later. For now it`s good to know that if you leave this line from the code you will need to include std:: before each cout or cin from your code. Notice that the line is ended by a semicolon (";");
The third line starts a function called main, which will return and int (integer). The main function starts first in any program. The curly braces { and } shows the beginning and end of the function.
The next line writes out the "Hello world!" text. Notice that the line is ended by a semicolon (";").
The next line is telling the compiler to pause the program and wait for the user to press Enter. Without system("pause") the program will run so fast that you will not see the result.
Let`s see the program in action:
Note: Don`t worry if you haven`t understand everything. This lesson is written to have an idea about how a program looks and to see the basic syntax of C++. Another useful and simple thing to learn are comments: In C++ you can add comments (notes) after the code lines. The comments are skipped by the compiler and they don`t affect the program. Comment are helpful to note or explain things. The comment syntax is:
//Comment goes here
or/*Comment line 1
Comment line 2 */
Comment line 2 */
Next lesson: C++ Lesson 4 - Variables
Previous lesson: C++ Lesson 2 - Set up Visual C++
HTML Code
Direct Link
1 comments:
Many thanks :D
Post a Comment