计算机C语言专业外文翻译.docx
《计算机C语言专业外文翻译.docx》由会员分享,可在线阅读,更多相关《计算机C语言专业外文翻译.docx(7页珍藏版)》请在冰点文库上搜索。
计算机C语言专业外文翻译
计算机C语言专业外文翻译
CAHistoryofCCandCTheCprogramminglanguagewascreatedinthespiritoftheCandCprogramminglanguages.Thisaccountsforitspowerfulfeaturesandeasylearningcurve.ThesamecantbesaidforCandCbutbecauseCwascreatedfromthegroundupMicrosofttookthelibertyofremovingsomeofthemoreburdensomefeatures—suchas
pointers.ThissectiontakesalookattheCandClanguagestracingtheirevolutionintoC.TheCprogramminglanguagewasoriginallydesignedforuseontheUNIXoperatingsystem.CwasusedtocreatemanyUNIXapplicationsincludingaCcompilerandwaseventuallyusedtowriteUNIXitself.ItswidespreadacceptanceintheacademicarenaexpandedtoincludethecommercialworldandsoftwarevendorssuchasMicrosoftandBorlandreleasedCcompilersforpersonalcomputers.TheoriginalWindowsAPIwasdesignedtoworkwithWindowscodewritteninCandthelatestsetofthecoreWindowsoperatingsystemAPIsremaincompatiblewithCtothisday.FromadesignstandpointClackedadetailthatotherlanguagessuchasSmalltalkhadalreadyembraced:
theconceptofanobject.YoulllearnmoreaboutobjectsinChapter8quotWritingObject-OrientedCode.quotFornowthinkofanobjectasacollectionofdataandasetofoperationsthatcanbeperformedonthatdata.Object-stylecodingcouldbeaccomplishedusingCbutthenotionofanobjectwasnotenforcedbythelanguage.Ifyouwantedtostructureyourcodetoresembleanobjectfine.Ifyoudidntfine.Creallydidntcare.
Objectswerentaninherentpartofthelanguagesomanypeopledidntpaymuchattentiontothisprogrammingparadigm.Afterthenotionofobject-orienteddevelopmentbegantogainacceptanceitbecameclearthatCneededtoberefinedtoembracethisnewwayofthinkingaboutcode.Cwascreatedtoembodythisrefinement.ItwasdesignedtobebackwardlycompatiblewithCsuchthatallCprogramswouldalsobeCprogramsandcouldbecompiledwithaCcompiler.ThemajoradditiontotheClanguagewassupportforthisnewobjectconcept.TheClanguageaddedsupportforclasseswhicharequottemplatesquotofobjectsandenabledanentiregenerationofCprogrammerstothinkintermsofobjectsandtheirbehavior.TheClanguageisanimprovementoverCbutitstillhassomedisadvantages.CandCcanbehardtogetahandleon.Unlikeeasy-to-uselanguageslikeVisualBasicCandCareveryquotlowlevelquotandrequireyoutodoalotofcodingtomakeyourapplicationrunwell.Youhavetowriteyourowncodetohandleissuessuchasmemorymanagementanderrorchecking.CandCcanresultinverypowerfulapplicationsbutyouneedtoensurethatyourcodeworkswell.Onebugcanmaketheentireapplicationcrashorbehaveunexpectedly.BecauseoftheCdesigngoalofretainingbackwardcompatibilitywithCCwasunabletobreakawayfromthelowlevelnatureofC.MicrosoftdesignedCtoretainmuchofthesyntaxofCandC.DeveloperswhoarefamiliarwiththoselanguagescanpickupCcodeandbegincodingrelativelyquickly.ThebigadvantagetoChoweveristhatitsdesignerschosenottomakeitbackwardlycompatiblewithCandC.Whilethismayseemlikeabaddealitsactuallygoodnews.CeliminatesthethingsthatmakesCandCdifficulttoworkwith.BecauseallCcodeisalsoCcodeChadtoretainalloftheoriginalquirksanddeficienciesfoundinC.CisstartingwithacleanslateandwithoutanycompatibilityrequirementssoitcanretainthestrengthsofitspredecessorsanddiscardtheweaknessesthatmadelifehardforCandCprogrammers.IntroducingCCthenewlanguageintroducedinthe.NETFrameworkisderivedfromC.HoweverCisamodernobjected-orientedfromthegrounduptype-safelanguage.LanguagefeaturesThefollowingsectionstakeaquicklookatsomeofthefeaturesoftheClanguage.Ifsomeoftheseconceptsdontsoundfamiliartoyoudont
worry.Allofthemarecoveredindetailinlaterchapters.ClassesAllcodeanddatainCmustbeenclosedinaclass.Youcantdefineavariableoutsideofaclassandyoucantwriteanycodethatsnotinaclass.Classescanhaveconstructorswhichexecutewhenanobjectoftheclassiscreatedandadestructorwhichexecuteswhenanobjectoftheclassisdestroyed.Classessupportsingleinheritanceandallclassesultimatelyderivefromabaseclasscalledobject.Csupportsversioningtechniquestohelpyourclassesevolveovertimewhilemaintainingcompatibilitywithcodethatusesearlierversionsofyourclasses.AsanexampletakealookataclasscalledFamily.Thisclasscontainsthetwostaticfieldsthatholdthefirstandlastnameofafamilymemberaswellasamethodthatreturnsthefullnameofthefamilymember.classClass1publicstringFirstNamepublicstringLastNamepublicstring
FullNamereturnFirstNameLastNameNoteSingleinheritancemeansthataCclasscaninheritfromonlyonebaseclass.Cenablesyoutogroupyourclassesintoacollectionofclassescalledanamespace.Namespaceshavenamesandcanhelporganizecollectionsofclassesintologicalgroupings.AsyoubegintolearnCitbecomesapparentthatallnamespacesrelevanttothe.NETFrameworkbeginwithSystem.MicrosofthasalsochosentoincludesomeclassesthataidinbackwardscompatibilityandAPIaccess.TheseclassesarecontainedwithintheMicrosoftnamespace.DatatypesCletsyouworkwithtwotypesofdata:
valuetypesandreferencetypes.Valuetypesholdactualvalues.Referencetypesholdreferencestovaluesstoredelsewhereinmemory.Primitivetypessuchascharintandfloataswellasenumeratedvaluesandstructuresarevaluetypes.Referencetypesholdvariablesthatdealwithobjectsandarrays.Ccomeswithpredefinedreferencetypesobjectandstringaswellaspredefinedvaluetypessbyteshortintlongbyteushortuintulongfloatdoubleboolcharanddecimal.Youcanalsodefineyourownvalueandreferencetypesinyourcode.Allvalueandreferencetypesultimatelyderivefromabasetypecalledobject.Callowsyoutoconvertavalueofonetypeintoavalueofanothertype.Youcanworkwithbothimplicitconversionsandexplicitconversions.Implicitconversionsalwayssucceedanddontloseanyinformationforexampleyoucanconvertaninttoalongwithoutlosinganydatabecausealongislargerthananint.Explicitconversionsmaycauseyoutolosedataforexampleconvertingalongintoanintmayresultinalossofdatabecausealongcanholdlargervaluesthananint.Youmustwriteacastoperatorintoyourcodetomakeanexplicitconversionhappen.Cross-ReferenceRefertoChapter3quotWorkingwithVariablesquotformoreinformationaboutimplicitandexplicitconversions.Youcanworkwithbothone-dimensionalandmultidimensionalarraysinC.Multidimensionalarrayscanberectangularinwhicheachofthearrayshasthesamedimensionsorjaggedinwhicheachofthearrayshasdifferentdimensions.Classesandstructurescanhavedatamemberscalledpropertiesandfields.Fieldsarevariablesthatareassociatedwiththeenclosingclassorstructure.YoumaydefineastructurecalledEmployeeforexamplethathasafieldcalledName.IfyoudefineavariableoftypeEmployeecalledCurrentEmployeeyoucanretrievetheemployeesnamebywritingCurrentEmployee.Name.Propertiesarelikefieldsbutenableyoutowritecodetospecifywhatshouldhappenwhencodeaccessesthevalue.IftheemployeesnamemustbereadfromadatabaseforexampleyoucanwritecodethatsaysquotwhensomeoneasksforthevalueoftheNamepropertyreadthenamefromthedatabaseandreturnthenameasastring.quotFunctionsAfunctionisacallablepieceofcodethatmayormaynotreturnavaluetothecodethatoriginallycalledit.AnexampleofafunctionwouldbetheFullNamefunctionshownearlierinthischapterinthe
Familyclass.Afunctionisgenerallyassociatedtopiecesofcodethatreturninformationwhereasamethodgenerallydoesnotreturninformation.Forourpurposeshoweverwegeneralizeandrefertothembothasfunctions.Functionscanhavefourkindsofparameters:
Inputparametershavevaluesthataresentintothefunctionbutthefunctioncannotchangethosevalues.Outputparametershavenovaluewhentheyaresentintothefunctionbutthefunctioncangivethemavalueandsendthevaluebacktothecaller.Referenceparameterspassinareferencetoanothervalue.Theyhaveavaluecomingintothefunctionandthatvaluecanbechangedinsidethefunction.Paramsparametersdefineavariablenumberofargumentsinalist.CandtheCLRworktogethertoprovideautomaticmemorymanagement.Youdontneedtowritecodethatsaysquotallocateenoughmemoryforanintegerquotorquotfreethememorythatthisobjectwasusing.quotTheCLRmonitorsyourmemoryusageandautomaticallyretrievesmorewhenyouneedit.ItalsofreesmemoryautomaticallywhenitdetectsthatitisnolongerbeingusedthisisalsoknownasGarbageCollection.Cprovidesavarietyofoperatorsthatenableyoutowritemathematicalandbitwiseexpressions.Manybutnotalloftheseoperatorscanberedefinedenablingyoutochangehowtheoperatorswork.Csupportsalonglistofstatementsthatenableyoutodefinevariousexecutionpathswithinyourcode.Flowcontrolstatementsthatusekeywordssuchasifswitchwhileforbreakandcontinueenableyourcodetobranchoffintodifferentpathsdependingonthevaluesofyourvariables.Classescancontaincodeanddata.Eachclassmemberhassomethingcalledanaccessibilityscopewhichdefinesthemembersvisibilitytootherobjects.Csupportspublicprotectedinternalprotectedinternalandprivateaccessibilityscopes.VariablesVariablescanbedefinedasconstants.Constantshavevaluesthatcannotchangeduringtheexecutionofyourcode.Thevalueofpiforinstanceisagoodexampleofaconstantbecauseitsvaluewontbechan