c双语翻译文献_第1页
c双语翻译文献_第2页
c双语翻译文献_第3页
c双语翻译文献_第4页
c双语翻译文献_第5页
已阅读5页,还剩4页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、c+双语翻译文献    一、 C+语言的起源从分类上讲,C+程序设计语言可以被看作是更具现代特色C语言。C语言是由AT&贝尔实验室的Dennis Ritchie在20世纪70年代创建的。它首先用于编写及维护UNIX操作系统,在此之前UNIX系统编程用的是汇编语言或是B语言,(Unix的创始人Kerg Thompson所发明的一种语言)。C语言用途非常广泛,可用于任何程序的编写,但它的成功及广受欢迎是与UINX操作系统密切相关的。如果要维护UNIX系统,就需要用C语言。C语言与UNIX配合得如此之好,以至于在短时期内,不只是系统程序,几乎所有在UN

2、IX环境下运行的商务程序都采用C语言进行编写。C语言如此流行,一时间产生了许多可运用于其他常见操作系统的不同版本,C语言的运用也不再局限于使用UNIX的计算机了,但是,抛C诱言的户受欢迎不谈,C语言并不是没有缺陷的。    C语言的独特之处在于它是一种拥有众多低级语言特性的高级语言。某种程度上来说,C语言介于严格的高级语言与低级语言之问,兼具了两者的优缺点,如同(低级的)汇编语言,C语言程序可以直接操作计算机的内存。另一方面,C语言又像其他高级语言一样比汇编语言易于读写。这使C语言成为编写系统程序的绝佳选择,然而,对其他程序而言(某种程度上甚至对系统程序

3、也如此),C语言不如其他语言易懂,也不像其他高级语言那样拥有许多自动检测。    为了克服上述及其他一些缺陷,20世纪80年代早期,同样是AT&贝尔实脸室的Bjarne Stroustrup创建了C+语言。Stroustrup意图使C+成为更好的C语言,因此C语言的大部分内容都包含在C+中,所以绝大多数C语言程序也是C+程序(反之并不成立,许多C+程序并不是C程序。与C不同的是C+有许多创建类的工具,因此它可被用作面向对象的程序设计。二、C+与面向对象的程序设计 面向对象的程序设计(object-oriented programing ,OOP)

4、是当今流行的一种功能强大的编程技术。OOP最主要的特点是:封装、继承与多态。封装是一种信息隐藏或抽象的方式,继承可用于编写可重复使用的代码,多态指的是在继承的内容中可以让一个名字有多个意义的方法。话虽如此。我们必须承认这些对于从未听说过OOP的读者而言意义不大。我们会在以后的章节中对上述问题进行详尽的介绍。C+通过提供类的方式来支持OOP,类是一种结合了数据及算法的数据类型。但是C+并不像某些作者所说的那样是"纯OOP语言", C+的OOP特性中融人了对有效性及实用性的考虑。这种结合使C+成为当前应用最为广泛的OOP语言,虽然在实际应用中并非所有的操作都严守OOP的准则。三

5、、C+特点     C+有类的概念,因此它是面向对象的程序设计语言。它允许函数及操作符的重载。C+与C语言的联系使其与那些新的面向对象的语言相比而言较为传统,但是它比现在流行的其他语言有更有力的抽象手法。C+具有的模板工具使完全而直接的算法抽象实现成为可能。C +模板允许使用类型参数进行编码,最新的C +标准及大多数C+编译器允许多重名字空间的存在,以便提供更多的类名和函数名的重复使用。C+的异常处理工具与其他程序语言相似。而C+的内存管理则与C语言相似。程序员必须分配他们自己使用的内存,并做相应的回收工作。由于C语言是C +基本的子集,许多编译器允许你

6、在C+程序中使用C语言风格的内存管理。但C+也有其自身的C+风格的内存管理的语法。建议在C+中编码时采用C +风格的内存管理。四、C+术语     所有过程式的实体在C+中均被称作函数(function,在其他语言中称为procedure(过程)、method(方法)、function(函数)或是subprogram(子程序)的在C+中都叫做函数。下面我们将提及C +程序(program)。一个C +程序实际上就是一个名为"main"的函数。其他C+术语与大多数程序设计语言中的术语非常相似。五、C+程序的风格   

7、  C+的程序设计风格与其他语言十分相似,其目的都是为了使代码易于阅读及修改。选择合适的变量名和适当的缩进可减少添加大量注解的麻烦,但仍会有少量不明确的地方需要注释。注释     有两种方法可以在C+程序中加上注释。在C+中,双斜杠"/"标记意味着注释的起始处。所有位于"/"及该行末尾之间的内容都是注释。编泽器将忽略行中"/"之后的部分。如果注释不止一行。在每行注释前均要加上"/"。符号"/"之间没有空格。  

8、60; 另一种在C+程序中加人注释的方法是使用符号组合"/*"及"*/",在这两个符号之间的部分为注释,会被编译器忽略。与使用"/"注释时要求每行都添加"/"不同,"/*"及"*/"注释可跨越数行。    "/* */"类的注释可插入到程序中的任何一处,且允许留出一个空格或换行,但它们不应放在任何不易阅读、有可能干扰整个程序设计的地方。通常情况下,注释都是放在行的末尾或单独成行。   &

9、#160; 关于使用哪种注释最好,目前意见尚未统一。如果运用得当。每一种("/"方式或"/* */"方式)注释均可起到有效作用。一种方法是在最后的代码中使用"/"注释。而"/* */"风格的注释则用作调试程序时的暂时性的注释。     很难断言一个程序中应该有多少注释,惟一正确的答案是"足够即可"。初学者往往欠缺这方面的经验,需要有一定的积累才能找到感觉,判断出何时加人注释才是最合适的。任何重要且不甚明了之处就是需要添加注释的地方。但是,过多的注

10、释与注释过少同样不妥,每一行都加上注释的程序将被掩盖在注释之中,以至于程序的结构隐藏在了一片饶舌的提示语中。 六、C+异常处理     1、抛出异常 异常处理机制有两个主要成分:异常的识别与发出,以及异常的处理方式。通常,不论是member functions或non-member functions,都有可能产生异常以及处理异常。异常出现之后,正常程序的执行便被悬置(suspended)。在此同时,异常处理机制开始搜寻程序中有能力处理这一异常的地点。异常被处理完毕之后,程序的执行便会重新激活( resume ),从异常处理点接续执行下去。C+通过thr

11、ow表达式产生一个异常:    inline void Triangular_iterator:check_integrity()if(_index>Triangular:_max_elems)throw iterator_overflow(_index,Triangular:_max_elems);if(_index>Triangular:_elems.size()Triangular:gen_elements(_index);throw表达式看起来有点像函数调用,上例中,如果_index大于_max_ e1ems,型别为Iterator_

12、overflow的异常对象便会被抛出(thrown ),于是第二个if语句便不会被执行。但如果_index小于或等于_max_elems,就不会有任何异常产生。程序会如我们所预期地执行。何谓抛出(thrown一个异常?所谓异常( exceptions)是某种对象。最简单的异常对象可以设计为整数或字符串。大部分时候被抛出的异常都属于特定的异常类。2、捕捉异常     我们可以利用单一或连串的catch子句来捕捉(catch)被抛出的异常对象。Catch子句由3部分组成:关键词catch、小括号内的一个型别或对象、大括号内的一组语句(用以处理异常)。异常对象

13、的型别会被拿来逐一地和每个catch子句比较如果型别符合、那么该catch子句的内容便会被执行起来。举个例子,当我们抛出Iterartor overflow对象时、3个catch子句会被依次检验。其中第三个子句所声明的异常型别与被抛出的异常对象相符,于是其后的语句内容便被执行起来。我们通过iof这个异常对象,调用异常类(exception class)中的member functions what_happened:型别为const char*的返回值会被传递给log_message函数。然后,status被设为上false。以上呈现了完整的异常处理过程流程通过所有catch子句之后,由正常程

14、序重新接手。有时候我们可能无法完成异常的完整处理.在记录消息之外,我们或许需要重抛(rethrow)异常,以寻求其它catch子句的协助,做进一步的处理。catch(iterator_overflow&iof)log_message(iof.what_happened();/rethrow for another catch clause to handlethrow;重抛时,只需写下关键词throw即可.它只能出现于catch子句中.它会将捕捉到的异常对像再一次抛出,并由另一个型别吻合的catch子句接手处理。如果我们想要捕捉任何型别的异常,可以使用一网打尽(catch-all)的方

15、式。只需在异常声明部分指定省略符号()即可。/matches all exceptionscatch(.)log_message("exception of unknown type");/clean up and exit.3.提炼异常catch子句应该和try块相应而生。try块以关键词try作为开始,然后是大括号内的一连串程序语句。catch子句置于try块的尾端,这表示如果try块内有任何异常发生,便由接下来的catch子句加以处理。    异常处理机制开始检视,异常由何处抛出,并判断是否位于try块内?如果是,就检验相应的C

16、atch子句,看它是否具备处理此异常的能力。如果有,这个异常便被处理,而正常程序也就继续执行下去。 在try块内该放置哪些语句,在try块外该放置哪些语句,这些都是程序员的权责。如果某一语句有可能引发异常,而它不位于try块内,那么该异常保证不会在此函数内被捕捉处理。这也许会有问题,也许不会有一句题-并非每个函数都必须处理每一个可能发生的异常。1.ORIGINS OF THE C+ LANGUAGEThe C+ programming languages can be thought of as the C programminglanguage with classes(and other

17、modern features added).The C program-ming language was developed by Dennis Ritchie of AT&T Bell Laboratoriesin the 1970s.It was first used for writing and maintaining the UNIX operat-ing system.(Up until that time,UNIX systems programs were written eitherin assembly language or in a language cal

18、led B,a language developed by KenThompson,the originator of UNIX.)C is a general-purpose language that canbe used for writing any sort of program,but its success and popularity areclosely tied to the UNIX operating system.If you wanted to maintain yourUNIX system,you needed to use C.C and UNIX fit t

19、ogether so well thatsoon not just systems programs but almost all commercial programs that ranunder UNIX were written in the C language.C became so popular that ver-sions of the language were written for other popular operating systems;its useIntroduction to C+3is thus not limited to computers that

20、use UNIX.However,despite its popularity,C wasnot without its shortcomings.The C language is peculiar because it is a high-level language with many of the fea-tures of a low-level language.C is somewhere in between the two extremes of a veryhigh-level language and a low-level language,and therein lie

21、s both its strengths and itsweaknesses.Like(low-level)assembly language,C language programs can directlymanipulate the computer's memory.On the other hand,C has the features of a high-level language,which makes it easier to read and write than assembly language.Thismakes C an excellent choice fo

22、r writing systems programs,but for other programs(andin some sense even for systems programs)C is not as easy to understand as other lan-guages;also,it does not have as many automatic checks as some other high-level lan-guages.To overcome these and other shortcomings of C,Bjarne Stroustrup of AT&

23、;T BellLaboratories developed C+in the early 1980s.Stroustrup designed C+to be a betterC.Most of C is a subset of C+,and so most C programs are also C+programs.(Thereverse is not true;many C+programs are definitely not C programs.)Unlike C,C+has facilities for classes and so can be used for object-o

24、riented programming.2.C+AND OBJECT-ORIENTED PROGRAMMINGObject-oriented programming(OOP)is a currently popular and powerful program-ming technique.The main characteristics of OOP are encapsulation,inheritance,andpolymorphism.Encapsulation is a form of information hiding or abstraction.Inherit-ance ha

25、s to do with writing reusable code.Polymorphism refers to a way that a singlename can have multiple meanings in the context of inheritance.Having made thosestatements,we must admit that they will hold little meaning for readers who have notheard of OOP before.However,we will describe all these terms

26、 in detail later in thisbook.C+accommodates OOP by providing classes,a kind of data type combiningboth data and algorithms.C+is not what some authorities would call a"pure OOPlanguage."C+tempers its OOP features with concerns for efficiency and what somemight call"practicality."T

27、his combination has made C+currently the most widelyused OOP language,although not all of its usage strictly follows the OOP philosophy.3.THE CHARACTER OF C+C+has classes that allow it to be used as an object-oriented language.It allows foroverloading of functions and operators.(All these terms will

28、 be explained eventually,sodo not be concerned if you do not fully understand some terms.)C+'s connection tothe C language gives it a more traditional look than newer object-oriented languages,yet it has more powerful abstraction mechanisms than many other currently popularlanguages.C+has a temp

29、late facility that allows for full and direct implementation ofalgorithm abstraction.C+templates allow you to code using parameters for types.The newest C+standard,and most C+compilers,allow multiple namespaces toaccommodate more reuse of class and function names.The exception handlingfacilities in

30、C+are similar to what you would find in other programming languages.Memory management in C+is similar to that in C.The programmer must allocatehis or her own memory and handle his or her own garbage collection.Most compilerswill allow you to do C-style memory management in C+,since C is essentially

31、a sub-set of C+.However,C+also has its own syntax for a C+style of memory manage-ment,and you are advised to use the C+style of memory management when codingin C+.This book uses only the C+style of memory management.4.C+TERMINOLOGYAll procedure-like entities are called functions in C+.Things that ar

32、e called procedures,methods,functions,or subprograms in other languages are all called functions in C+.Aswe will see in the next subsection,a C+program is basically just a function calledmain;when you run a program,the run-time system automatically invokes the functionnamed main.Other C+terminology

33、is pretty much the same as most other program-ming languages,and in any case,will be explained when each concept is introduced.七7.1 Throwing an ExceptionThe exception handling facility consists of two primary components:the recognition and raisingof an exception and the eventual handling of the exce

34、ption.Typically,an exception is raised andhandled within various member or nonmember functions.After an exception is raised,normalprogram execution is suspended.The exception handling facility searches for a portion of theprogram that is capable of handling the raised exception.After the exception h

35、as been handled,program execution resumes in the portion of the program that has handled the exception.In C+,we raise an exception by using the throw expression:inline void Triangular_iterator:check_integrity()if(_index>Triangular:_max_elems)throw iterator_overflow(_index,Triangular:_max_elems);i

36、f(_index>Triangular:_elems.size()Triangular:gen_elements(_index);The throw expression looks somewhat like a function call.In this example,if_index is greaterthan_max_elems,an exception object of type iterator_overflow is thrown.The second ifstatement is never executed,and control does not return

37、to this point in the program after theexception is handled.If_index is less than or equal to_max_elems,no exception is raised,andthe program executes as we expect.What is the exception that gets thrown?An exception is an object of some type.A simpleexception object might be an integer or a string。Mo

38、st often,thrown exceptions are class objects of an explicitly provided exception class orexception class hierarchy.7.2 Catching an ExceptionWe catch exception objects by their type using one or a series of catch clauses.A catch clauseconsists of three parts:the keyword catch,the declaration of a sin

39、gle type or single object withinparentheses,and a set of statements within curly braces that does the actual handling of theexception.The type of the exception object is compared against the exception declaration of each catchclause in turn.If the types match,the body of the catch clause is executed

40、.For example,when we throw our iterator_overflow object,the three catch clauses are examined in turn.The exceptiondeclaration of the third clause matches the type of the exception object,and the associatedstatements are executed.The what_happened()member function of our exception class isinvoked thr

41、ough iof,the exception object.The const char*return value is passed to someexternal log_message()function.Following that,status is set to false.This represents a complete handling of the exception.Normal program execution resumes at thefirst program statement following the set of catch clauses.In our example,normal programexecution begins again with the return of status.It may be that we cannot completely handle an exception.Af

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论