Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 [C++] Picking apart "Hello, world!"
Index -> Programming, C++ -> C++ Tutorials
View previous topic Printable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
wtd




PostPosted: Fri Nov 03, 2006 2:54 pm   Post subject: [C++] Picking apart "Hello, world!"

Or, why there's no C++ whirlwind... yet

Let's start with the classic "Hello, world!" program.

code:
#include <iostream>

int main()
{
   std::cout << "Hello, world!" << std::endl;

   return 0;
}


Includes

Where to begin? Well, let's look at the first line.

code:
#include <iostream>


This is a preprocessor command. Note the "pre" in preprocessor. This runs before the compiler even gets hold of the source code. This particular line includes the iostream header, which contains code relevant to input and output streams.

The < and > around the name are significant. They indicate that the preprocessor will look in the include path to find this header. The include path can be modified when we compile an app, but by default it contains the directories necessary to use the standard library.

Using double quotes instead of < and > would indicate that the preprocessor should look for the header in a directory relative to where the source file is located.

Functions

The next thing up is the main function.

code:
int main()
{

}


We'll look at the stuff inside it later.

This basic fucntion definition defines a function called "main" which returns the type "int". It takes no arguments.

The main function in C++ programs acts as the entry point. Though there may be many functions in a program, when the program is run, the code in main is executed.

Return values

code:
int main()
{


   return 0;
}


The main function returns an integer to indicate the success or failure of the program to the operating system. In most operating systems, returning a zero indicates success. Anything else indicates failure. Different values can return different sorts of failure.

As it happens, in standard C++, a main function which reaches the end of its execution without returning a value, is assumed to return zero. We may thus simplify our code.

code:
#include <iostream>

int main()
{
   std::cout << "Hello, world!" << std::endl;
}


And the rest...

code:
std::cout << "Hello, world!" << std::endl;


Namespaces

What's with these :: things?

Well, they indicate namespace resolution. Namespaces are used to avoid naming collisions. Say, for instance, you wish to use "cout" as the name for a variable in your program. You can refer to std::cout in that case to distinguish the two.

Of course, there are shortcuts. We can bring an entire namespace into our program.

code:
#include <iostream>

using namespace std;

int main()
{
   cout << "Hello, world!" << endl;
}


Or we can bring just certain names from the namespace in.

code:
#include <iostream>

using std::cout;
using std::endl;

int main()
{
   cout << "Hello, world!" << endl;
}


Objects

code:
std::cout


This variable is an object. It is an instance of the std::ostream class. There's a whole whirlwind on that subject.

Operator overloading

The << operator is commonly used to represent a bit-shift. This operation acts on two integers, and is not explained here.

Since C++ allows operator overloading, though, we can use it to insert values into an output stream. This is a very common idiom in C++ written with respect for the standard library.

To sum up...

You should now have an idea of what concepts one must understand in order to come to a decent understanding of the "Hello, world!" program written in C++.
Sponsor
Sponsor
Sponsor
sponsor
jack




PostPosted: Sat Nov 04, 2006 4:06 pm   Post subject: (No subject)

Whats the difference between using these two lines:

using std::cout;

or....

using namespace std;
wtd




PostPosted: Sat Nov 04, 2006 4:33 pm   Post subject: (No subject)

The former brings just "cout" from the "std" namespace into the namespace of our program.

The latter brings everything in the std namespace into the namespace of our program.
Sniper4Life




PostPosted: Mon Apr 13, 2009 4:40 pm   Post subject: RE:[C++] Picking apart "Hello, world!"

namespace std; lets you be lazy and not write std::cout; every time you wanaa output a message Razz
Display posts from previous:   
   Index -> Programming, C++ -> C++ Tutorials
View previous topic Tell A FriendPrintable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 4 Posts ]
Jump to:   


Style:  
Search: