You are on page 1of 102

C C++

MLN4EVER :
MLN4EVER9@hotmail.com :

:
.
.
( ) .

:
C C++ ..........
C C++
( )
.
...........
:

:
C

C
-
C .

C "
" " " ( The C
) programming language .
.

C ++C ++C
(Object Oriented
.) Programming ++C ++Visual C
.

C 1983
C
" " ANSI C .

:

.

C
. ( ) function


.
.

C
.

:
-1 ( (Editors
2 ( ) compilation " OBJ" .
3 ( )Linking " ."EXE .

" "Terbo C ""Quick C



.

-------------------------------------------------------------------

: C :
1- C


"" Hello C

CODE
>#include <stdio.h
()main
{
;(printf ) Hello C
}

printf
. C
.
printf
.

main " } "


" { ".

" } { " (. )block


( )main .

" "/* */
.

C .

" ." /* " " */

" " # ( ) Directive



" " stdio.h (Standard Input
) Output header file

C

.

-1 .
-2 ( ) printf ( )staements
.
.
-3

.
-4 ( ) key words ( ) printf
)Small letters( .

2 printf
.

(
. )string

printf

CODE
>#include <stdio.h
()main
{
;( printf)Welcome
;(printf) C Programmer
}

WelcomeC Programmer


( ) new line character ( \)n

CODE
>#include <stdio.h
()main
{
;(printf)Welcome \n
;(printf) C Programmer
}


Welcome
C Programmer


!!!

Future Horizons Co.


81 emarat othman
NasrCity
Cairo

CODE
>#include <stdio.h
()main
{
;(printf)Future Horizons Co.\n
;(printf)81 emarat othman \n
;(printf)NasrCity\n
;(printf)Cairo\n
}

3-

printf
printf
. .

C :

-1 ((Integers
2- ()Real numbers

.
( ).

()
.

CODE
>#include <stdio.h
()main
{
;(printf)%d \n,130
;(printf)%f\n,130.5
}

( )d%
( ) d ( ) decimal

( )f% ( ) f
( ) floating point number .


130
130.5


. .

:

.
:

+
-
*
/

() printf

CODE

>#include <stdio.h
()main
{
;(printf)%d\n,128*2
;(printf)%f\n,128.0/2
}


256
64.000000

4
.
.

- -
.
C ( )Identifiers
:

-1 ( ) Reserved words
( ) main
.
-2 ( )A-Z
( ) 9-0 ( _ )

.
-3

.
-4 C ()MY_NUMBER
( )my_number ( .)My_Number

:
C
.

(
) ( )
.

CODE
>#include <stdio.h
()main
{
/* variable declaration*/
;int a
;float b
/*Display output */
;(printf)%d\n,a
;(printf)%f\n,b
}

( )a (
)integer int .
( ( )Real float
.


( int )float
a,b


22348
476.950
( :) Assignment


( )assignment statement

.

CODE

>#include <stdio.h
()main
{
/* variable declaration*/
;int a
;float b
/* Assignment */
;a=1000
;b=796.5
/*Display output */
;(printf)%d\n,a
;(printf)%f\n,b
}


1000
796.5
( : ) Assignment statment


;a=1000
:
1000 ""a

.

:
-1 " "a
2 " "a 2 " "b 3- " "a" " b " ."c

CODE
>#include<stdio.h
()main
{
;int a
;float b,c

;a=1024
;b=a/2.0
;c= b+a

;(printf)The result is %f\n,c

" "b" " c


.

" "c

:
C :
;a = b = c = 24

:
; float a = 5.6

.

-5
.

.

25.0000000

25

(
) format modifiers
25

| |

| 0f.% | 25

| 3f.% | 25.000

f
.
75

CODE
>#include <stdio.h
()main
{
;float x
;x=75
;(printf)%.0f\n,x
;(printf)%.1f\n,x
;(printf)%.2f\n,x
}


75
75.0
75.00


25.8756

CODE
>#include <stdio.h
()main
{
;(printf)%.0f\n,3.0/4.0
;(printf)%.1f\n,3.0/4.0
;(printf)%.2f\n,3.0/4.0
}


1
0.8
0.75

6 ( : ) char variable
( )character
( .) char


( )ASCII code table

" "a :
;char a

'a = 'Z
" " a " "Z
" "c%

, character .

" "first_letter
" "A .c%
.A

CODE
>#include <stdio.h
()main
{
;char first_letter
;first_letter = A
;(printf)%c\n,first_letter
}

:
-1 c.%
2- .d%

d% .

CODE
include <stdio.h>#
()main
{
;char first_letter
;first_letter = A
;(printf)%c\n,first_letter
;)printf)%d\n,first_letter

6- ( ) String & Pointer : C

.
( ) string

C .

(
)pointer
.

( :)pointers

.
.


( ) :
;int *a

( character
)pointer :

char * a
" "a " *
" .
( ) int ( ) real ( ) char
C


( ) s%

CODE

>#include <stdio.h
()main
{
;char *a
;a = Welcome C programmer
;(printf)%s\n,a
}

:
Welcome C programmer


;char *a
:
-1 " " a .
-2" * " a .

C

.

" " a
" " Hello again .

CODE

>#include <stdio.h
()main
{
;char *a
;a = Hello again
;(printf)%s\n,a
;(printf)%c\n,*a
;(printf)%d\n,a
;(printf)%p\n,a
;(printf)%d\n,*a
}

. " " p%
.

: ( ) I/O
printf
() .
.

printf
scanf " "f "format
"

scanf .

CODE
>#include <stdio.h
()main
{
;float x,y,z
;(scanf )%f,&x
;(scanf )%f,&y
;z=x+y
;(printf)the sum of the numbers you entered is : %.2f\n,z
}

" " x,y,z


" "x :
(scanf )"%f",&x

" "y " " x,y


" "z
" " z .

" "x
Enter " " y
Enter .

scanf :

-1 printf
" " f% " "x " y".
2 " "x " "y (& )x ( ) address operator
. & ( address-of
)operator

:
printf scanf

CODE

>#include <stdio.h
()main
{
;float x,y,z
;(scanf )%f%f,&x,&y
;z=x+y
;(printf)the sum of the numbers you entered is : %.2f\n,z
}

( )
" " f %f%
" ( " , )scanf

scanf
.
.

:
Enter

CODE

>#include <stdio.h
()main
{
;int x
;float y,z
;(scanf )%d,%f,&x,&y
;z=x+y
;(printf)the sum of %d and %f is : %.2f\n,x,y,z
}

scanf
.
printf :
Please Enter the number

.

CODE

>#include <stdio.h
()main
{
;float x,y,z
;( printf)Enter the first number :
;(scanf )%f,&x
;( printf)Enter the second number :
;(scanf )%f,&y
;z=x+y
;(printf)the sum of the numbers you entered is : %.2f\n,z
}
:
scanf
. .


. .
( ) NULL character
.


( ) string variables
C .

( ) character arrays
.

.
( ) size .

( :) employee_name

CODE
;]char employee_name[20


( .) NULL

( strcpy
) ,) a " " a " "b .

" "a
" " Hello again
printf .%s

CODE

>#include <stdio.h
>#include <string.h
()main
{
;]char a[20
;(strcpy)a,Hello again
;(printf) %s\n,a
}

:
>#include <string.h
strcpy " "string.h
" " strcpy
" "string header file

. printf
" " a
.

" ] "a[20 " " 0


" "19:

CODE
]a[0], a[1],..,a[19

( )
11
.

scanf .
.

: gets
" " get string
( ) NULL
. :
;)gets)a
" " a .

CODE
>#include <stdio.h
()main
{
;]char employee_name[20
;(gets)employee_name
;(printf) Employee: %s\n,employee_name
}

" employee
" name .
.

scanf
.Enter

.
19 .

: fgets

( .) input device
( ) :

CODE
;( fgets) a, n, stdin

" " a
" " n .
" "stdin ( )

stdin
.

:
-1 ( \n (.

2- ( .) NULL

CODE
>#include <stdio.h
()main
{
;]char employee_name[20+2
;(fgets)employee_name,22,stdin
;(printf) Employee: %s\n,employee_name
}
:

:puts
" " put string gets
:
;(puts ) a

a .

gets

CODE

>#include <stdio.h
()main
{
;]char employee_name[20+1
;( puts)Enter employee_name:
;(gets)employee_name
;(puts)employee_name
}

.
puts " \"n

:fputs

fgets
.

:
;( fputs) a, stdout

a .
" " stdout .

fputs puts .

:
C
.

:
-1 ( ( Arithmetic Operators
2 ( ( Relational Operators 3- ( ) Logical Operators

( : ) Arithmetic Operators
C

()+
)) (*)
))/
C

) )%
) )-( )++


x % y :

" " x " " y

CODE
7%3

" " 1 7/3

(: )Decrement & Increment


C ++ 1
1 :

CODE
;X++
;++X

1 X :

CODE
;X=X+1

1 X :

CODE
;--X
;X--

CODE
;X=X-1

, X++ X=X+1

( : ) Relational Operators

, ( )true ()false
( )false " " 0 ( )true
" . " 1
:
int a=b=3 :

( : ) Logical Operators

:

.

C
.

( :)if statement

CODE
( if ) condition
;statement

( ) condition ( ) statement
.

.

CODE

>#include <stdio.h
()main
{
;float sum
;( printf)\n Enter the Sum :
;(scanf)%f,sum
(if )sum >50
;(printf )\n The student had passed
}


( ) 50

( )if . else statement

: 50
.
.
:

CODE

>#include <stdio.h
()main
{
;float sum
;( printf)\n Enter the Sum :
;(scanf)%f,sum
(if )sum >50
;(printf )\n The student had passed
else
;(printf)\n The student had failed
}

CODE
(if ) condition
;statement-1
else
;statement-2

( ) condition
( ) statement -1 .
( ) statement -2 .

" :
"

- -


CODE
(if ) condition
{
;statement 1
;statement 2
;statement n
}
else
{
;statement 1
;statement 2
;statement m
}



1000
.

CODE

>#include <stdio.h
()main
{
;float sum
;( printf)\n Enter the Sum :
;(scanf)%f,sum
(if )sum >50
{
;(printf )\n The student had passed
(printf)\n The percentage is : %f,)sum/1000(*100
}
else
{
;(printf)\n The student had failed
;(! printf)\ There is no percentage for failed student
}
}

- -

. .

CODE

(if ) condition 1
;statement 1
(else if ) condition-2
;statement-2
(else if) condition-3
;statement-3
..
else
;statement-n

( ) statement switch

.
.

CODE

(switch )variable
{
;case value1
;statement 1
;break
;case value2
;statement 2
;break
;case value 3
;statement 3
;break

default:
;statement
}

( )switch

( ) case
- - .

( )break
!!!
:


() break

( ) default
.

:
C
.
" " C

-1 for ) for loop (.
2 while ) while loop (. 3 .)do. while ) do-while loop .

:)for ) for loop


for ( )
( )

CODE
(for ) counter statement; condition; step

:
-1 ( : ) counter .
-2 ( :)condition
.
-3( : )step .

: for

CODE
>#include <stdio.h
()main
{
;int counter
(for ) counter=1;counter<=20;counter++
;(printf)%d,counter
}

for ( ) for
.
.
.20
1
.
1 .20

:
-1 for
.
-2 ( for
( ) ;)printf ) " %d",counter .

:)while ) while loop


..
while

CODE
( while ) conditon
{
;statement 1
;statement 2
.
.
;statement n
}

( ) condition
.
while 1 20

CODE
>#include <stdio.h
()main
{
;int counter=1
( while ) counter <=20
{
;(printf)%d,counter
;counter++
}
}

while:
1 while. 2- while

:do-while

.
do-while

CODE
do
{
;statement 1
;statement 2
.
.
;statement n
}
while ) conditon

do-while
!!!
.

++
++C : C
++C ++C
.

:
C
.
++C
.

CODE
>#include <stdio.h
()main
{
;int I
;(scanf)%d,&I
(if )I>5
{
;int j
;( printf)Enter the second number
;(scanf)%d,j
;(printf) the result is ,j*I
}

" " j

.
.

:
++C

.

.

CODE
>#include <iostream.h
{(void Add)int a=5,int b=9
;cout << a+b
;}
()main
{
;(Add)4,6
;()Add
}

( ) )(Add
( )4
( ) 6

.

:
++C " " //
C "
" */ " *." /
" " //

" " // .

:
++C
( ) class

CODE
{class class_name
private:
private data and functions
public :
public data and functions
}

( ) class .

++C
( ) public , private , protected

( :) constructors and destructors


++C
( ) constructor
.
.

( ) destructor

.
( )Ball
( .) Ball
( ~ )
( ~.) Ball

( : )Inheritance
.

.
( ) derived class ( )parent class

.


.

( :) friend functions

.

.
.

++C :


: .

. .

CODE
>#include <iostream.h
()void main
{
;cout << Every age has its own language . . .
}

++C
.

:

( ) )(main
.


.
( ) void
.

" { " " } " .


.




( )cout
( << )

( :) preprocessor directives
( )<include <iostream.h#
.
( )#
.
.

( :)Comments

.

.

++C
.

" " //
.
" "// .
" " //
" "*/ " * "/
.

CODE
// this is the first method

// this is the
// first method

/* this is the second method */

/* this is
the second
method*/


. .


" "*/ "
* "/ .

( ++C )


.
:
-1 ( ) Reserved words
( ) main
.
-2 ( )A-Z
( ) 9-0 ( _ )
.
-3

.
-4 ++C (
)MY_NUMBER ( )my_number (
.)My_Number

( :) integer variables

CODE

>#include <iostream.h
()void main
{
//define var1

;int var1

int var2; //define var2

//assign value to var1

;var1=20

;var2=var1+10

cout<< var1+10 is ; //output text


cout<<var2; // output value of var2

" "var1 " . "var2


" "int " "integer
.

++C
.


.
" "20

" "10 .
.

( :) char variables

.
.

;char variable_name

( )variable_name
.

;variable = A

( )A ( ) variable

( : ) escape sequences
++C
:
CODE

>#include <iostream.h
()main
{
;char var1=A
;char var2=\t
;char var3=\n

;cout << var1


;cout << var2
;cout << var1
;cout << var3

}
( )A .

( ) back slash

( )n .
( )t
( .) tab

( )floating point variables


.


.

CODE

>#include <iostream.h
()void main
{
//define var1

;float var1

float var2; //define var2

//assign value to var1

;var1= 50.79

;var2= var1 + 56.9

cout<< var1+ 56.9 is ; //output text


cout<<var2; // output value of var2
}


( )float ( ) floating point
.
.


.

(
).
.

. ++C

( *)

;float *ptr
.ptr

.++C

cin
.

CODE
>#include<iostream
()void main
{
;int ftemp
; cout << Enter temperature in Fahrenheit:
;cin >> ftemp
;int ctemp= )ftemp-32( * 5/9
;cout<<The temperature in Celsius is : <<ctemp<<\n
}
( )cin ( >>)
.
( ) Enter
.

( :)Structures

.
( ) fields

.

CODE
struct structure_name
{
;type field1
;type field2

;}

( )stucture_name (
) ..,field1, field2 .

()var1
( )structure1

CODE

struct structure1
{
;type field1
;type field2

;} var1



( ).
( )Student ( )Mohammed (
)name
CODE
>#include<iostream.h
struct Student
{
;char* name
;int number
;}
()main
{
;Student Sdt1
;Std1.name=Mohammed
;Cout << Std1.name
}
" "Mohammed
( )name ( .)Std1

:

.

++C
.

( :)if statement

( if ) condition
;statement

( ) condition ( ) statement
.

.

CODE

>#include <iostream.h
()main
{
;float sum
; cout<< Enter the sum
;cin >> sum

(if)sum>50
;cout<< The student had passed
}


( ) 50

( )if . else statement

: 50
.
.

CODE

>#include <iostream.h
()main
{
;float sum
; cout<< Enter the sum
;cin >> sum

(if)sum>50
;cout<< The student had passed
else
;cout<< The student had failed

(if ) condition
;statement-1
else
;statement-2

( ) condition
( ) statement -1 .
( ) statement -2 .
" :
"

- -

CODE
(if ) condition
{
;statement 1
;statement 2
.
.
;statement n
}
else
{
;statement 1
;statement 2
.
.
;statement m
}

CODE

>#include <iostream.h
()main
{
;float sum
; cout<< Enter the sum
;cin >> sum

(if)sum>50
{
;cout<< The student had passed
;cout<< His points are << sum/100
}
else
{
;cout<< The student had failed
;!! cout<< No points are calculated for failed student
}
}

) if-else- if Ladder (:

- -

. .

CODE

(if ) condition 1
;statement 1
(else if ) condition-2
;statement-2
(else if) condition-3
;statement-3
..
else
;statement-n

( ) statement switch

.
.

CODE

(switch )variable
{
;case value1
;statement 1
;break
;case value2
;statement 2
;break
;case value 3
;statement 3
;break

default:
;statement
}

( )switch

( ) case
- - .

( )break
!!!
:


() break

( ) default

.
--------------------------------

:
++C
.
" " C

-1 for ) for loop (.
2 while ) while loop (. 3 .)do. while ) do-while loop .

:)for ) for loop


for ( )

(for ) counter statement; condition; step

:
-1 ( : ) counter .
-2 ( :)condition
.
-3( : )step .

: for

CODE

>#include <iostream.h
()main
{
;int counter
(for ) counter=1;counter<=20;counter++
;cout<<counter
}

for ( ) for
.
.
.20
1
.

1 .20

:
-1 for
.
-2 ( for
( );cout << counter .

:)while ) while loop



..
while

CODE
( while ) conditon
{
;statement 1
;statement 2
.
.
;statement n
}

( ) condition
.
while 1 20

CODE
>#include <iostream.h
()main
{
;int counter=1
( while ) counter <=20
{
;cout<< counter
;counter++
}
}

while:
1- while.

2- .while

:do-while

.
do-while

CODE
do
{
;statement 1
;statement 2
.
.
;statement n
}
( while ) conditon

do-while
!!!
.

: ( : )Function & Macro


:

.
:

-1
.
-2
.
-3
.

( )1

CODE

>#include <iostream.h

(void DrawLine)(; )1
()void main
{
;cout << This is the output of the function : << \n
(DrawLine)(; )2
}

()void DrawLine
{
(for )I=1;I<=40;I++( )3
;*<<cout
}

( ) DrawLine
(*)
( )1 ( function
) declaration
.

( ) void .

( )2 ( )function calling
.

( ) 3 ( )function definition
" } " " { "
" *"

:
-1
.
-2 .

:
:
-1 ( )int functions
integer((.
2 ( ) float functions float ( ( .
3 ( ) string functions . -4 ( ) char functions ( char(.
5- ( )void function

):)2

CODE

>#include<iostream.h
(float sum)float x, float y
{
;float result
;result = x + y
(return result; )1
;}
()void main
{
;( cout << sum) 4.5 , 8.9
}

( )sum ( )main
( ) sum
.

( )1
( ) return

:
( ) sum ( )2
.
( ) DrawLine
()1

( )main function arguments


( ) main

()3

CODE
> #include < iostream.h
(][main )int argc, char*argv
{
(if)afgc!=3
{
;cout<< Arguments number error .
;(exit)1
}
;cout<<the first argument is<<argv[1]<<\n
;]cout<<the second argument is<<srgv[2
}

( )argc
( )argc
( )argument counter

( ) argv
.

( )prog1.exe
:
C:> prog1 First Second

*** Prog1 : [argv[0 :


**** First : [argv[1 :
** Second : [argv[2 :

.
( )
.
.

the first argument is First
the second argument is Second

CODE

>#include <iostream.h
#define SQUARE)A( A*A
()main
{
;int x
; cout<< Please enter a number to calculate its square
;cin >> x
;(cout << The square of << x << is : << SQUARE)x
}



#define SQUARE)A( A*A


;(SQUARE)x

.
:
-1 ( )Editors
( ) coding " "cpp. (
source file(.
2 ( ) compilation " OBJ" .
3 ( )Linking " ."EXE .


( ( ) )SQUARE)A
.
.

:

.



++C .

]A= [ 2 3 4 5 6


[A[0 .2
A[1] = 3 A[2] = 4


12
.B[0][0]=12 .

CODE
B[1][2] = 35

B[2][1] = 80

B[0][2] = 15

B[2][2] = 16

++C
.

++C
A

CODE
>#include <iostream.h
()main
{
;]int A[4

(// )1

(for)int I=0; I<4; I++


{
;] << cout<< Please enter the value of the element A[ << I
;]cin >> A[I
}

(for)int I=0; I<4; I++


{
;]cout<< The value of the element A[ << I << ] is << A[I
}
}


( ) 1 (
)int .

.

for
.
for .

: ++C

CODE

#include <iostream.h>
main)(
{
float Degrees[3][3];

// Array declaration

// Array Element entry

for )int I=0; I<3; I++(


{
for)int J=0; J<3; J++(
{
cout<< Enter the result of subject << I << for
student << J;
cin >> Degrees[I][J];
}
};
// Array elements display
for )int I=0; I<3; I++(
{
for)int J=0; J<3; J++(
{
cout<< the result of subject << I << for student << J
<<is;
cout<< Degrees[I][J];
}
};
}

for

.

: ( ) Classes & Objects



.
( )1

CODE
{class class_name
private:
private data and functions
public :
public data and functions
}

: ( ) class ( )class_name
:

( ) Reserved words ( ) main


.

( )A-Z ( ) 9-0 ( _ )
.
-

++C ( )MY_CLASS ( )my_class ( .)My_Class

: ( ) private
.

: ( ) public
.

CODE

01: #include <iostream.h>


02: class smallobj // class name
03:{
04: private:
05: int somedata; //class data
06: public:
07: void setdata )int d(; // member function to set data
08: {somedata= d;}
09: void showdata)( // mamber function to display data
10: { cout << \n Data is << somedata;}
11:};

12: void main)(


13:{
14: smallobj s1,s2;

// define two objects of the class

15: s1.setdata)1096(; //call member function to set data


16: s2.setdata)200(;

17: s1.showdata)(;
18: s2.showdata)(;
19:}


.
:

) smallobj ( : 02

" "// . C

04 :05 .

:06
.

07 10 .
10
.C .

12 . )(main

14
.

15 ( ) )(setdata
( ). ( ).
.

17 18 ( ) )(showdata ( .)s1,s2

" " .

( )

:

.
.


( ) a , b

CODE

# include iostream.h
class MyClass
{
;int a,b
public:
(MyClass )int i, int j
{
;a=i
;b=j
}
()void Show
{
;<<b

<<cout << a
}
;}
()void main
{
;(C1)2,6

MyClass
;()C1.Show
}

( ) MyClass
.

( ) a , b .
( ) )(Show ( ) a , b
( ) C1

:

.
++C
.

CODE
>#include <iostream.h
class Date
{ public:
;int day,month,year
(set_date)int d, int m, int y
};{day=d; month=m; yaer=y
;}
()main
{
;]date_array[3

Date

;(date_array[0].set_date)2,3,1990
}

( ) Date
.

( ) set_date
.


( ) pointers ( )
.
++C
( ) Date ( Date
) ;*dptr

CODE
>#include <iostream.h
class Date
{ public:
;int day,month,year
(set_date)int d, int m, int y
};{day=d; month=m; yaer=y
;}
()main
{
;Date *dptr
;Date date
;Dptr -> day=3
;Date.day=4
}
( ) date ( )dptr
( ) <- (
) day ( ). ( .) date

You might also like