Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to C++ tariq mahmoud Egyptian School on High Energy Physics 27 th May to 4 th June 2009.

Similar presentations


Presentation on theme: "Introduction to C++ tariq mahmoud Egyptian School on High Energy Physics 27 th May to 4 th June 2009."— Presentation transcript:

1 introduction to C++ tariq mahmoud Egyptian School on High Energy Physics 27 th May to 4 th June 2009

2 outline Introduction Generalities Basics Variables Control Functions Array I/O Structures Classes and Objects Templates

3 Ability to write a small program Basic knowledge to understand existing code Root Aim Introduction does not replace a systematic course not comprehensive presentation of the c++ scope you have to do a lot yourself … but A program is a series of instructions that determine how are data processed. Only few basic functions are needed, which are included in all programming languages: Input: data from the keyboard, file, network, sensor, … Output: data on screen, file, printer, controller,... Operation: a mathematical expression, assignment,... Testing and Branching: verification of conditions, different processes Loops: Repeated execution of certain sections What is programming

4 Huge feature set, everything is possible from small hardware drivers with a few lines to complex SW projects with many millions of rows. Direct connection to the hardware through pointers Full object-oriented functionality, including templates Good performance Variety of tools and libraries available Pros Introduction C: is a high-level and general purpose language. C++: is an extension of C. It is strong typing and Object-Oriented. C/C++: compiler languages Main features of C/C++ Too many features, too many freedoms Code is often illegible and chaotic Difficult to Maintain Steep learning curve for a long and full Overview Aid packages (I/O, Networking, Graphics, Databases,...) are not integrated into standard distribution. Conts

5 /* Hello-world, C Version */ #include /* pre-prozessor command */ /* start function main */ int main() { /* begin function body */ printf("Hello world \n"); /* function call printf(...) */ return(0); /* return some value */ } /* end function body */ C first steps #include int main(){ printf("Hello world \n"); return(0); } #include int main(){ printf("Hello world \n"); return(0); }

6 /* Hello-world, C Version */ #include /* pre-prozessor command */ /* start function main */ int main() { /* begin function body */ printf("Hello world \n"); /* function call printf(...) */ return(0); /* return some value */ } /* end function body */ C first steps specifies a header-file which contains needed libraries or functions. Preprocessor command Functions need a type (int) and parentheses (). The name main is special Ξ main program. Function definition, main() enclose blocks of code and declarations. Curly braces, {...} Function from the C Standard Library. Not directly part of the language. Must be declared through Header-File Function call, printf() important, every statement ends with ;. Semicolon, ; Functions give a value back. return Everything between / * and * / is ignored, no matter whether: - part of a or entire line - several lines Comments

7 /* Hello-world, C Version */ #include /* pre-prozessor command */ /* start function main */ int main() { /* begin function body */ printf("Hello world \n"); /* function call printf(...) */ return(0); /* return some value */ } /* end function body */ C first steps /* Hello-world, C++ Version */ #include // pre-prozessor command using namespace std; // declare namespace /* start function main */ int main() { /* begin function body */ cout << "Hello world" << endl; return(0); /* return some value */ } /* end function body */ C++ using namespace std; // declare namespace cout << "Hello world" << endl; additional I/O-functions compilers require namespace declaration comments also with // OOP (later) What is programming

8 first steps edit: (emacs, xemacs, nedit, …) compile: execute: compile and execute emacs Hello.C g++ -o Hello Hello.C./Hello

9 a little more // print Fahrenheit- > Celsius conversion table # include using namespace std; // declare namespace int main(){ int lower(0), upper(300), step = 20; //declaration and initialization double fahr, celsius; fahr = lower; /* the following code block.... is a while loop */ while ( fahr < = upper ) { // while loop celsius = (5.0/9.0) * (fahr - 32.0); /*calculate...*/ cout << fahr << " " << celsius << endl; // output... fahr += step; // calculate... } // end-while } // end-main a small program

10 a little more // print Fahrenheit- > Celsius conversion table #include using namespace std; // declare namespace int main(){ int lower(0), upper(300), step = 20; //declaration and initialization double fahr, celsius; fahr = lower; /* the following code block.... is a while loop */ while ( fahr < = upper ) { // while loop celsius = (5.0/9.0) * (fahr - 32.0); /*calculate...*/ cout << fahr << " " << celsius << endl; // output... fahr += step; // calculate... } // end-while } // end-main a small program

11 a little more // print Fahrenheit- > Celsius conversion table # include using namespace std; // declare namespace int main(){ int lower(0), upper(300), step = 20; //declaration and initialization double fahr, celsius; fahr = lower; /* the following code block.... is a while loop */ while ( fahr < = upper ) { // while loop celsius = (5.0/9.0) * (fahr - 32.0); /*calculate...*/ cout << fahr << " " << celsius << endl; // output... fahr += step; // calculate... } // end-while } // end-main a small program

12 a little more // print Fahrenheit- > Celsius conversion table # include using namespace std; // declare namespace int main(){ int lower(0),upper(300),step = 20; //declaration and initialization double fahr, celsius; fahr = lower; /* the following code block.... is a while loop */ while ( fahr <= upper ) { // while loop celsius = (5.0/9.0) * (fahr - 32.0); /*calculate...*/ cout << fahr << " " << celsius << endl; // output... fahr += step; // calculate... } // end-while } // end-main a small program

13 a little more // print Fahrenheit- > Celsius conversion table # include using namespace std; // declare namespace int main(){ int lower(0),upper(300),step = 20;//declaration and initialization double fahr, celsius; fahr = lower; /* the following code block.... is a while loop */ while ( fahr <= upper ) { // while loop celsius = (5.0/9.0) * (fahr - 32.0); /*calculate...*/ cout << fahr << " " << celsius << endl; // output... fahr += step; // calculate... } // end-while } // end-main a small program Do Something...

14 a little more // print Fahrenheit- > Celsius conversion table # include using namespace std; // declare namespace int main(){ int lower(0),upper(300),step = 20;//declaration and initialization double fahr, celsius; fahr = lower; /* the following code block.... is a while loop */ while ( fahr <= upper ) { // while loop celsius = (5.0/9.0) * (fahr - 32.0); /*calculate...*/ cout << fahr << " " << celsius << endl; // output... fahr += step; // calculate... } // end-while } // end-main a small program

15 upper=300 fahr = lower=0 fahr = 0+step = 20 fahr= 20+step = 40 #include using namespace std; int main(){ int lower(0),upper(300),step=20; double fahr, celsius; fahr = lower; while ( fahr <= upper ) { celsius=(5.0/9.0) * (fahr-32.0); cout <<fahr<<” “<<celsius<<endl; fahr += step; } a small program 0 -17.778 20 -6.667 40 4.444... output getting serious: a loop

16 upper=300 fahr = lower=0 fahr = 0+step = 20 fahr= 20+step = 40 while ( fahr <= upper ) { celsius=(5.0/9.0) * (fahr-32.0); cout <<fahr<<” “<<celsius<<endl; fahr += step; } while loop calculate: fahr=lower=0 → celsius=(5.0/9.0)*(0-32.0)=-17.778 output: 0 -17.778 go to next step: fahr=fahr+step=0+20=20 calculate: fahr=20 → celsius=(5.0/9.0)*(20-32.0)=-6.667 output: 20 -6.667 go to next step: fahr=fahr+step=20+20=40 calculate: fahr=40 → celsius=(5.0/9.0)*(40-32.0)=4.444 output: 40 4.444 go to next step: fahr=fahr+step=40+20=60 getting serious: a loop

17 substantial terms 1.data types 2.variable 3.expression 4.assingment 5.statement 6.control statement

18 data types In C + + a number of data types are defined by which operations are possible, eg: 1247 // int = integer 42 - 15 // subtraction of whole numbers 3.1415 * 2.0 // floating-Multipikation "Hello World" // String "Hello" + "Cairo" // string addition is possible "Hello Cairo" * 3.1415 // wrong In general: no operations with various data types possible.

19 data types Basic feature of a programming language is the ability to operate with variables. A variable is something like an identifier for a memory location where a value can be stored. A variable must have a fixed data type. Before first use a variable must be defined: type and name, eg: int i; // int variable i created i = 42 - 15; // result of an int operation is assigned double pi = 3.1415; // floating variable pi is created and initialized double wide, radius; // floating-point variables and large radius is created radius = 2.5; // radius value for assigned circumference = 2 * Pi * radius: // size is calculated according to formula and assigned string gruss = "Hello"; string name1 = "Ahmad"; string name2 = "Khaled"; string text1 = gruss + name1; string text2 = gruss + name2;

20 expression and assignement generally, any C++ command provides a result, for example: 3 + 6; x > y; 1./sqrt (2 * sigma) * exp (- (x-x0) * (x-x0) / (sigma * sigma)) // Gauss function court «" Hello World " Command in C++ is a defined operation or a function call, (no comments!) expression Variable gets a value or the result of an expression assigned, for example: a = 3 + 6; x = sqrt (2); None equation in the mathematical sense but assignment: the right expression is evaluated and the result is assigned to the left variable. b = b + 1 expression

21 statement and control-statemen executable C++ command, i.e. Expression by ';' completed 3 + 6; x = sqrt (2); court «" Hello World "; Several expressions / allocations in a statement are possible: y = (x = sqrt (a))> 0; statement ramification, loop if (a> b)... while... for... switch... break... return … later control-statement

22 arithmetic operations and allocations expressiongoal x++postincrement ++xpreincrement x--postdecrement --xpredecrement -xsign x+yaddition x-ysubtraction x*ymultiplication x/ydivision x%ymodulo pow(x,y)exponent x=yassignment x+=y (-=, /=, *=) assignment with cahneg

23 logical operations and comparisons expressiongoal false or 0false true or 1 (or !=0)true !xnegation x && y AND composition x || y OR composition x < ysmaller than x <= y smaller than or equal x > ygrater than x >= y grater than or equal x == yequal x != ynot equal

24 Gestern lernte ich einen Buchstabe Und ich dachte ich sei wissend, Heute lernte ich zwei dazu Und ich entdeckte, Dass mir noch sechsundzwanzig fehlen. ألبارحه تعلمت حرفاً فظنَنتُني عالماً, اليوم تعلمت إثنين إلى ذلك فأدركت أنني ما زِلت أجهل ستةً وعشرين ThanX


Download ppt "Introduction to C++ tariq mahmoud Egyptian School on High Energy Physics 27 th May to 4 th June 2009."

Similar presentations


Ads by Google