Home | C++ Tutorial | 3. First Program |     Share This Page
The skill that makes your computer think

A simple C++ program

Let's use the code below to find what makes up a very simple C++ program - one that simply prints "Hello World!" and stops. Adjust your browser so you can see the text window below the code. Then point your mouse at different statements in the program.

#include <iostream>

using namespace std;

int main()
{
	cout << "Hello World!" << endl;
	return 0;
}
      

You may wish to copy this program into your C++ programming editor and compile it, just for practice. Because this is a special display program, it is somewhat more tricky to copy than others in this tutorial. And if you are using Arachnophilia, be sure to use the new edit menu function "Paste Text." Note: You may notice I use the following line in most programs in this tutorial:
using namespace std;
    
This line disables the C++ namespace feature. Namespaces are an important feature of C++, but for tutorial purposes I have decided the clarity of the program listings is more important. Later on, the topic of namespaces will be discussed.

Discussion

Each C++ program consists of one or more functions. The function above, main(), is the only function that is required to be present in your program. Functions may or may not return a value. Although you may write a function to return anything or nothing --
string answerQuestion(string q)
{
	string a;
	if(q == "Please?") {
		a = "Okay!";
	}
	else {
		a = "You forgot the magic word!";
	}
	return a;
}
    
— the special function main() must return an integer.

Within functions, there are program statements. Here is a statement:

	a = "You forgot the magic word!";
    
Statements may be grouped together with braces ("{","}"). The braces identify groups of statements that are meant to be executed as a unit.

These pages are Copyright © 2000, P. Lutus. All rights reserved.

www.arachnoid.com Main Page
Home | C++ Tutorial | 3. First Program |     Share This Page