End Chapter)

Source: Internet
Author: User

In this section, note the naming rules for namespaces and header files in C ++. It is also the last section of the C ++ basic series. First of all, I would like to thank all the bloggers who have followed this series for their support and help along the way to C ++ learning. With your suggestions and guidance, I am more interested in C ++. After this series ends, I will continue to learn and pay attention to some practical applications of C ++.

We all know that a large software is usually composed of multiple modules. These modules are often completed by different people, and finally constitute a complete program. If different people define functions and classes respectively and put them in different header files, use the # include command to include these functions and classes in the main file. However, because different header files are designed by different people, different header files may have the same names to define functions or classes. In this way, naming conflicts may occur. At the same time, if a third-party class library is used in the program, the same problem may occur. To solve this problem, ANSI/iso c ++ introduces a namespace, that is, the memory area named by a program designer. The program designer specifies the namespace as needed and associates the identifiers declared in the namespace with the namespace. This ensures that the identifiers of the same name in different namespaces do not conflict with each other. Its general format:

Namespace name

{

Identifier 1;

Identifier 2;

...

}

The curly braces are the namespace scopes. In fact, a standard namespace STD specified by C ++ has been used in all previous chapters. Do you still remember that the using namespace STD statement was used in the example of C ++'s development, characteristics, and source program composition? The meaning is to use the standard namespace STD. It is the abbreviation of the word standard. All the identifiers in the Standard C ++ library are in this namespace, for example, functions, classes, and objects in the common iostream header files are defined in the STD namespace. If you want to call functions, classes, and objects in a namespace, there are two methods: (1) use the "using namespace name" in the original file, and then directly call the identifier; (2) Add the namespace and scope operator ":" Before the identifier "::";

Custom header file University. h:

1 namespace Peking // declare the namespace Peking
2 {
3 int Rank = 47; // identifier
4}
5
6 namespace Tsinghua // declare the namespace Tsinghua
7 {
8 int Rank = 54; // an identifier with the same name as Peking
9}

Use the university. h file and namespace:

1 # include "stdafx. H"
2 # include <iostream>
3
4 # include "University. H" // load the header file University. h
5 usingnamespace Peking; // explicitly use the namespace Peking in the university. h header file
6
7 int main ()
8 {
9 STD: cout <"World University Ranking (2010)" <STD: Endl;
10
11 STD: cout <"Peking University:" <rank <STD: Endl; // rank is equivalent to Peking: Rank
12 STD: cout <"Tsinghua University:" <Tsinghua: rank <STD: Endl; // Add the namespace Tsinghua and scope operator before the identifier rank "::"
13
14 return0;
15}

Result:

Finally, the naming rules for header files in C ++ are described as C ++ evolved from the C language. To be compatible with C, C ++ retains some rules in C language, this includes usage. H is used as the suffix header file, such as stdio. h, math. H and string. h. However, we recommend that the header file of ANSI/iso c ++ not contain the suffix ". H ". However, to enable the original C ++ program to run, the header file in the C ++ program can either use a header file without a suffix or use a header file with a Suffix in the C language. You can use either of these two types of header files in C ++, but there are a few notes to note: (1) if the C ++ program uses a suffix ". H "header file, you do not need to declare the namespace in the program, you only need to include the header file; (2) c ++ standard requires that the system provides the header file without a suffix ". H ", but in order to indicate the connection and difference between the C ++ and C header files, the header files in C ++ do not contain a suffix ". H ", but add the prefix C before the corresponding header file name in C language:

1 # include <cstdio> // equivalent to # include <stdio. h> in C
2 # include <cstring> // equivalent to # include <string. h> in C
3 # include <cmath> // equivalent to # include <math. h> in C

End

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.