Home

Built-In Namespaces

 

The System Namespace

The Microsoft .NET Framework contains a namespace named System. This is the primary namespace of the classes used to create applications. This namespace contains a class named Console. This class represents the DOS window that is used to display the results of a console application:

DOS Window

The Console class contains methods used to display information on the screen or to retrieve information from the user who types it in the DOS window. The method that is used to display text on the screen is called Write. To use the Write() method, inside of the parentheses, type the sentence between double-quotes. Here is an example:

int main()
{
	System::Console::Write("Welcome to C++/CLI Programming");

	return 0;
}

This would produce:

DOS Window

Besides the Write() method, the Console class also provides a method called WriteLine(). The difference is that, after displaying something on the screen, the Write() method keeps the caret on the same line but WriteLine() transfers the caret the next line.

The std Namespace

The C++ Standard provides a namespace called std. The std namespace includes a series of libraries that you will routinely and regularly use in your programs.

The following libraries are part of the std namespace:

algorithm iomanip list ostream streambuf
bitset ios locale queue string
complex iosfwd map set typeinfo
deque iostream memory sstream utility
exception istream new stack valarray
fstream iterator numeric stdexcept vector
functional limits      

Therefore, whenever you need to use a library that is part of the std namespace, instead of typing a library with its file extension, as in iostream.h, you can just type the name of the library as in iostream. Then, on the second line, type using namespace std;. As an example, instead of typing

#include <iostream.h>

You can type:

#include <iostream>
using namespace std;

Because this second technique is conform with the C++ Standard, we will use it whenever we need one of its libraries.

Like the System namespace, the std namespace provides its own means of displaying a value to the screen. In the std namespace, the class that takes care of this is called cout. To use it, type cout, followed by <<, followed by the value, and end it with a semi-colon. Here is an example:

#include <iostream>
using namespace std;

int main()
{
	cout << "Welcome to C++/CLI Programming";

	return 0;
}

As mentioned previously, you can use different namespaces in the same program. Based on this, you can use both the std and the System namespaces in your program. The compiler would know exactly where each is located. To access a class that is part of a certain namespace, you can call the namespace followed by the :: operator and access the class. Here are examples:

#include <iostream>

int main()
{
	std::cout << "Welcome to C++/CLI Programming. ";
	System::Console::WriteLine("C++/CLI is the new full-featured C++");

	return 0;
}

You can also type using namespace for each namespace that you will need in your program. The above program could be written as:

#include <iostream>
using namespace std;
using namespace System;

int main()
{
	std::cout << "Welcome to C++/CLI Programming. ";
	System::Console::WriteLine("C++/CLI is the new full-featured C++");

	return 0;
}

Remember that, when calling using namespace, you can omit qualifying a class by preceding it with the namespace:

#include <iostream>
using namespace std;
using namespace System;

int main()
{
	cout << "Welcome to C++/CLI Programming. ";
	Console::WriteLine("C++/CLI is the new full-featured C++");

	return 0;
}

C Routines

The C computer language is the parent of C++ and C++ is the parent of the C++/CLI language. To maintain backward compatibility, the operations performed in C were made available to C++ and most standards of C++ are available in C++/CLI. Based on this, you can transparently include C routines in your C++/CLI program.

The libraries of the C language available in C++ are:

cassert cios646 csetjmp cstdio ctime cctype
climits csignal cstdlib cwchar cerrno clocale
cstdarg cstring cwctype cfloat cmath cstddef

Like C++/CLI and C++, the C computer language has its own mechanisms to display a value on the screen. One of the functions used to do this is printf_s. To use it, add an opening and a closing parentheses to it. Inside of the parentheses, enter the value you want to display. Here is an example:

#include <iostream>
using namespace std;
using namespace System;

int main()
{
	cout << "Welcome to C++/CLI Programming. ";
	Console::WriteLine("C++/CLI is the new full-featured C++");
	printf_s("C++/CLI highlights the best of C++ and significantly modernizes it");

	return 0;
}

Of course, there are many other details and aspects concerning these built-in namespaces, libraries, and operations. We will review as many as possible in future lessons.

 

Home Copyright © 2006-2010 FunctionX, Inc.