Begin with c++ Fekra Course #1

58
BEGIN WITH C++

description

Fekra Course c++

Transcript of Begin with c++ Fekra Course #1

Page 1: Begin with c++ Fekra Course #1

BEGIN WITH C++

Page 2: Begin with c++ Fekra Course #1

WHO ARE WE?

• Ahmed Farag Mohammed• [email protected]

• Sara Tarek Ali• [email protected]

Our target :is to keep a smile on your face while learning new stuff ;)

Page 3: Begin with c++ Fekra Course #1

NOW, WHO ARE YOU?!!

Page 4: Begin with c++ Fekra Course #1

WHY DID YOU JOIN THIS COURSE?

Hwaay you do zes, hwaaay!!??

Page 5: Begin with c++ Fekra Course #1

What is a Computer?

• A definition of a computer is an electronic device that can input data, process data and output data, accurately and at great speed.

• Data are any kind of information that can be codified in some manner (i.e. 0s , 1s) and input into the computer.

Page 6: Begin with c++ Fekra Course #1

What is a Computer? Cont.

Fast Devicewith a sequence

of commands

mn el a5er!

Input output

Take 2 numbers and add them and output the

result

Example

7, 5 12

Page 7: Begin with c++ Fekra Course #1

The Purpose of this course

Fast Devicewith a sequence

of commandsInput output

our main concern through out this course is to write the sequence of commands that the computer will

perform in high speeds to solve certain problems of our daily life.

Page 8: Begin with c++ Fekra Course #1

Lets play a game

• Type the following instruction, and see the magic happens ;)

cout << 6 + 7 ;

Page 9: Begin with c++ Fekra Course #1

Congratulations

• You just created your first Computer Program (Application) in 30 secs ;)

Page 10: Begin with c++ Fekra Course #1

Now, let’s see how this happened

Starting for the very beginning :D

Page 11: Begin with c++ Fekra Course #1

Stored Program Computer

• John Von Neumann (Dec. 28, 1903 – Feb. 8, 1957) {

Instruction 1Instruction 2Instruction 3....Instruction n}

ProgramCounter

Page 12: Begin with c++ Fekra Course #1

How can the computer understand us?

• Those instructions that you saw previously are translated to computer language (binary [0,1]).

• There are two ways to translate a program; Interpreting or Compiling.

Page 13: Begin with c++ Fekra Course #1

Interpreter

• An interpreter is a program that reads a high-level program and does what it says. In effect, it translates the program line-by-line, alternately reading lines and carrying out commands.

Page 14: Begin with c++ Fekra Course #1
Page 15: Begin with c++ Fekra Course #1

Complier

• A compiler is a program that reads a high-level program and translates it all at once, before executing any of the commands.

• Often you compile the program as a separate step, and then execute the compiled code later. In this case, the high-level program is called the source code, and the translated program is called the object code or the executable.

Page 16: Begin with c++ Fekra Course #1
Page 17: Begin with c++ Fekra Course #1

Primitives of Programming Languages

Input Output Logic

Testing Repetition

Page 18: Begin with c++ Fekra Course #1

Computers cannot Think.. People can!

• Computers are merely machines, made of silicon, iron, and other stuff.

• What mostly qualifies computers is their Speed.

• A Pentium 4 computer (2 GHz), can do addition of two numbers in 0.5 Nanosecond.

• Nanosecond is: ( 1/1,000,000,000 of a second)

Page 19: Begin with c++ Fekra Course #1

Thus, we have this rule..

• If you tell the computer to do something stupid, the computer accurately and at great speed does that stupid action!

• Your idea of a computer program not working or making a mistake is likely coming from this aspect.

Page 20: Begin with c++ Fekra Course #1

Data

• Normally, we think of data as facts and numbers such as a person’s name and address or the quantity or cost of an item purchased. However, data can also be graphical images, sound files, movies and more.Characters

ABC

Decimals

8.0023Sounds ImagesIntegers1234

Page 21: Begin with c++ Fekra Course #1

Errors

• When a program has an error in it, that error is often called a “bug.” And the process of getting all of the errors out of a program is called debugging.

• Funny story behind it :D

Page 22: Begin with c++ Fekra Course #1

Programming Languages

Page 23: Begin with c++ Fekra Course #1

Programming Languages

• The computer internally operates on the binary number system. In the binary number system, the only valid digits are 0s and 1s.

• Electronic circuits can either have some electricity in them or not. 1 means ON, and 0 means OFF.

• No one writes programs in binary, it’s very tedious.

Page 24: Begin with c++ Fekra Course #1
Page 25: Begin with c++ Fekra Course #1

C++

• C++ is the programming language we will learn in this course

• The instruction that you used at the beginning of this session is actually C++ statement “cout << 6 + 7;”

Page 26: Begin with c++ Fekra Course #1

I/O

• As we said, any program gets input from user and has an output, so how can we get input and return output ?

• Let’s see ;)

Page 27: Begin with c++ Fekra Course #1

Output

cout

Data

>>

operator

Console output stream

Data

Computer Screen

Page 28: Begin with c++ Fekra Course #1

Output examples

cout << “Hello World!!”;cout << 3;cout << “3”;cout << 5 * 15;cout << 15 / 5;

Page 29: Begin with c++ Fekra Course #1

Input

• Let’s get input from user, But where we will save this data ?!!

Page 30: Begin with c++ Fekra Course #1

Variables

• Do you remember when we talked about “Data” ?

• So, where it will be saved ?!• We create variables to save data needed

through the program or in files to be reused again and again (we will talk about files later)

• Variables shouldn’t begin by a number, and shouldn’t have any spaces

Page 31: Begin with c++ Fekra Course #1

Data types

• Since the variable holds data, so this variable should have a data type

• Data types in C++ :– int integer number– float float numbers– double big numbers (int OR float)– char characters– Adding long more size for a variable

EX: long int– bool return true or false– string

Page 32: Begin with c++ Fekra Course #1

Declaration and initialization

• int x;– Here we say that we want to declare x as a

variable from the type int (integer)• int x = 3;– Here , x is declared as an integer and also

initialized by the value “3”

• Note: it is a better practice to initialize your variables.

Page 33: Begin with c++ Fekra Course #1

Back to I/O

• Now we can use these variables to take data from the user.

• But how input actually works?

Page 34: Begin with c++ Fekra Course #1

Input

cin

Variable

<<

operator

Console input stream

Data

Computer Screen

Contains the input Data

Page 35: Begin with c++ Fekra Course #1

I/O cont’

• So now, let’s take input and save it in avariable

• We will use cin object , very similar to cout

• For example :– Int x;– cin>>x

• Here the value entered will be saved in variable x

Page 36: Begin with c++ Fekra Course #1

I/O example

int x;cout<<“Enter a number: ”;cin>>x;cout << x + 5;

Try it ;)

Page 37: Begin with c++ Fekra Course #1

I/O training

• Get Two numbers from the user and print the result of multiplying them together.

Input:

3 4

Expected out from your program:12

Page 38: Begin with c++ Fekra Course #1

Answer

int num1;int num2;cin >> num1;cin >> num2;cout << num1 * num2;

Page 39: Begin with c++ Fekra Course #1

Errors

• There is two types of errors• Syntax : appears when you complie your

code (error & warning)• Semantic (by logic) : appears when you

run your program and you don’t get the desired output.

• Example if you made a program to devide 2 numbers and you gave it 5/0, this wouldn’t give you the desired output.

Page 40: Begin with c++ Fekra Course #1

Operators

Now: * / + - % -- , ++ (before and after var) =

Next sessions: ==, < , <= , > , >= || ,&& , >> , << | , & ! , !=

Page 41: Begin with c++ Fekra Course #1

Practice operators

• Take input from user on a variable named “x”

• Increment x-variable value by 1• Declare a variable named “y”• Apply the following equation

• Decrement y-variable value by 1• Output the value of the variable y• Example: if x = 3 then y = 10

Page 42: Begin with c++ Fekra Course #1

Special characters

• \a : make a sound when the program come to it

• \n : make a new line• \r : make the cursor goes to the beginning of

the line • \t : make the cursor leave to the next tab• For example :– Cout<<“\n” ; <- prints a new line– Cout<<“\t”; <- prints a tab

• We have also -> \\ , \’ , \”

Page 43: Begin with c++ Fekra Course #1

Practice special char.s

• cout << “line1\nline2\nline3”;• cout << “row1-col1\trow1-col2\n”

<< “row2-col1\trow2-col2\n”;• cout << “my name is \“ahmed\””;• cout << “path is c:\\folder\\file.exe”;• cout << “maho enta aslan 3ayel teet\a”

Page 44: Begin with c++ Fekra Course #1

I have a little confession

I was hiding some code from you

Page 45: Begin with c++ Fekra Course #1

Simple Program

# include <iostream>using namespace std;

int main(){

cout << “Hello World!!" ;return 0;

}

Page 46: Begin with c++ Fekra Course #1

C++ Program

• Wait wait wait a moment !!• Where will I write my program ? O_o

Page 47: Begin with c++ Fekra Course #1

IDE

• IDEs are programs that help us to write new programs !!

• IDEs as : Eclipse, Netbeans, Visual Studio, Dev C++ and others

• So let’s open our IDE and write a program !

Page 48: Begin with c++ Fekra Course #1

Libraries

# include <iostream>using namespace std;

int main(){

cout << “Hello World!!" ;return 0;

}

Page 49: Begin with c++ Fekra Course #1

Libraries cont.

• What are Libraries ?!• Every thing should extend from a library

in C++ language• We declare the library on this form• #include< name of the library.h >– <> : we should surrounded the library name

with this two brackets – .h : the extension of the library

Page 50: Begin with c++ Fekra Course #1

Namespace

# include <iostream>using namespace std;

int main(){

cout << “Hello World!!" ;return 0;

}

Page 51: Begin with c++ Fekra Course #1

Namespace cont.

• We always add the line “Using namespace std;” at the beginning of the program, what does it means ??

• Why namespaces are useful ?

Page 52: Begin with c++ Fekra Course #1

Main

# include <iostream>using namespace std;

int main(){

cout << “Hello World!!" ;return 0;

}

Page 53: Begin with c++ Fekra Course #1

Main cont.

• In C++ any program should have a main from which the program begin its work

int main(){// our programreturn 0;}• So soon we will know the meaning of each

part of these stuff !• But for now we will write our program inside

the main

Page 54: Begin with c++ Fekra Course #1

Simple Program

# include <iostream>using namespace std;

int main(){

cout << “Hello World!!" ;return 0;

}

Page 55: Begin with c++ Fekra Course #1

Let’s get interactive with users

# include <iostream>using namespace std;

int main(){ int num1, num2;

cout << “Enter first number: " ;cin >> num1;cout << “Enter second number: ”;cin >> num2;cout << num1 << “+” << num2 << “=“ << num1+num2;return 0;

}

Page 56: Begin with c++ Fekra Course #1

Notes for a good programmer

• Indentation !!!• Variable names (naming ways)• Variables are case sensitive • Spaces and comments• Comments types (// , /* */)• initialize your variables

Page 57: Begin with c++ Fekra Course #1

Training 1

• Write a program that takes your name and say: – Hello “somebody”

Page 58: Begin with c++ Fekra Course #1

Training 2

• Write a program that takes from the user two numbers and calculate:– Sum– Difference– Multiplication– Division