Your First C++ Program
Programming
|
DoxCoding.com
Ok the best way to start is to tell you what we will do ;).
Ok. First we will be using:
-------------------------------------
Dev-CPP. (Type this in google to download it).
A Brain. (Vital)
-------------------------------------
Ok so i think we should really get started.
So lets start with a simple hello world application.
Ok to start this we can simply open Dev-CPP, File > New > Source Code. We can type all our code in there. Projects will come later if i do something thats neeeded. By all means if you want to use a new project you may.
So. the hello world.cpp
#include <iostream>
using namespace std;
int main()
{
cout << "Hello, World! \n";
system("PAUSE");
return 0;
}
Ok, so i guess you havent a clue on what any of this means do you?
This is what i am here for. To explain your first C++ program.
Lets start off with the first line of code.
#include <iostream> .
Lines beginning with a sign (#) are directives for the preprocessor. They are not used within the main code but are only used to firect to the processor. In this case the directive #include <iostream> tells the preprocessor to include the iostream standard file. This specific file (iostream) includes the declarations of the basic standard input-output library in C++, and it is included because its functionality is going to be used later in the program.
using namespace std;
This is used to declare all the standard Output and Input such as 'cout' and 'cin'. without this we would have to keep typing in std:: or something like that. I am not too sure as i never used it. I always declared them at the start if i knew i was going to use them.
This line of code is always needed if you are going to use the standard library 'cout' , 'cin'.
int main()
This declares what part this is. It does not matter whether there are other functions with other names defined before or after it - the instructions contained within this function's definition will always be the first ones to be executed in any C++ program. As it seems and looks. 'main', the main part of the program goes inside here.
The two { and }.
These go after a function to kind of group the code underneath it together.
cout << "Hello World";
This line is a C++ statement. A statement is a simple or compound expression that can actually produce some effect. In fact, this statement performs the only action that generates a visible effect in our first program.
'cout' is just basicly what you type to show some kind of writing on your command / application. As in this example [i]cout << "Hello, World";[i]
prints out Hello, World.
system("PAUSE"); and return 0;
This waits for another user input to show that the user is ready to close the application. It prints [i]Press any key to continue[i] the application will then close otherwise.
I hope this has helped some of you newbs out there.
Next time i have some spare time i will write a small tutorial on how to add in a variable and user input, then print out that user input.
Stay TUNED!
-[O]
Please login to rate coding articles.
Click here to register a free account with us.
|
|
Comments
|
| Please login to post comments. |
|
|
 |
Categories |
 |
|
|