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

24
introduction to introduction to C++ C++ tariq mahmoud tariq mahmoud Egyptian School on High Energy Egyptian School on High Energy Physics Physics

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

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

introduction to introduction to

C++C++

tariq mahmoudtariq mahmoudEgyptian School on High Energy Egyptian School on High Energy

PhysicsPhysics2727thth May to 4 May to 4thth June 2009 June 2009

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

outline

IntroductionGeneralitiesBasics

VariablesControlFunctionsArrayI/O

StructuresClasses and ObjectsTemplates

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

Ability to write a small programBasic knowledge to understand existing codeRoot

Aim

Introduction

does not replace a systematic coursenot comprehensive presentation of the c++ scopeyou 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

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

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 pointersFull object-oriented functionality, including templatesGood performanceVariety 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 freedomsCode is often illegible and chaoticDifficult to MaintainSteep learning curve for a long and full OverviewAid packages (I/O, Networking, Graphics, Databases, ...) are not integrated into standard distribution.

Conts

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

/* Hello-world, C Version */#include <stdio.h> /* 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 <stdio.h>int main(){ printf("Hello world \n"); return(0); }

#include <stdio.h>int main(){ printf("Hello world \n"); return(0); }

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

/* Hello-world, C Version */#include <stdio.h> /* 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

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

/* Hello-world, C Version */#include <stdio.h> /* 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 <iostream> // pre-prozessor commandusing 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++

<iostream> 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

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

first steps

• edit: (emacs, xemacs, nedit, …) • compile:• execute:

compile and execute

emacs Hello.C g++ -o Hello Hello.C./Hello

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

a little more

// print Fahrenheit->Celsius conversion table#include <iostream>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

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

a little more

// print Fahrenheit->Celsius conversion table

#include <iostream>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

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

a little more

// print Fahrenheit->Celsius conversion table#include <iostream>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

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

a little more

// print Fahrenheit->Celsius conversion table#include <iostream>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

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

a little more

// print Fahrenheit->Celsius conversion table#include <iostream>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 ...Do Something ...

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

a little more

// print Fahrenheit->Celsius conversion table#include <iostream>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

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

upper=300

fahr = lower=0

fahr = 0+step = 20

fahr= 20+step = 40

#include <iostream>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.77820 -6.66740 4.444. . .

output

getting serious: a loop

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

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

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

substantial terms

1. data types2. variable3. expression4. assingment5. statement6. control statement

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

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.

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

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;

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

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

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

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

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

arithmetic operations and allocations

expression goalx++ postincrement++x preincrementx-- postdecrement--x predecrement-x signx+y additionx-y subtractionx*y multiplicationx/y divisionx%y modulopow(x,y) exponentx=y assignment

x+=y(-=, /=, *=)

assignment with cahneg

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

logical operations and comparisons

expression goalfalse or 0 false

true or 1 (or !=0) true!x negation

x && y AND compositionx || y OR compositionx < y smaller than

x <= y smaller than or equal

x > y grater than

x >= y grater than or equal

x == y equalx != y not equal

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

Gestern lernte ich einen Buchstabe

Und ich dachte ich sei wissend,

Heute lernte ich zwei dazu

Und ich entdeckte,

Dass mir noch sechsundzwanzig fehlen.

! حرفًا تعلمت ألبًارحه ! عًالمًا ,فظن5نُت3ني

إثنين تعلمت اليومذلك إلى

فأدركتأجهل ِزGلت مًا أنني

وعشرين سُتًة!

ThanThan

XX