Begin with c++ Fekra Course #1

Preview:

DESCRIPTION

Fekra Course c++

Citation preview

BEGIN WITH C++

WHO ARE WE?

• Ahmed Farag Mohammed• ahmed.farag1111@gmail.com

• Sara Tarek Ali• eng.saratarek@gmail.com

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

NOW, WHO ARE YOU?!!

WHY DID YOU JOIN THIS COURSE?

Hwaay you do zes, hwaaay!!??

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.

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

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.

Lets play a game

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

cout << 6 + 7 ;

Congratulations

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

Now, let’s see how this happened

Starting for the very beginning :D

Stored Program Computer

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

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

ProgramCounter

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.

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.

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.

Primitives of Programming Languages

Input Output Logic

Testing Repetition

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)

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.

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

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

Programming Languages

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.

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;”

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 ;)

Output

cout

Data

>>

operator

Console output stream

Data

Computer Screen

Output examples

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

Input

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

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

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

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.

Back to I/O

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

• But how input actually works?

Input

cin

Variable

<<

operator

Console input stream

Data

Computer Screen

Contains the input Data

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

I/O example

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

Try it ;)

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

Answer

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

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.

Operators

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

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

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

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 -> \\ , \’ , \”

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”

I have a little confession

I was hiding some code from you

Simple Program

# include <iostream>using namespace std;

int main(){

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

}

C++ Program

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

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 !

Libraries

# include <iostream>using namespace std;

int main(){

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

}

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

Namespace

# include <iostream>using namespace std;

int main(){

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

}

Namespace cont.

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

• Why namespaces are useful ?

Main

# include <iostream>using namespace std;

int main(){

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

}

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

Simple Program

# include <iostream>using namespace std;

int main(){

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

}

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;

}

Notes for a good programmer

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

Training 1

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

Training 2

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

Recommended