C ++ later development of additional tools

In the late C ++ development, adds some features to use as a tool, there are templates (including function templates and class templates), exception handling, name space and runtime type identification. In 1997 ANSI C ++ committee to incorporate them into the ANSI C ++ standard.

1, exception handling

       Program, there are two common errors: syntax error and runtime error. C ++ exception handling approach taken is: if an exception occurs during the execution of a function, this function may not be processed immediately, but send a message passed to it on one (ie the calling function), its parent after processing to capture this information. If the function can not be a process which would then pass on a, by the primary treatment. So step by step on delivery, to the highest level if unable to deal with, and had abnormal termination of program execution. This is to do a good job at the bottom of the function dedicated to solving practical tasks, the move to the task exception handling to deal with a certain level in order to improve efficiency.

C ++ exception handling mechanism is composed of three parts, i.e., to check (the try), throw (the throw) and capture (catch). The statements to be checked (including functions called) on the try block, the throw is used to issue an abnormality information when the abnormality occurs, and is used to capture the catch abnormality information, abnormality if the captured information, the processing it.

throw statement is generally throw operator and a data composition, in the form of

throw expression;

Structure of try-catch

try

      {Statement} be checked 

catch (Exception Information Type [Variable Name])

      {Statement} exception handling

After the program starts running, the normal execution of the order to try block starts executing statements inside the curly brackets try block. If the statement is not an exception occurs during the execution of the try block, the catch clause does not work, the flow proceeds to the statement following the catch clause continues.

If the statement occurs within the try block (including functions called) during abnormal, the operator throws an exception throw information. Throw after throw an exception information, processes immediately leave this function, go to its previous level of function.

After exception handling is performed, the program does not automatically terminate, continue to catch clauses following statements.

Since the catch clause is to handle exception information, often referred to catch exception handling block or catch the exception handler.

note:

(1) function is detected to be in a try block, otherwise no effect.

(2) try block and a catch block appears as a whole, is a part of the catch block try-catch structure, must be followed by a try block, can not be used alone, can not be inserted between two other statements. However, in a try-catch configuration may only try block without catch block. I.e. not only the check function in the present process, the processing block on the catch other functions.

(3) try and catch blocks must be useful compound statements enclosed in braces, even flowers, only one statement in parentheses, braces can not be omitted.

(4) a try-catch structure can have a try block, but it can have a plurality of catch block, to match the different exception information.

    (5) catch the use of the following two formats:

    catch(double)

    catch only type of exception checking the captured information, without checking their values. Therefore, if necessary to detect abnormalities of a plurality of different information, different types of exception should be thrown out by the information throw.

    C ++ exception information may be predefined by the system standard type, and may be the type (e.g., structure or the like) user-defined. If the information is thrown by the throw of that type (or subtype), with the matching catch both throw, catch catch the exception information.

    There may also be another kind of writing, i.e., specify the type names and variable names, such as

    catch (double d) // such that a value of d is obtained.

(6) If the type of abnormality information is not specified in the catch clause, the use of ellipses "...", it indicates that it can capture any kind of abnormality information, such as

catch(…) {cout<<″OK″<<endl;}

It can capture all types of exceptions information, and outputs "OK".

This catch clause should be placed on the last try catch structure, the equivalent of the "other." If it as a first catch clause, the rear catch clauses are ineffective.

(7) try catch and throw structure may occur in the same function, the same function may be not. When an exception is thrown throw information, first look for matching catch in this function, if no trycatch structure in this function or can not find the matching catch, just go to the nearest try leave appear abnormal catch structure to deal with.

    (8) In some cases, the throw statement may not include expressions such as

     throw;

He said, "I do not handle this exception, by a higher processing."

(9) If you throw thrown exception information can not find a matching catch block, NA me the system will call a system function terminate, the program terminates.

#include <iostream>

#include <cmath>

using namespace std;

int main ()

{

double triangle(double,double,double);

double a,b,c;

cin>>a>>b>>c;

try

{

while(a>0&&b>0&&c>0)

{

cout<<triangle(a,b,c)<<endl;

cin>>a>>b>>c;

}//while

}//try

catch(double)

{

 {cout<<"a="<<a<<",b="<<b<<",c="<<c<<",that is not a triangle!"<<endl;}

 cout<<"end"<<endl;

}//catch

return 1;

}

double triangle(double a,double b,double c)

{

double s=(a+b+c)/2;

if((a+b)<=c||(a+c)<=b||(c+b)<=a) throw a;

return sqrt(s*(s-a)*(s-b)*(s-c));

}

C ++ allows the listed types of exceptions that might be thrown when you declare a function, such as

double triangle(double,double,double) throw(double);

It represents a double triangle function can only throw exception information. If written

double triangle(double,double,double) throw(int,double,float,char);

It said triangle function can throw int, double, float, or char exception information. Abnormal specify is part of the function declaration, it must appear on both the first line of the function declaration and function definition. If the types of exceptions that might be thrown is not listed in the declaration of a function, the function can throw any type of exception information. If you want to declare a function that can not throw an exception, can be written in the following form:

double triangle(double,double,double) throw();//throw无参数

Then even if there is a throw statement in the function is being executed, it does not actually execute the throw statement does not throw any exception information, the program will terminate abnormally.

       Exception handling mechanism of C ++ exception is thrown information is captured catch in the throw, partial objects associated performed destructor (destructor calls class object), the opposite order as the configuration of the destructor object and then executes abnormality catch block matching information in the statement.

2, namespaces

ANSI C ++ namespace is introduced by a user named scope, procedures for handling conflicts of the same name in common. It defines three levels of scopes in the C language, document (compilation unit), compound statements, and functions. C ++ and the introduction of class scope, class is present within the file. Variables can be defined with the same name in different scopes, without disturbing each other. If you define two classes in the file, in both classes can have a function with the same name. When referring to, in order to distinguish, it should be added to the class name as defined as

void A :: fun1 () // definition of class A function fun1

void B :: fun1 () // definition of class B in the function fun1

Name conflicts, namely in the same scope with two or more entities of the same name. As two header files contained in the file have fun this function, there will be conflict.

People want to ANSI C ++ standard provides a mechanism, a tool that allows library designers named by the global identifier global entity capable of and the name of the program libraries and other global identifiers distinguish.

       ANSI C ++ adds namespace (namespace). The so-called namespace, in fact, a programmer named by the memory area. Programmer can specify what you want some domain name space, the number of global entities were placed in each namespace, which is separated from other global entities apart. Such as

namespace ns1 // Specify the namespace ns1

{int a;

double b;

}

Namespace members include variables a and b, a and b are still global variables, only hid them in the specified namespace only. To use the variables a and b in the program shall be added to the namespace name and the scope resolution symbol "::", as ns1 :: a, ns1 :: b. This usage is called a namespace defined (qualified), names (e.g., ns1 :: a) referred to as being qualified name (qualified name). The relationship between directories and files of the operating system in C ++ namespace role is similar. The role of the namespace is separated from each other to establish some scope, some separated by a global entity, in order to avoid name conflicts. You can set multiple namespaces, each namespace name represents a different domain name space, a different namespace can not be the same name.

When you declare a namespace, braces may include not only variable, but may include the following types: 

Variables (which may have initialization);

constant;

Function (which may be defined or declared);

Structure;

class;

template;

Namespace (in one another to define a namespace namespace, i.e. nested namespaces).

ns1 :: Student stud1 (101, "Wang", 18); Student stud1 // class defines a namespace declared ns1

stud1.get_data (); // Do not write ns1 :: stud1.get_data ();

cout << ns1 :: fun (5,3) << endl; // call the namespace ns1 of fun function

:: namespace name namespace Member Name

C ++ provides a number of mechanisms to simplify the procedures for using the namespace members.

(1) using the namespace alias

It may be an alias (namespace alias) for the namespace namespace used instead of the longer names. Such as

namespace Television // declare a namespace called Television

 {…}

It can be easily understood by a short note alias instead. Such as

namespace TV = Television; // Alias ​​formerly Television and TV equivalent

(2) use a member name using the namespace

Using namespace member names must be back by the namespace qualified name. E.g

using ns1::Student;

Effective range using statements from using statements to the beginning where the scopes using end. If the following statement after using the above statement:  

Student stud1 (101, "Wang", 18); // corresponds to ns1 :: Student

The above statement is equivalent to

ns1::Student stud1(101,″Wang″,18);

using ns1 :: fun; fun // followed by the statement that appears belongs to the namespace ns1 of fun

cout << fun (5,3) << endl; // fun function here corresponds to ns1 :: fun (5,3)

(3) using namespace using the namespace name

The general format using namespace statement is

using namespace namespace name;

E.g

using namespace ns1;

Declared in this scope to use in the members namespace ns1, you do not have qualified with a namespace in the use of any member of the namespace.

Of course, C ++ also allows the use of no-name namespace, such as

namespace // namespace without a name

{Void fun () // define namespace member

    {cout<<″OK.″<<endl;}

}

Standard headers (e.g. the iostream) a function, class, object class, and a template is defined in the namespace std.

Thus, when used in the C ++ standard library procedures, as required std defined. Such as

std :: cout << "OK." << endl; // statement cout stream object is defined in the namespace std.

Thus, all identifiers in defined and declared std can be used in this document as a global amount. It should be an absolute guarantee identifier with namespace std members of the same name does not appear in the program. Also can be used directly using namespace namespace name; statement to handle.

3, early library

    You can use the C language library in C ++ programs.

Such as

Conventional method 1) #include <math.h> // C of

2) A new method of using C ++. The system for providing standard C ++ header file does not include the suffix .h, such as iostream, string. And C different, C ++ header file name is used before the addition of the letter c in a corresponding C language header file name (excluding extension .h). In addition, because these functions are declared in namespace std, so in the program to make a statement to the namespace std. Such as

#include <cstdio>

#include <cmath>

using namespace std;

 

Published 208 original articles · won praise 30 · views 10000 +

Guess you like

Origin blog.csdn.net/hopegrace/article/details/104168394