Professional C__ - Marc Gregoire [4]
#include "namespaces.h"
using namespace mycode;
int main()
{
foo(); // Implies mycode::foo();
return 0;
}
Code snippet from namespaces\usingnamespaces.cpp
A single source file can contain multiple using directives, but beware of overusing this shortcut. In the extreme case, if you declare that you’re using every namespace known to humanity, you’re effectively eliminating namespaces entirely! Name conflicts will again result if you are using two namespaces that contain the same names. It is also important to know in which namespace your code is operating so that you don’t end up accidentally calling the wrong version of a function.
You’ve seen the namespace syntax before — you used it in the Hello, World program, where cout and endl are actually names defined in the std namespace. You could have written Hello, World with the using directive as shown here:
#include using namespace std; int main() { 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 current block below the line where they are declared. Variables can be declared without being given a value. These uninitialized variables generally end up with a semi random 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. int uninitializedInt; int initializedInt = 7; cout << uninitializedInt << " is a random value" << endl; cout << initializedInt << " was assigned an initial value" << endl; Code snippet from hellovariables\hellovariables.cpp When run, this code will output a random value from memory for the first line and the number 7 for the second. This code also shows how variables can be used with output streams. Most compilers will issue a warning when code is using uninitialized variables, and some C++ environments may report a run time error when uninitialized variables are being accessed. The table that follows shows the most common variable types used in C++. TYPE DESCRIPTION USAG int Positive and negative integers, range depends on compiler int i = 7; short (int) Short integer (usually 2 bytes) short s = 13; long (int) Long integer (usually 4 bytes) long l = -7; long long (int) Long long integer, range depends on compiler, but at least the same as long (usually 8 bytes) long long ll = 14; unsigned int unsigned short (int) unsigned long (int) unsigned long long (int) Limits the preceding types to values >= 0 unsigned int i = 2; unsigned short s = 23; unsigned long l = 5400; unsigned long long ll = 140; float Floating-point numbers float f = 7.2f; double Double precision numbers, precision is at least the same as for float double d = 7.2; long double Long double precision numbers, precision is at least the same as for double long double d = 16.98L; char A single character char ch = 'm'; char16_t A single 16-bit character char16_t c16 = u'm'; char32_t A single 32-bit character char32_t c32 = U'm'; wchar_t A single wide-character size depends on compiler wchar_t