Example: bachelor of science

Introduction to C++ (and C) Programming

IntroClassesE ciencyOOPI ntroductionto C++(andC)ProgrammingHansPetterLangtangen 1; Informatics, Oslo2 January LangtangenIntroductionto C++(andC)ProgrammingIntroClassesE ciencyOOPO utline1 Introto C++programmingAboutC andC++Introductory C++exampleManipulatedata lesMatrix-vector productTheC preprocessorExercisesAboutclassesin C++A simpleclass2 ClassprogrammingClassComplexA vector classStandard TemplateLibrary3E ciency;C++ :ODEsolversClassesfor LangtangenIntroductionto C++(andC)ProgrammingIntroClassesE ciencyOOPC ontentsGentleintroductionto C++FileI/OArrays andloopsDetailedexplanationof classeswithbuilt-inarithmeticsComputatio nale ciencyaspectsObject-orientedprogramminga ndclasshierarchiesUsingC++objectsin LangtangenIntroductionto C++(andC)Progra

Point: C++ o ers abstractions, i.e., complicated variables that hide lots of low-level details. Something similar is o ered by Java. H. P. Langtangen Introduction to C++ (and C) Programming. Intro Classes E ciency OOP C/C++ Hello World I/O A*x Macros Exercises Classes Simple class Python implementation #!/usr/bin/env python

Tags:

  Programming, Abstraction

Information

Domain:

Source:

Link to this page:

Please notify us if you found a problem with this document:

Other abuse

Transcription of Introduction to C++ (and C) Programming

1 IntroClassesE ciencyOOPI ntroductionto C++(andC)ProgrammingHansPetterLangtangen 1; Informatics, Oslo2 January LangtangenIntroductionto C++(andC)ProgrammingIntroClassesE ciencyOOPO utline1 Introto C++programmingAboutC andC++Introductory C++exampleManipulatedata lesMatrix-vector productTheC preprocessorExercisesAboutclassesin C++A simpleclass2 ClassprogrammingClassComplexA vector classStandard TemplateLibrary3E ciency;C++ :ODEsolversClassesfor LangtangenIntroductionto C++(andC)ProgrammingIntroClassesE ciencyOOPC ontentsGentleintroductionto C++FileI/OArrays andloopsDetailedexplanationof classeswithbuilt-inarithmeticsComputatio nale ciencyaspectsObject-orientedprogramminga ndclasshierarchiesUsingC++objectsin LangtangenIntroductionto C++(andC)

2 ProgrammingIntroClassesE ciencyOOPR equiredbackgroundProgrammingexperiencewi theitherJavaor Fortran/MatlabInterestin numericalcomputingwithC++Interestin low-leveldetailsof thecomputerKnowledgeof someC is advantageous(butnotrequired) LangtangenIntroductionto C++(andC)ProgrammingIntroClassesE ciencyOOPA boutlearningC++C++is a complicatedcomputerlanguageIt takes timeto masterC++{ oneyear is theruleof thumbFourdays canonlygivea tasteof C++Youneedto work intensivelywithC++in yourownprojectsto masterthelanguageC++exposesyouto lotsof \low-leveldetails"{ thesearehiddenin languageslike Java,MatlabandPythonHopefully, youwillappreciatethespeedand exibility of C++ LangtangenIntroductionto C++(andC)ProgrammingIntroClassesE ciencyOOPT eachingphilosophyIntensivecourse.}}

3 Lectures9-12 Hands-ontraining13-16 Learn fromdissectingexamplesGetin touchwiththedirty workGetsomeoverviewof advancedtopicsFocusonprinciplesandgeneri cstrategiesContinuedlearningonindividual basisThiscoursejustgetsyoustarted- usetextbooks,referencemanualsandsoftware examplesfromtheInternetfor LangtangenIntroductionto C++(andC)ProgrammingIntroClassesE ciencyOOPR ecommendedattitudeDiveintoexecutableexam plesDon'ttryto understandeverythingTry to adaptexamplesto newproblemsLook uptechnicaldetailsin manuals/textbooksLearn ondemandStay LangtangenIntroductionto C++(andC)ProgrammingIntroClassesE ciencyOOPC/C++HelloWorldI/OA*xMacrosExer cisesClassesSimpleclassWhydoyouneedto learn \old"compiledlanguages?

4 BecauseC, C++,andFortran(77/95)are themoste cientexistingtoolsfor intensivenumericalcomputingBecausetonsof fastandwell-testedcodesare availableinFortran,C/C++Newer languageshaveemphasizedsimplicity andreliability {at thecostof computationale ciencyTo getspeed,youneedto diveintothedetailsof compiledlanguages,andthiscourseis a rst, LangtangenIntroductionto C++(andC)ProgrammingIntroClassesE ciencyOOPC/C++HelloWorldI/OA*xMacrosExer cisesClassesSimpleclassCC is a dominatinglanguagein UnixandWindowsenvironmentsTheC syntaxhasinspiredlotsof popular languages(Awk,C++,Java,Perl,Python,Ruby) Numeroustools(numericallibraries, ,MPI)are writteninC; interfacingthemrequiresC knowledgeC is extremelyportable;\all"machinescancompil eandrunCprogramsC is verylow levelandcloseto themachineUnlimitedpossibilities.}

5 Onecandoanythingin CProgrammersof high-levellanguagesoftengetconfusedbystr ange/unexpectederrors in LangtangenIntroductionto C++(andC)ProgrammingIntroClassesE ciencyOOPC/C++HelloWorldI/OA*xMacrosExer cisesClassesSimpleclassC++C++extendsC withnicersyntax:-declare variableswhereveryouwant-in/outfunctiona rgumentsusereferences(insteadof pointers)classesfor implementinguser-de neddatatypesa standard library (STL)for frequentlyuseddatatypes (list,stack,queue,vector, hash,string,complex,..)object-orientedpr ogramminggenericprogramming, ,parameterizationof variabletypesviatemplatesexceptionsfor error handlingC is a subsetof C++ LangtangenIntroductionto C++(andC)ProgrammingIntroClassesE ciencyOOPC/C++HelloWorldI/OA*xMacrosExer cisesClassesSimpleclassC versusotherlanguagesFortran77 is more primitivebutmore reliableMatlabis as simple/primitiveas Fortran77,butwithmanymore high-levelcommands(=easyto use)

6 C++is a supersetof C andmuchricher/higher-level/reliableJavai s simplerandmore reliablethanC++Pythonis evenmore high-level,butpotentiallyslowFortran90/9 5is simplerthanJava/C++anda goodalternativeto LangtangenIntroductionto C++(andC)ProgrammingIntroClassesE ciencyOOPC/C++HelloWorldI/OA*xMacrosExer cisesClassesSimpleclassSpeedof C versusspeedof otherlanguagesC is regardedas veryfastFortran77 may yieldslightlyfastercodeC++andFortran90/9 5are in generalslower,butC++isverycloseto C in speedJavais LangtangenIntroductionto C++(andC)ProgrammingIntroClassesE ciencyOOPC/C++HelloWorldI/OA*xMacrosExer cisesClassesSimpleclassSomeguidelinesC programmersneedto be concernedwithlow-leveldetailsthatC++(and Javaor Fortran)

7 ProgrammerscanomitDon'tuseC unlessyouhaveto - useC++insteadThebestsolutionis oftento combinelanguages:Pythontoadministeruseri nterfaces,I/Oandcomputations,withintensi venumericsimplementedin C++or LangtangenIntroductionto C++(andC)ProgrammingIntroClassesE ciencyOOPC/C++HelloWorldI/OA*xMacrosExer cisesClassesSimpleclassHighvs low levelprogramsGoal:make a window onthescreenwiththetext\HelloWorld"Implem entationsin1C andtheX11library2C++ LangtangenIntroductionto C++(andC)ProgrammingIntroClassesE ciencyOOPC/C++HelloWorldI/OA*xMacrosExer cisesClassesSimpleclassC/X11implementati on(1)#include< >#include<X11 >#include<X11 >#defineSTRING"Hello,world"#defineBORDER1 #defineFONT"fixed"XWMH intsxwmh= {(InputHint|StateHint)}

8 ,/* flags*/False,/* input*/NormalState,/* initial_state*/0,/* iconpixmap*/0,/* iconwindow*/0, 0,/* iconlocation*/0,/* iconmask*/0,/* Windowgroup*/}; LangtangenIntroductionto C++(andC)ProgrammingIntroClassesE ciencyOOPC/C++HelloWorldI/OA*xMacrosExer cisesClassesSimpleclassC/X11implementati on(2)main(argc,argv)intargc;char**argv;{ Display*dpy;/* X serverconnection*/Windowwin;/* WindowID */GCgc;/* GC to drawwith*/XFontStruct*fontstruct;/* Fontdescriptor*/unsignedlongfth,pad;/* Fontsizeparameters*/unsignedlongfg,bg,bd ;/* Pixelvalues*/unsignedlongbw;/* Borderwidth*/XGCV aluesgcv;/* StructforcreatingGC */XEventevent;/* Eventreceived*/XSizeHintsxsh;/* Sizehintsforwindowmanager*/char*geomSpec ;/* Windowgeometrystring*/XSetWindowAttribut esxswa;/* */if ((dpy= XOpenDisplay(NULL))== NULL){fprintf(stderr,"%s:can'topen%s\en" ,argv[0],XDisplayName(NULL));exit(1).}

9 } LangtangenIntroductionto C++(andC)ProgrammingIntroClassesE ciencyOOPC/C++HelloWorldI/OA*xMacrosExer cisesClassesSimpleclassC/X11implementati on(3)if ((fontstruct= XLoadQueryFont(dpy,FONT))== NULL){fprintf(stderr,"%s:display%s doesn'tknowfont%s\en",argv[0],DisplayStr ing(dpy),FONT);exit(1);}fth= fontstruct-> +fontstruct-> ;bd = WhitePixel(dpy,DefaultScreen(dpy));bg = BlackPixel(dpy,DefaultScreen(dpy));fg = WhitePixel(dpy,DefaultScreen(dpy));pad= BORDER;bw = 1; (PPosition| PSize); fth+ pad* 2; XTextWidth(fontstruct,STRING,strlen(STRI NG))+ pad* 2; (DisplayWidth(dpy,DefaultScreen(dpy)) )/2; (DisplayHeight(dpy,DefaultScreen(dpy)) )/2;win= XCreateSimpleWindow(dpy,DefaultRootWindo w(dpy), , , , ,bw,bd,bg); LangtangenIntroductionto C++(andC)ProgrammingIntroClassesE ciencyOOPC/C++HelloWorldI/OA*xMacrosExer cisesClassesSimpleclassC/X11implementati on(4)XSetStandardProperties(dpy,win,STRI NG,STRING,None,argv,argc, XSetWMHints(dpy,win, DefaultColormap(dpy,DefaultScreen(dpy)); CenterGravity.))

10 XChangeWindowAttributes(dpy,win,(CWColor map| CWBitGravity), fontstruct->fid; fg; bg;gc = XCreateGC(dpy,win,(GCFont| GCForeground| GCBackground), XSelectInput(dpy,win,ExposureMask);XMapW indow(dpy,win); LangtangenIntroductionto C++(andC)ProgrammingIntroClassesE ciencyOOPC/C++HelloWorldI/OA*xMacrosExer cisesClassesSimpleclassC/X11implementati on(5)/** Loopforever,examiningeachev))


Related search queries