3
Posted November 20, 2009 by Spyros in C/C++ Programming
 
 

1. Your First C++ Program

C++
C++

I’ve decided to create a section in this blog in order to write some tutorials about one of my favorite programming languages, C++. In this series, i will be starting to talk about C++ basics and then go into more advanced stuff like function overloading, templates, classes inheritance and more. My hope is that in the end you will be able to find a whole source of interesting C++ tutorials that will help you learn the language and program effectively.

As you notice, there will be a number indicating the tutorial number. This does not necessarily mean that you have to read part 2 in order to read part 4, but it would be a good idea to know the things in the previous posts in order to read  and understand the ones discussed next. In this part i will discuss the very basics and guide you through creating your first “Hello World” C++ program, introducing you to the main language concepts.

Let’s first see the actual program code and then analyze it :


#include <iostream.h>

int main()
{

cout <<”Hello World!!!”;

return 0;

}

The above program just prints the message “Hello World!!!”. The character # in the beginning of our program instructs the preprocessor of our compiler that at this place there is a command waiting to be executed by it. This command is actually the “include <iostream.h>” which informs that our program needs to include the standard file “iostream.h”. This header file is used as a way to execute the command cout, used for printing characters to the standard output. Including this file means that we can now execute the command specified in there.

The special function int main() is the actual beginning of our code. At the next line, the character “{” specifies the start of this function. Right after that we execute “cout”, a command that as i mentioned is used to print characters or strings to the standard output. Now, take a look at the stream redirection operands (<<). You will recognize this if you are Unix knowledgeable. It is like the append redirector. What this does is redirect the right part of the expression to the left part of it. In our case, the string “Hello World!!!” gets redirected to the command “cout” to be printed to screen. Then, follows the greek question mark character (;) that specifies the end of the command. Almost every c++ command ends with this character, you will be seeing that a lot. In the next line, we see “return 0”, a command that instructs our program to return the value 0 if the execution of the program reaches that point. Our function just has to return a value because it was created to return an integer value (more on functions in a later post). In the end, you can see the “}” character that closes the function and the actual program execution.

In General :

After almost every C++ command, use the greek question mark character(;).

#Include <iostream.h> is essential if we want to use cout.

The cout command is used to print characters to the screen or other output device.

The special function int main() is always the beginning of the program. Starts with “{” and ends with “}” and the actual code of the program is in the middle.

A Simple Exercise

1. Create a program that prints your name.


Spyros