2011年12月22日 星期四

Getting Started (II)


Namespace std and the new Header filenames

The namespace keyword of C++ is used to group related data and functions. For example, if two sources provide a function called strcpy(). You may distinguish between them by prefacing strcpy() with the namespace, like VendorA::strcpy() or std::strcpy(). The namespace std is used to identify the standard ANSI/ISO symbols (functions, classes, and variables). The ANSI/ISO standards committee stipulated that standard header files would not have a filename extension. So, the header file, iostream.h will be identified as just iostream. The standard C header filenames, such as math.h, string.h, etc. will be prefaced with a c and the extension is dropped. Hence, math.h and string.h become cmath and cstring.

Example 1-2 namespace std and ANSI/ISO standard header files

1      // File: ex1-2.cpp - namespace std and the new header filenames
2     
3      #include <iostream>
4      #include <cmath>
5      #include <cstring>
6      #include <cstdlib>
7      #include <cctype>
8      using namespace std;
9     
10      // Create a namespace
11      namespace mystuff
12      {
13          int cout = 5;
14          double sqrt(double x) 
15          {
16              return x / 2.0;
17          }
18      }
19     
20      int main(void)
21      {
22          char cout[32] = "This is a bad idea";
23          char temp[80];
24          std::cout << "hey\n";
25          std::cout << "the square root of 2 is " << sqrt(2.) << endl;
26          strcpy(temp,"hello");
27          strcat(temp," there");
28          std::cout << strlen(temp) << temp << endl;
29          std::cout << atoi("4") << endl;
30          std::cout << toupper('a') << endl;
31          std::cout << (char)toupper('a') << endl;
32         
33          std::cout << mystuff::cout << ' ' << cout << endl;
34         
35          std::cout << sqrt(5.75) << ' ' << mystuff::sqrt(5.75)  << endl;
36          return 0;
37      }


******  Program Output  ******

hey
the square root of 2 is 1.41421
11hello there
4
65
A
5 This is a bad idea
2.39792 2.875

Note that symbols default to their local definitions first, then to std definitions.

main() and the return type

The C++ standard specifies that main() must return an int.  That is, you must define main() like

int main()
{

}

or

int main (void)
{

}

or

int main(int argc, char* argv[])
{

}

You may not define main() as

void main()
{

}

or

main()
{

}

You do not, however, have to end main() with a return statement. If the end of main() is reached without a return statement, a return of 0 is assumed.

The using directive and declaration

The keyword using is used as both a compiler directive and as a declaration. Throughout this text, the "using namespace std" directive directs the compiler to make available all of the "std" names.  So, for example,

#include <iostream>
using namespace std;

tells the compiler to recognize the names: cin, cout, endl, and others in whatever scope these two lines appear. Without the "using namespace std;" directive, the user would still have to qualify the the cin, cout, and endl identifiers as std::cin, std::cout, and std::endl;

Another approach is the using declaration, like this:

#include <iostream>

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



cout << …

Now, the user can use the identifiers cout and endl without the std namespace, but only those std identifiers. The using declaration adds an identifier to the current scope.
  1. 在 C++ 下 namespace 用來 group 相關的 data 與 function. 假始有兩個 source 都有 strcpy() 這個 function, 就可以用不同的 namespace 來區分.
  2. 在 C++ 標準中, main() 必須要 return int.
  3. 在 C++ 下 using 用來做為 compiler 指令或是宣告 .

沒有留言:

張貼留言