1
Posted September 28, 2009 by Spyros in C/C++ Programming
 
 

Write Your First C++ Program

The C++ Programming Language
The C++ Programming Language

If you are new to C++ Programming it would be worthwhile reading the introduction on c++ and object oriented programming. Now that you understand why C++ was really created and what it’s good for, it’s time you start some simple coding. We’ll be coding a very easy program that almost every new programmer has coded when they started to learn the language.

We’ll be creating a simple program that prints the line “Hello CoderCaste.com !” :) Ok, let’s just see the program source code as a whole first.

#include <iostream>

using namespace std;

int main()
{
cout << "Hello CoderCaste.com !" << endl;
return 0;
}

Well, if you compile and run this you’ll get the message we talked about. Let’s now examine what this code does. The first statement starts with the character #. Whenever you see such a character not inside quotes, there’s a call to the preprocessor. What this does, is it tells the compiler that it needs to include the file iostream.h, which is a standar C++ header file that we include in order to use cout. Cout as you see at the source code, is the command that it’s used in order to print something to the application window. This command is initialized inside iostream.h and in order to use it, we just need to include its code, which is inside that file.

Using namespace std is just a simple command to disable namespaces for this source code and it states that the standard namespace is used. Namespace std contains all the classes, objects and functions of the standard C++ library created by Bjarne Stroustrup himself along with other people. Namespaces is just a way to define a new level of scope but it’s certainly more advanced programming and is not to be discussed more here.

After the declaration of the namespace, the main function is clarified. We’ll be thoroughly discussing functions in a later article, but for now you need to know that a function returns a value (even if that value is nothing) and gets arguments to work with. Every C++ program has a standard function called main. Main is an overloaded function and this means that it can be used with variable arguments and return values. At this instance, main takes no arguments and returns an integer value right before exiting.

The first command that main executes is the command cout. This is the standard output command that c++ provides. Cout prepares to print something in the console. The symbol ‘<<‘ right after is the stream input operator. This is like telling :

Cout command will be responsible to handle the string “Hello CoderCaste.com !” and right after it the “endl” statement. The endl is just a way to say “change to a new line”, also seen as /n at c users. Cout now handles the string and endl in the way it knows and since this is a command that prints text, it does just that, it prints the text. The semicolon(;) in the end is used in the end of almost every c++ command so it’s better to get used to it.

After the print is made, the program is prepared to end and just returns a value stating that everything went well, that value most times being 0 but it doesn’t really matter.

I hope you understood some basic C++ things. At the tutorial to come, we’ll be discussing how to get input from the command line and how to edit and appear it on screen.


Spyros