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

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line.
INTRODUCTION T.Najah Al_Subaie Kingdom of Saudi Arabia Prince Norah bint Abdul Rahman University College of Computer Since and Information System CS240.
True or false A variable of type char can hold the value 301. ( F )
COSC 120 Computer Programming
Chapter 8: Introduction to High-level Language Programming Invitation to Computer Science, C++ Version, Third Edition.
1 ICS103 Programming in C Lecture 2: Introduction to C (1)
Guide To UNIX Using Linux Third Edition
Introduction to C Programming
Chapter 8: Introduction to High-level Language Programming Invitation to Computer Science, C++ Version, Third Edition.
Basic Elements of C++ Chapter 2.
Copyright 2003 Scott/Jones Publishing Brief Version of Starting Out with C++, 4th Edition Chapter 1 Introduction to Computers and Programming.
VARIABLES, TYPES, INPUT/OUTPUT, ASSIGNMENT OPERATION Shieu-Hong Lin MATH/CS Department Chapel.
COMPUTER SCIENCE I C++ INTRODUCTION
CSC 125 Introduction to C++ Programming Chapter 2 Introduction to C++
Chapter 3 Getting Started with C++
High-Level Programming Languages: C++
Goals of Course Introduction to the programming language C Learn how to program Learn ‘good’ programming practices.
Programming I Introduction Introduction The only way to learn a new programming language is by writing programs in it. The first program to.
C++ Basics Structure of a Program. C++ Source Code Plain text file Typical file extension .CPP Must compile the C++ source code without errors before.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
ECE 264 Object-Oriented Software Development Instructor: Dr. Michael Geiger Spring 2009 Lecture 2: Basic C++ Programs.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 1 February 8, 2005.
Data & Data Types & Simple Math Operation 1 Data and Data Type Standard I/O Simple Math operation.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 2 Chapter 2 - Introduction to C Programming.
C++ Programming: Basic Elements of C++.
CHAPTER 2 PART #1 C++ PROGRAM STRUCTURE 1 st semester H 1 King Saud University College of Applied studies and Community Service Csc 1101 By:
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
Fundamental Programming: Fundamental Programming Introduction to C++
Chapter 0 Getting Started. Objectives Understand the basic structure of a C++ program including: – Comments – Preprocessor instructions – Main function.
C++ Lecture 1 Friday, 4 July History of C++ l Built on top of C l C was developed in early 70s from B and BCPL l Object oriented programming paradigm.
CSC1201: Programming Language 2 Lecture 1 Level 2 Course Nouf Aljaffan (C) CSC 1201 Course at KSU1.
Chapter 2 part #1 C++ Program Structure
A first program 1. #include 2. using namespace std; 3. int main() { 4. cout
C++ / G4MICE Course Session 1 - Introduction Edit text files in a UNIX environment. Use the g++ compiler to compile a single C++ file. Understand the C++
CHAPTER 1: INTRODUCTION C++ Programming. CS 241 Course URL: Text Book: C++ How to Program, DETITEL & DEITEL, eighth Edition.
Recap……Last Time [Variables, Data Types and Constants]
General Computer Science for Engineers CISC 106 Lecture 12 James Atlas Computer and Information Sciences 08/03/2009.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 2 - Introduction to C Programming Outline.
2/4/2016Engineering Problem Solving with C++, Second Edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 2 Simple C++ Programs.
CS201 Introduction to Sabancı University 1 Chapter 2 Writing and Understanding C++ l Writing programs in any language requires understanding.
 2007 Pearson Education, Inc. All rights reserved. A Simple C Program 1 /* ************************************************* *** Program: hello_world.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2 Introduction to C++
 2003 Prentice Hall, Inc. All rights reserved Basics of a Typical C++ Environment C++ systems –Program-development environment –Language –C++
1 Structure of Simple C++ Program Chapter 1 09/09/13.
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
Bill Tucker Austin Community College COSC 1315
Chapter Topics The Basics of a C++ Program Data Types
Introduction to C++ Programming
Chapter 2: Introduction to C++
Chapter 2 - Introduction to C Programming
Chapter 2 part #1 C++ Program Structure
Basic Elements of C++.
Chapter 2 - Introduction to C Programming
Basic Elements of C++ Chapter 2.
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
Introduction to C++ Programming
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
Chapter 2: Introduction to C++.
Programs written in C and C++ can run on many different computers
Engineering Problem Solving with C++ An Object Based Approach
Engineering Problem Solving with C++ An Object Based Approach
Capitolo 1 – Introduction C++ Programming
Chapter 2 - Introduction to C Programming
C++ Programming Basics
Introduction to C Programming
Chapter 2 part #1 C++ Program Structure
Presentation transcript:

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

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

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

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

/* 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); }

/* 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

/* 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

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

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 ); /*calculate...*/ cout << fahr << " " << celsius << endl; // output... fahr += step; // calculate... } // end-while } // end-main a small program

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 ); /*calculate...*/ cout << fahr << " " << celsius << endl; // output... fahr += step; // calculate... } // end-while } // end-main a small program

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 ); /*calculate...*/ cout << fahr << " " << celsius << endl; // output... fahr += step; // calculate... } // end-while } // end-main a small program

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 ); /*calculate...*/ cout << fahr << " " << celsius << endl; // output... fahr += step; // calculate... } // end-while } // end-main a small program

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 ); /*calculate...*/ cout << fahr << " " << celsius << endl; // output... fahr += step; // calculate... } // end-while } // end-main a small program Do Something...

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 ); /*calculate...*/ cout << fahr << " " << celsius << endl; // output... fahr += step; // calculate... } // end-while } // end-main a small program

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 output getting serious: a loop

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)= output: go to next step: fahr=fahr+step=0+20=20 calculate: fahr=20 → celsius=(5.0/9.0)*( )= output: go to next step: fahr=fahr+step=20+20=40 calculate: fahr=40 → celsius=(5.0/9.0)*( )=4.444 output: go to next step: fahr=fahr+step=40+20=60 getting serious: a loop

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

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

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 = ; // result of an int operation is assigned double pi = ; // 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;

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

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

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

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

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