Datasheet
// helloworld.cpp
#include <iostream>
using namespace std;
int main(int argc, char** argv)
{
cout << “Hello, World!” << endl;
return 0;
}
The using directive can also be used to refer to a particular item within a namespace. For example, if the
only part of the
std namespace that you intend to use is cout, you can refer to it as follows:
using std::cout;
Subsequent code can refer to cout without prepending the namespace, but other items in the std
namespace will still need to be explicit:
using std::cout;
cout << “Hello, World!” << std::endl;
Variables
In C++, variables can be declared just about anywhere in your code and can be used anywhere in the cur-
rent block below the line where they are declared. In practice, your engineering group should decide
whether variables will be declared at the start of each function or on an as-needed basis. Variables can be
declared without being given a value. These undeclared variables generally end up with a semirandom
value based on whatever is in memory at the time and are the source of countless bugs. Variables in C++
can alternatively be assigned an initial value when they are declared. The code that follows shows both
flavors of variable declaration, both using
ints, which represent integer values.
// hellovariables.cpp
#include <iostream>
using namespace std;
int main(int argc, char** argv)
{
int uninitializedInt;
int initializedInt = 7;
cout << uninitializedInt << “ is a random value” << endl;
cout << initializedInt << “ was assigned an initial value” << endl;
return (0);
}
6
Chapter 1
04_574841 ch01.qxd 12/15/04 3:39 PM Page 6










