Standard using namespace std of vc++6.0

Source: Internet
Author: User
Tags new set

I've been looking at the C + + language programming (third edition) written by Ms. Lv, published by the University of Electronics, and I didn't know there was a flaw in the program code, and I was immersed in his book and didn't go to see the other version, the original C + + It has been explicitly proposed not to promote the use of some of the provisions of the book is still in use, but also in my recent work into a certain dilemma, very depressed, and later in the standard exercises to understand some coding code writing specifications. Learn the standard code writing today.

Let's take a look at the older version of C + + programming style (which is the book I've been learning)

<font size= "4" ><span style= "color: #ff0000;" > #include <iostream.h>//legacy C + + header files contain commands </span>class MyClass//defined by a class {public://class of public member MyClass (int i)// The defined constructor with one parameter {value = i; cout << "Constructor called." << Endl;} In the function body of the constructor within the class, the call will be output once int Max (int x, int y) {return x>y? X:y;} The overloaded function defined is a member function of the class int Max (int x, int y, int z)//the overloaded function defined in the class {if (x > Y) return x>z? X:z;elsereturn y>z? y:z    ;} int GetValue () const {return value;} Defines the general member function ~myclass () {cout << "destructor called." << Endl;} A destructor defined within a class that is called once will output a private member in the private://class once, and only the member function can access int value;}; <span style= "" ><span style= "Background-color:rgb (255, 255, 255);" ><span style= "color: #ff0000;"    >void main ()//The return value type is NULL for the main function </span></span></span>{myclass obj (10);//A parameter-defined object that will be initialized with a constructor function cout<< "The value is" <<obj. GetValue () << endl;//refer to general member functions by object cout << "Max number is" << obj. Max (10,20) << Endl;//overloaded functions by object reference}</font> 

This kind of programming style is also I have been used, the original limitations, but also will be replaced, although in the vc++6.0 can still run, but in other programming software is no longer applicable, such as VS2010 and so on.

Let's take a look at the new C + + programming writing:

<span style= "color: #ff0000;" > #include <iostream>//the new C + + header file contains the command </span><span style= "color: #ff0000;" >using namespace Std;</span>class MyClass//define a class {public://class with a public member MyClass (int i)//The constructor with one parameter defined//= value = I ; cout << "Constructor called." << Endl; }//the function body of the constructor within the class, the call will be output once int Max (int x, int y) {return x>y? X:y;} The overloaded function defined is a member function of the class int Max (int x, int y, int z)//the overloaded function defined in the class {if (x > Y) return x>z? X:z;elsereturn y>z? y:z    ;} int GetValue () const {return value;} Defines the general member function ~myclass () {cout << "destructor called." << Endl;} A destructor defined within a class that is called once will output a private member in the private://class once, and only the member function can access int value;}; <span style= "color: #ff0000;" >int main ()//The return value type is an integral type of the main function </span>{myclass obj (10);//A parameter-defined object that will be initialized with a constructor cout<< "The value is" <<obj. GetValue () << endl;//refer to general member functions by object cout << "Max number is" << obj. Max (10,20) << endl;//<span style= "color: #ff0000 by object reference overload function." >return 0;</span>} 

This is the new C + + programming code of the standard writing, then to see the difference between the two:

1. header files contain different commands, older versions of # include <iostream.h> when using <iostream.h>, the equivalent of calling library functions in C, using the global namespace, which is the earlier C + + implementation. and the new C + + # include <iostream> when using < Iostream>, the header file does not have a global namespace defined and must use namespace STD in order to use cout correctly.

#include <iostream.h>
using namespace Std; The error
So it's either written
#include <iostream >
using namespace Std;
Either written
#include <iostream.h>
Of course, the best is the former.

2. The return value type of the main function is different, the old version is void Main (), and the new C + + is int main (), and strictly also add return 0 to the last line of the function body of the main function, otherwise there will be a warning error, although it does not affect the operation of the program, But in good programming style to add is the best.

The difference between the two is also:

1. The old C + + header file is officially objected to (that is, explicitly listed no longer supported), but the old C header file is not (to maintain compatibility with C). Old C + + header filenames such as <iostream.h> will continue to be supported, although they are not in the official standard. The contents of these header files are not in the namespace Std. The new C + + header file, such as <iostream> contains the same basic functionality as the corresponding old header file, but the contents of the header file are in the namespace Std. (in the process of normalization, the details of some parts of the library have been modified, so the old header files and the entities in the new header file do not necessarily correspond exactly.) )

2. The so-called namespace refers to the various visible ranges of identifiers. All identifiers in the C + + standard library are defined in a namespace called Std.

Due to the concept of namespace, there are three options for using any identifier of the C + + standard library:

(Oct octal Dec decimal hex hexadecimal)

(1), directly specify the identifier. For example Std::ostream rather than ostream. The complete statement is as follows:

Std::cout << Std::hex << 3.4 << Std::endl;

(2), use using keyword.

Using Std::cout;

Using Std::endl;

The above procedure can be written

cout << Std::hex << 3.4 << Endl;

(3), the most convenient is to use using namespace std;

For example:

#include <iostream>

using namespace Std;

All identifiers defined in the namespace Std are valid (exposed). As if they were declared as global variables. Then the above statement can be written as follows: cout << hex << 3.4 << Endl;

3.<iostream> and <iostream.h> is not the same, the former has no suffix, in fact, in your compiler include folder can be seen, the two are two files, open the file will find that the code is not the same.

The header file with the suffix. h C + + standard has been explicitly not supported, the earlier implementation of the standard library functions defined in the global space, declared in the header file with the. h suffix, C + + standard in order to distinguish with C, and in order to correctly use the namespace, the header file does not use the suffix. h.

Therefore, when using <iostream.h>, the equivalent of calling library functions in C, using the global namespace, which is the earlier C + + implementation, when using <iostream>, the header file does not define a global namespace. Namespace STD must be used so that cout can be used correctly.

4. Before standard C + +, it was written in #include<iostream.h>, because the header file name to include is iostream.h. Standard C + + introduced the concept of namespaces, and the iostream and other standards in the standard library in the Std namespace, and in order not to be confused with the original header file, the standard C + + to use a new set of header files, This header file filename does not add the. h extension, such as iostream and so on, and the original C standard library header file also renamed, as the original string.h changed to CString (that is, the. h is removed, preceded by the letter C), so the header file contains the wording of the # Include <iostream>.
It is not written that #include<iostream> must use the using namespace Std; The reason we usually write is to expose the Std namespace to the global domain all at once (as if it were directly containing the iostream.h header file without namespace), making the standard C + + library as convenient as the traditional iostream.h. If using namespace STD is not used, the standard library must always be accompanied by the full name of the namespace, such as Std::cout << "Hello" << Std::endl; for easy writing, a using Namespace Std. This will allow you to read the program code that you write better.



Standard using namespace std of vc++6.0

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.