180 daraga cpp course session-1

27
180Daraga- Cpp course BY: Moustafa Mohamed Ali Mid-Year 2013/2014 1

Transcript of 180 daraga cpp course session-1

Page 1: 180 daraga cpp course session-1

180Daraga-

Cpp course BY: Moustafa Mohamed Ali

Mid-Year

2013/2014

1

Page 2: 180 daraga cpp course session-1

Outline Variables, Data types , casting & I/O

Compilers and IDEs

Control Statements

Loops

Functions

Arrays & String

Structures & Files

Pointers and Dynamic allocation

Recursion

STL.

2013/2014 2

Page 3: 180 daraga cpp course session-1

Introduction

Why programming languages !?

That’s how we communicate with the

machines and tell the computer what to

do.

2013/2014 3

Page 4: 180 daraga cpp course session-1

Evolution of programming

languages

Machine code

Time consuming, very difficult

Assembly

Written for one pc and can’t run on

another

High level programming languages

2013/2014 4

Page 5: 180 daraga cpp course session-1

Why using C++ !?

Maintainability

Modifying code is much easier

It’s OOP

Portability

Can’t be written and compiled on any

CPU.

C++ gives you access to some lower level

functionality (Pointers , bytes ,…etc)

2013/2014 5

Page 6: 180 daraga cpp course session-1

Tools we will use

Eclipse (Recommended)

Codeblocks (Recommended)

Dev

Netbeans

MS c++

2013/2014 6

Page 7: 180 daraga cpp course session-1

Hello World

2013/2014 7

Page 8: 180 daraga cpp course session-1

RUN !

2013/2014 8

Page 9: 180 daraga cpp course session-1

IN DEEP (Libraries) !

What is iostream !?

It’s Header that defines the standard

input/output stream objects .

it has two classes istream , ostream

Cout it’s an object from the class ostream << it’s called insertion operator

Cin it’s an object from the class istream >> it’s called extraction operator

2013/2014 9

Page 10: 180 daraga cpp course session-1

IN DEEP (Cont.)

Namespace std Sort of directory of names.

many standard C++ identifiers are

defined

When we use it we tell the compiler to look

for any identifier we haven’t defined in

the std (Standard) namespace.

2013/2014 10

Page 11: 180 daraga cpp course session-1

IN DEEP (Cont.)

Int main () The program needs to know from where to

start executing the program, the main

function is the starting point

Return 0 It’s a value that tells the operating system

that this program executed successfully

with no errors

2013/2014 11

Page 12: 180 daraga cpp course session-1

IN DEEP (Cont.)

(int argc, char** argv) They are called parameters and they will

be discussed next sessions (functions

session )

2013/2014 12

Page 13: 180 daraga cpp course session-1

Important rules

Semi-colon “ ; “

It’s tell the compiler that this line of code is

finished

Cin takes this operator >> , while Cout

takes this operator <<

Write a readable code

Use tabs

Don’t forget brackets … etc

2013/2014 13

Page 14: 180 daraga cpp course session-1

Compiler VS IDE

Compiler

It’s a program that transform from high level

languages to machine code to let the CPU

understands what you wrote, computers

doesn’t know but 0’s and 1’s

Best compiler is GNU

2013/2014 14

Page 15: 180 daraga cpp course session-1

Compiler VS IDE (Cont.)

Integrated development environment it

provide the developer a set of features

like

Source code editor

Debugger

Intelligent code completion

Eclipse , NetBeans , Dev ….

2013/2014 15

Page 16: 180 daraga cpp course session-1

Compilation process

2013/2014 16

Page 17: 180 daraga cpp course session-1

Compilation process (Cont.)

Preprocessor

It processes include-files , replace the #include with the content

Compiler

Output an incomplete object code (machine code, binary form 0’s and 1’s)

Linker

Linking refers to the creation of a single executable file from multiple object files.

2013/2014 17

Page 18: 180 daraga cpp course session-1

Compilation process (Cont.)

In C++, all these steps are performed

early, before you start running a

program. In some other languages, they

are done during the execution

process, which takes time. This is one

of the reasons C++ code runs far

faster than code in many more recent

languages.

2013/2014 18

Page 19: 180 daraga cpp course session-1

Data Types

2013/2014 19

Page 20: 180 daraga cpp course session-1

How to declare a variables !?

A variable in C++ is a name for a piece of

memory that can be used to store information

DataType Variablename ;

Int x ;

Double y ;

Char c ;

Variables names can’t begin with numbers,

can’t be reserved in the langauge it self like

(return , int , double ..etc)

2013/2014 20

Page 21: 180 daraga cpp course session-1

Variables

We can initialize it by setting a value using

assignment operator “=”

Int x = 5

Char y = ‘n’

….etc

2013/2014 21

Page 22: 180 daraga cpp course session-1

Inputs

We can take inputs from user by using

cin>>variable name

Cin>>x;

Cin>>mychar ;

….etc

2013/2014 22

Page 23: 180 daraga cpp course session-1

Outputs

We can print the value of the number by

using the cout << variable name

Cout<<x;

Cout<<myChar ;

….etc

Note that cout<<“x” will print x on the

console

Cout<<x; it will print the value of x

2013/2014 23

Page 24: 180 daraga cpp course session-1

How to write a code !?

Think in very primitive way . What do I

want to do ? What do I need?

Don’t start writing code unless you know

what you will write

We tell computers to do what we want

they are stupid but very fast

Write readable code, organized sothat

you can trace it

2013/2014 24

Page 25: 180 daraga cpp course session-1

Practice

Write a program that takes from the user

his age and print it

Write a program to takes from the user his

gender and print it (M: Male , F:Female)

2013/2014 25

Page 26: 180 daraga cpp course session-1

Readings

More about compilation process

http://faculty.cs.niu.edu/~mcmahon/CS241

/Notes/compile.html

How to design a program!?

http://www.learncpp.com/cpp-tutorial/1-

10a-how-to-design-your-first-programs/

2013/2014 26

Page 27: 180 daraga cpp course session-1

Questions

2013/2014 27