You are on page 1of 285

Elixir

C++

..
OOP ,Linked List , Files, Exceptions , Template ..
Elixir In C++ Language

1426

12

12 ........................................................
14 ........................................................
16 ........................................................
16 .............................................
16 .............................................
17 ..............................................

17 ..............................................

18 ..............................................
18 ......................................
18.............................................
18 ..............................................

19 ............................................
19 ..............................................
sizeof
)( 20 ...........................................
20 ..............................................

21 ..............................................

21 ..............................................
)(1
22 ..............................................
)(2
23 ..............................................

23 ..............................................

25

2
25 ..............................................

25 ..............................................
if
26 ..............................................
if/else
27 ..............................................
else/if
29 ..............................................

31 ..............................................
switch
34 .................. if
35 ..............................................
35 ..............................................

38 ..............................................
goto
39 ..............................................

40 ..............................................
do/while
40 ..............................................

41 ..............................................
while
43 ..............................................

44 ..............................................
for
45 ..............................................

46 ..............................................
break
48 ..............................................
continue

49 .........................................
50 .........................................cmath
53

53 ...............................................

53 ..............................................
53 ...............................................

54 .......................................
54 ...............................................

55 ...............................................

55 ...............................................

55 ...............................................

57 ...............................................

57 ...............................................

58 ...............................................

) ( 61 .................................
61 .................................
62 ...............................................
getline
62 ...............................................

63 ...............................................
ctype
66 .........
68 ...............................................

70

70 ...............................................

72 ...............................................

73 .............................................
74 ...............................................
75 .................................
75 ...............................................

77 .....................................
77 ...............................................

77 ...............................................

77 ...............................................

79 ...............................................

79 ...............................................

79 ...............................................

79 ...............................................
void
80..............................................
81

5
81 ...............................................

83 ...............................................
83 ...............................................

83 ...............................................

84 ...............................................

84 ...............................................

87 ...............................................

87 ...........................................

88 ....................................
88 ...............................................

)89 ...........................................(::
89 ...............................................
90 ........
93 .......................
93 ...............................................
95 .........................
95 ...............................................

97 ...............................................

99 ...............................................
101 ................................
102 ...............................................

102 ...............................................
104 ..........................
104 ...............................................

105 ...............................................
) (105 ....................................
108 ...............................................

110 ...............................................

110 ...............................................

112....................................
113

113 ...............................................

113 ...............................................

114 ...............................................

: 114 ......................
) ( 115 ........................................
116 ..............................................
116 ...............................................
120 ............................................
121 ...............................................

123 ...............................................

123 ............................
123 .............................................
124 ...............................................
this
125 ...............................................

126 .............................................
127 ...............................................

smaltalk128 ...........................................
128 ...............................................

129 ...............................................

130 ...............................................

130 ...............................................
)(1
132 ...............................................
)(2
134...............................................
)(3
138

138 ...........................

139 ...........................................
142 ...............................................

145 ...............................................

145 ..................................... num
148 ...............................................
this
148 ...............................................

151 ...............................................

151 ...............................................
)(+
154 ...............................................

156 ...............................................

158 ...............................................
158 ...............................................
) (
160............................ Fraction
167

String8

167 ............................
168 ............................
sting
169 ......................................
170 ...............................................

170 ...............................................
) (substr
) ( end ) (171 ......................................... begin
171 ...............................................
) (capacity
171 ...............................................

173 ......................................
) (174 ............................................... erase
175 ............................................... string
177

177 .............
177 ...............................................

178 ............................
178 ...............................................

179 ...............................................

180 ...............................................
180 .........................
183 ..............................................
184 .....................
184 .......
)( 185 .....................................
188 ...............................................

190 .........................
190 ............................

192 ...............................................

194 ...............................................

194 ......................................

198

10

198 ...............................................

198 ...............................................

198 ..............................................
199 ...............................................
1
205 ...............................................

206 ...............................................

208 ...........................
211..........................
214

11
214 ...............................................

214 ...............................................

214 ..............................................
215 ...............................................

218 ...............................................
catch
218 ...............................................

221 ...............................................
224................................
227

12

227 ...............................................

227 ...............................................
ios
227 ..................................... Formatted File I / O
229 ...............................................
231 ...............................................

232 ............................................... write
234 .....................................
236 ..............
238 ...............................................
) (open
239 ...............................................
241 ..................................
241 ..............
243 ....................................
245

13
245 ...............................................

245 ..............................................
245 ...............................................
245 ...............................................

245 ............................................
247 ...............................................

249 ...............................................

250 ...............................................
deque
251 .....................................
252 ...............................................

253 ...............................................
set
255 ...............................................
map
257 ...............................................

258 ...............................................

259 ............................................

259 ...............................................
260...............................................
263

280
281
283

14

( )
( )
( )



....









.
270


.
" "






.
" "


for .. while
. math
" "




.
" "Pointers




.
" " Function



.
" " Object



.
" Operator Overloading
"
)
(
Fraction

.
" " string

string


.
" " Inheritance




.
" " Linked List



.
" Handling Exceptions
"

.
" " Handling With Files



.
" Standard Template
" Library

.
" "

UML
.
""

.


.
:
CODE
1. CODE
2. CODE
3. CODE

.

.


Function
.
Visual
C++ file
New Files C++ source file
Build
compile Ctrl+F5
.


sultan_altaif@yahoo.com



Introduction to C++ Language
Programming



:
CODE
>1. # include <iostream.h

)(2. main
{ 3.
; " 4. cout << "Hii C++
;5. return 0
} 6.

. Hii C++
.
:

. iostream
iostream

# include
iostream
input output stream
iostream
:

) ( main
) ( main
) ( main

) ( main

) ( main
.
:

iostream cout
cout
cout
<<

cout
. Hii C++

) " ( "Hii C++ ;
.
:
main
return 0

; .
:

.

[ [ ) (. main

0 o
.







.


:

CODE
>1. # include <iostream.h

2.

)(main
{
; " "Hii C++

<<

3.
4.

cout

5.

};return 0



.


.




.
CODE
>1. # include <iostream.h
)(2. main
{

3.

;int num1 , num2

4.

; " cout << "the first number:\n

5.

;cin >> num1

6.

;"cout << " the second number:\n

7.

;cin >> num2

8.

;cout << "the Value is: " << num1+num2

9.

;return 0

10.

11.

1 2 3 10 11
.
:






num1 num2 num1 num2

int integer int
int char
num1 num2 .
) ( ,

) ; ( .

:
; 1. int num1
;2. int num2

.
:


:
" "the first number:\n




) " ( "\n
\n


.
\n

ENTER Word
.
:


num1
num2
iostream cout
cin
) ( >>
cout
.
:

cout
num1+num2

) ( num1+num2 num1
num2 .
.

.



:

.

.


.
: Variable

.

.


.
:


@
_


.
:

int

float

char
double long .


1
bool
0 256
1
char
4 2
int

4
Float

4
double
:
short long
: int
;short int number=0
;long int index=0


int :
;short number=0
;long index=0

:Constants





const :
; const int number=14



:
;const int number
;number=14

: Declarations and Definitions



.

:
;int number =4



.
: Arithmetic Operations
:
-1 ) : ( +
-2 ) : ( -
-3 ) * ( :
-4 ): (/
-5 )( %


int .
: Relation Operator

char
:
<
=<
>
=>
==
.
: Assignment Operator And Expressions
)
= (


:
;char a=b


b . a
i
2 :
;i=i+2

=
= + :
; i+=2

: Conditional Expressions


a
b . max
) if (a > b
; max = a
)if ( b < a

1
2
3

; max = b
)if ( b == a
;max = a = b

4
5
6

if


. if
a b

.
==
a b
=
=
==
.
: Increment and Decrement Operators

++ . --



a b :
; a = ++b

b
) ( 1 b . a
b 6
b 7
a a 7
:
; a = b ++


b a
b
b 6
a a 6
b . 7

-- .
: sizeof
sizeof
:
; )sizeof (int

int
:
; sizeof a

a .
)( :

cout <<


:
;"cout << "Hellow C++


:
; cout << a

a .

:
; "cout << "Please: " << a << b << "Hellow


:
; "cout << "Hellow" << endl << "World

:
Hellow
World

cout
\n
.
:
3.
\t
.
\n
.
\r
.
\a
) .( back space
\b

" " .
cin


:




.



iostream
std cin cout
iostream

.
;using namespace std


. std

.
:



.

// :
// this is a

int a=0

//




* /
*/
:
/* the compiler

int a=0

cannot read thie*/


.
)(1
.
Hellow Worlad
I am a programmer

) :
(
:
CODE
>1. #include <iostream
;2. using namespace std

)(3. int main


{ 4.
;5. cout << "Hellow World\n I am a programmer " << endl
;6. return 0
} 7.


\n
.

) (.h
std


.
):(2

.
:

if.


2 2
2 0 0
.

.
cin
cin :
CODE
>1. #include <iostream
;2. using namespace std
)(3. int main

{ 4.
;5. int a=0
;"6. cout << "Enter The Number:\t
;7. cin >> a
)8. if (a%2==0
"9. cout << "\nThe Number is divide by 2\n
;return 0

10.

11.

int
7
8 2
0 9
.

.
:
.

.


.
;} enum Days { sat , sun , mon

enum
Days .

:
;const int sat = 0
;const int san = 1
;const int mon = 2


0
:
; } enum Days { sat = 1 , sun , mon

1 .

.
) (: function

.

) ( main
.



.




.


Control Flow
:



100


.

:
:
-1 .
-2 .
.

:





:
-1 if.
-2 . switch

:if
if :
{ )if (expression
;statement1
;statment2
}
) ( if

if
. if
:
CODE
>1- #include <iostream

;2- using namespace std

)(3- int main


{ 4-
;5- int i=0 ,j=0
; 6- cin >> i >> j
{

) 7- if (i > j

; "8- cout << "The number i is bigger than j


} 9-
;return 0

10-

11-




8 . if

:if/else
if
if if


if
. if
:
{ )if (expression
; statement1
;statement2
}
else
{
;statement3
;statement4
}
:
if
else if
if
. else

.

CODE
>#include <iostream

12-

;using namespace std

13-

)(int main

14-

15-

;int i=0 ,j=0

16-

; cin >> i >> j

17-

) if (i > j

18-

; "cout << "The number i is bigger than j

19-

20-

} ; "else { cout << "error

21-

;return 0

22-

23-

21

. if
if/else if
if

.



.

:else/if


.
{ )if (expression
;statement1
;statement2
;statement3
}
{ )else if (expression
;statment1

}
{ )else if (expression
;statement
}
else
{
;statement
}

.

CODE
>1- #include <iostream
;2- using namespace std

)(3- int main


{ 4-
;5- int i=0 ,j=0
; 6- cin >> i >> j
{

) 7- if (i > j

; "8- cout << "The number i is bigger than j


} 9-
)else if (j > i

10-

; "cout << "The number j is bigger than i

11-

12-

{ )else if ( j=i

13-

; "cout << "there is no bigger number

14-

else

15-

;return 0

16-

17-

} ; "{ cout << "error

10 15
else if


else
.
else/if
38-30 :
{

) 1- if (i > j

; "2- cout << "The number i is bigger than j

} 3-
{

)4- if (j > i

; "5- cout << "The number j is bigger than i


} 6-
{ )7- if ( j=i
; "8- cout << "there is no bigger number
} ; "{ cout << "error

9- else

else/if if
.
-1 else/if
else/if
else/if

if
if


.
-2 if
else
if i
j
4
5
6 7
else 9
if 7
if
9
else/if
.
:

.
:
.
CODE
>1- #include <iostream
;2- using namespace std
3)(4- int main
{ 5-

;float a,b

6-

;char x

78-

; "cout << "Enter Number1:\t

9-

;cin >> a

10-

; "cout << "Enter Number2:\t

11-

;cin >> b

12-

;"cout << "Enter the operator\t

13-

;cin >> x

141516-

;cout << endl << endl

1718-

;"cout << "Result:\t

1920}; if (x=='+') { cout << a+b

21-

};else if (x=='-') { cout << a-b

22-

};else if (x=='*') { cout << a*b

23-

};else if (x=='/') { cout << a/b

24-

else

25-

};"{ cout << "Bad Command

2627-

;cout << endl

2829-

;return 0
}

30-


else/if
21 25 x

25


.
x else/if

.

:switch
switch if
if
:
{ )switch (expression
; case const-expr: statements
; case const-expr: statements
; default: statements
}

) ( x
switch
x

) else
( else/if
switch
:
-1

switch else/if
.
-2 switch

switch
break

default
.

switch : else/if
CODE
>#include <iostream

1-

;2- using namespace std


3)(4- int main
{ 5-
;float a,b

6-

;char x

78-

; "cout << "Enter Number1:\t

9-

10-

cin >> a;

11-

cout << "Enter Number2:\t" ;

12-

cin >> b;

13-

cout << "Enter the operator\t";

14-

cin >> x;

1516-

cout << endl << endl;

1718-

cout << "Result:\t";

1920-

switch (x) {

21-

case '+':

22-

cout << a+b ;

23-

break;

24-

case '-':

25-

cout << a-b;

26-

break;

27-

case '*':

28-

cout << a*b;

29-

break;

30-

case'/':

31-

cout << a/b;

32-

break;

33-

default:

34-

cout << "Bad Command";

35-

3637-

cout << endl;

383940-

return 0;
}


: switch
1- switch (x) {
2-

case '+':

3-

cout << a+b ;

4-

break;

5-

case '-':

;cout << a-b

6-

;break

7-

case '*':
;cout << a*b
;break

8910-

case'/':

11-

;cout << a/b

12-

;break

13-

default:
;"cout << "Bad Command

1415-

16-

switch if
case
:
)'if ( x=='+

5 6 *
x
switch 2
*
5
8


switch
break break

x

. switch
: switch
switch


switch
.

:
default
else )
( ) (.

:break
break switch

switch break
switch
. switch

switch } {
else/if




syntax


) (.
: if



true false

:
;bool value=true
)if (value
;"cout << "Hellow

value true if
value true
if :
)if (value==true

:
)if (!value

:
)if (value==false

} {
if
.

:


.

if if

. if

:
-1 ) ( : And && .
-2 ) ( : OR || .
-3 )( :Not ! .
):( And

true
:
) ) if ( (total > 90) && (total < 100

if
.
) :( OR

true
) (
if:
) if ( (total < 0) || (total > 100

if
. 100
) : ( NOT
true


)
( :
))if ( !(numberOne% numberTwo == 0


. if
:

) ..( .
else/if
.
.

CODE
1- #include <iostream>
2- using namespace std;
34- int main()
5- {
6-

float degree=0;

7-

char mark;

89-

cout << "Please Enter Your degree:\t" ;

10-

cin >> degree;

1112-

if ((degree <=100) && (degree>= 90))

13-

mark='A';

1415-

else if ((degree <90) && (degree>= 80))

16-

mark='B';

1718-

else if ((degree <80) && (degree>= 70))

19-

mark='C';

2021-

else if ((degree <70) && (degree>= 60))

22-

mark='D';

2324-

else if ((degree <60) || (degree>= 0))

25-

mark='F';

2627-

else if((degree >100) || (degree < 0)) {

28-

cout << "False degree" << endl;return 0;

29-

30-

else

31-

return 0 ;}

32-

cout << endl;

33-

cout << "Your Mark:\t" << mark ;

34-

cout << endl;

3536-

return 0;

37-

{cout << "Bad command" << endl;

6 7
.
10
else/if 102

100 if
27 :
{ ))1- else if((degree >100) || (degree < 0
;cout << "False degree" << endl

2-

;return 0

3}

4-

else/if


102 true
false


False degree
return 0 %100



.
else/if
.


.

else/if . 24

if :
{ ) )(if (cin.fail

1-

;cout << "False degree" << endl

2-

;return 0

3}

4-

cin.fail .

cin
if

.

.
: goto








10
:
CODE
>1- #include <iostream
;2- using namespace std
3)(4- int main
{ 5-
;float degree=0

6-

;float total=0

7-

;int i=0

89-

ss:
cout << "Enter the degree of course number " << i+1

1011-

;<< endl

12-

;cin >> degree

13-

;total=total+degree

14-

;i++

15-

)if (i<5

16-

;goto ss

1718-

"cout << "The Avreg is:\t

19-

;<< total/5 << endl

2021-

;return 0
}

22-

8-6


)
4 0 .( 1

:

10 ) (ss:
.
13
.
15 i
i .
if
. 17
ss
goto 10
.
i 4
15 . 5
if
goto .
20

:
14

grade total
grade 14 :
;total=total+degree

grade total

total 100
grade total 100
20 grade
14 grade
total ) 100
( total 100 .120
grade
total grade


.

: goto
ss
goto goto


goto
. . do/while
: do/while

do/while
goto
.
:
do
{
;statement1
;statement2
; )} while (expression
do/while do
while
. do
if goto
:
{ 1- do
cout << "Enter the degree of course number " << i+1

2-

;<< endl

3-

;cin >> degree

4-

;total=total+degree

5-

;i++

6-

;)} while (i<5

7-


.
.
:

. do/while
CODE
>1- #include <iostream
;2- using namespace std
3-

)(4- int main


{ 5-
;double number=0

6-

;int i=0

7-

;"cout << "please Enter The Number:\t

8-

;cin >> number

9-

;cout << endl << endl

10-

;cout << "Number\t\tOther\t\tValue"<< endl

11-

do

12-

13-

;"\t\t

" << cout << number

14-

;"\t\t

cout << i

15-

;cout << i*number

16-

;cout << endl

17-

;i++

18-

;)} while ( i<=10

19-

;return 0

20-

" <<

21-

9
10 11
.
do/while 17-14
18

while
11
.
do/while
for . while
: while
while do/while


while
:
{ )while (expression
;statement1
;statement2
;statement3
}


.
CODE
>1. #include <iostream
;2. using namespace std
3.
)(4. int main
{ 5.
;'char d='a

6.

;"cout << "Please Enter What You want \n

7.
8.

{)'while (d!='.
;cin >> d

9.
10.
11.

12.
13.

;cout << endl << "Finish" << endl

14.
15.

;return 0
}

16.

9 ) ( .
d . while
.
do/while . while

0 :10
CODE
>1. #include <iostream
;2. using namespace std
3.
)(4. int main
{ 5.
;int number=0

6.
7.

{ )while (number <=10

8.

;"cout << "The number is :\t

9.

;cout << number

10.

;cout << endl

11.

;number++

12.
}

13.

;return 0

14.
}

15.


.
:

.


:
CODE
>1. #include <iostream
;2. using namespace std
3.
)(4. int main
{ 5.
;int number=0

6.

;int max=0

7.

;int min=0

8.
9.
10.

;"cout << "Please Enter The First Number:\t

11.

;cin >> min

12.
13.

;"cout << "Please Enter The Last Number:\t

14.

;cin >> max

15.
16.

;if (!(min%2==0)) min++

17.
18.

;number=min

19.
20.

{ )while(number < max

21.

;"cout << "The Next Number is\t

22.

;cout << number << endl

23.

24.

;number=number+2

25.

26.
27.

;return 0
}

28.

6 7 8





.

2
.
17
.
19
. number
while 21 25
24 .
while
.
: for
for
while
for :
{ )for ( expr1 ; expr2 ; expr3
;statement1
;statement2
;statement3
}
:
: expr1 .
: expr2 .
: expr3 .
0 10
for
.
>1. #include <iostream
;2. using namespace std
3.

4. int main()
5. {
6.

int number;

7.
8.

for (number=0;number <=10;number++) {

9.

cout << "The number is :\t";

10.

cout << number;

11.

cout << endl;

12.

13.

return 0;

14.

:


. if for
CODE
1. #include <iostream>
2. using namespace std;
3.
4. int main()
5. {
6.

int number=0;

7.

int max=0;

8.

int min=0;

9.
10.
11.

for (int i=0; i< 10;i++) {

12.

cout << "Enter the number:\t";

13.

cin >> number;

14.
15.

if (number > max)

16.

max=number;

17.
18.

if (number < min)

19.

min=number;

20.
21.

;cout << endl << endl

22.

;cout << "The Max Number is:\t" << max

23.

;cout << "\nTne Min Number id:\t" << min

24.

;cout << endl

25.
26.
27.

;return 0

28.
}

29.

max min
number
. i
for 11 10
. for

max
max 15
. 16
min
. min
number

23 . 24
11 i
for


.

.

.
: break
break
switch if.
break
.
goto
continue
.
break
.

2


.
CODE
>1. #include <iostream
;2. using namespace std
3.
)(4. int main
{ 5.
;int number=0

6.
7.

;"cout << "Please Enter The Number:\t

8.

;cin >> number

9.
10.

)for (int i=number-1 ; i>1 ; i=i-1

11.

12.
13.

)if (number%i==0

14.

;break

15.

16.
)if (i==1
; " are

17.

cout << endl << "The Number

;"cout << endl << "The Number not are

18.
19.

else

20.
21.

;cout << endl

22.
23.

;return 0
}

24.


for
for 11

1

1
2
17 if

1

1 19
.
: continue
continue

continue
.


:
>1. #include <iostream
;2. using namespace std
3.
)(4. int main
{ 5.
;int number=0

6.
7.

;"cout << "please Enter the number:\t

8.

;cin >> number

9.
10.
11.

;cout << endl << endl

12.
)for (int i=0 ; i<=number ; i++

13.

14.
15.

))if (!(i%2==0

16.

;continue
; cout << "The number is: " << i

17.

;cout << endl

18.
19.

20.
21.
22.
23.

;return 0
}

24.

9
.
for
.
15
for 16

continue ) for
17 ( 18 for
17 18
.
:

if/else

.
CODE
>1. #include <iostream
;2. using namespace std
3.
4.
)(5. int main
{ 6.
;int Number=0

7.
8.

;cin >> Number

9.

;cout << "The Abs Value" << endl

10.

;int Abs = Number < 0 ? -Number : Number

11.

;cout << Abs << endl

12.
13.
14.

;return 0
}

15.

Number
Abs Number .
11 :
;int Abs=Number < 0 ? -Number : Number

Number
Abs
Number . Abs

: if/else
CODE
>1. #include <iostream
;2. using namespace std
3.

4.
)(5. int main
{ 6.
;int Number=0
;cin >> Number

7.
8.

;cout << "The Abs Value" << endl

9.

;int Abs

10.

;)if (Number < 0) Abs=(-Number

11.

;else Abs=Number

12.

;cout << Abs << endl

13.
14.
15.

;return 0
}

16.

: cmath

. cmath
floor : ceil

floor
12.9
12 floor
ceil
12.1 ceil
. 13
.
CODE
>1. #include <iostream
>2. #include <cmath
;3. using namespace std
4.
)(5. int main
{ 6.
; double Num=0 ,Val=0

7.
8.

;"

cout << "Enter The Num:

9.

;cin >> Num

10.
11.

;)Val=floor(Num

12.

;" << Val << endl

13.

)cout << "Number (floor

14.

;" << Val << endl

;)Val=ceil(Num

15.

)cout << "Number (ceil

16.
17.
18.

;return 0
}

19.

floor . ceil
pow : sqrt
pow
:
; )Number= pow (Number , power

Number power
Number.
sqrt
:
;)Number= sqrt (Number

Number
:
CODE
>1. #include <iostream
>2. #include <cmath
;3. using namespace std
4.
)(5. int main
{ 6.
; double Num=0 ,Val=0

7.
8.

;"

cout << "Enter The Num:

9.

;cin >> Num

10.
11.

;" << Val << endl

;)Val=sqrt(Num

12.

)cout << "Number (sqrt

13.
14.

; "

cout << "Enter The Power

15.

;cin >> Val

16.
17.

;)Val=pow(Num , Val

18.

;" << Val << endl

19.

)cout << "Number (pow

20.
21.

;return 0
}

22.

) (: rand
rand
iostream
.
; ) (int Number= rand

.
0 6
%:
;int Number= (rand( )%5)+1


Arrays And Strings
:

12
float
1000
.

.

;]int mark[10


:
.
] [.


int
mark
][20
;

:
:
]int mark[2
;
]int mark[5
;
]int mark[8
;

;
;
;

]int mark[1
]int mark[4
]int mark[7

;
;
;
;

]mark[0
]mark[3
]mark[6
]mark[9

int
int
int
int


.

] int mark[0 ] int mark[9


.



] int mark[0

:

:


] int stud[10
int
float

:
CODE
1

>#include <iostream.h
) ( main
{

]int stud[10

;float Avrege

<< cout

)for (i=0 ; i<10 ; i++

; ,total=0 , i

; "grades of stud:\n

; " << Avrege

"Please Enter all

;cout << "grade number" << i+1 << endl

all student is:

; ]cin >> stud[i

10

; ] total=total+stud[ i

11

12

;Avrege=total /10

13

cout << "The Avrege of

14

;return 0

15

16


int i for
i

total


. for
:
for
i<10 i<12




..... i>10

:
for
:
;}int mark[7] = { 5,10,90,100,90,85,15

.

:
;}int mark[] = { 5,10,90,100,90,85,15

.

.
:
.

:
.1 : .
.2 : )
(
.

:
;]int mark[10] [8






.
CODE
>#include <iostream.h

) (main

; int employee[3][5] , size[4] , i , j , sum=0

;size [3]=0

;cout << "Please Enter all employees salary" << endl

6
7

) for (j=0 ; j < 3 ; j ++

;cout << " Enter the department " << j+1 << endl

10

) for (i=0 ; i < 5 ; i++

11

12

;cout << " Employee number " << i+1 << endl
; ] cin >> employee[ i ] [ j

13

; sum= employee[ i ] [ j ] + sum

14
15

16

; size[i] = sum/5

17

;cout << " The avreg is" << size[i] << endl

18

;"________________________________"<< cout
;]size[3] = size[3] + size [i

18

sum=0

19

20
21

<< " cout << " The avrege of all salary of employee is:

;size[3] << endl


;return 0

22

23

for
for
for
i < 5
for

for 16 19

] size [i
size
19
sum

.sum

:

.

:



:

; }int mark[10] = { 100,90,69,45,87,52,99,98,99,88

:

)
(


.

.

found

:
CODE
>#include <iostream.h

) (main

3
4

int mark[10] = { 100,90,65,45,87,52,99,97,87,98} , found ,

; index
;int search

5
6

;cout << "Please enter the mark you want it\t" << endl

;cin >> search

9
)for ( index=0;index<10;index++

10

11

)if (mark[index] == search

12

13

;found=1

14

;break

15

16

else

17

; found=0

18
19

; << index++

20

)if (found=1

21

"cout << "The number of student is:

22

else

23

; "cout << "No Body has this mark

24

;return 0

25

26




search
12 found 1 for
21 12
17 0 found

found 21
found 1 22
found 24
23
!.
.
: Bubble Sorting






1 :

50
32
93
2
74
. 32
50
32
50
93
2
74
32
93

2
50
93
32
74
2 74


50 93

2
32
93
50
74
32 74

93
50
2
32
50
93
74
50 74 .




1 C Languge ) (



)
( 1 )
(.
:
CODE
>1. #include <iostream
;2. using namespace std
3.
)(4. int main
{ 5.
;}int array[5]={50,32,93,2,74

6.

;int sure=0

7.

;int x=0

8.

;"cout << "Here is the Array befor sorted\n

9.
10.

)for (int j=0;j<5;j++

11.

;cout << array[j] << endl

12.
13.

{ )for (int i=0;i<5-1;i++


;sure=0

14.

{ )for (int j=i; j<5;j++

15.
16.

{ )]if (array[j] <array[i


;]x=array[j

17.

;]array[j]=array[i

18.

;array[i]=x

19.

;sure=1

20.
21.

}
}

22.

;if (sure ==0) break

23.
24.

25.
;"cout << "Here is the Array after sorted\n

26.

)for (i=0;i<5;i++

27.
28.

;cout << array[i] << endl

29.
30.

;return 0
}

31.



.


..
.

) (
:

char

:
;}'char word[]= { 'P','r','g','r','a','m','\0

Program word
\0

:
;cout << word << endl

.


)( :
;"char word[]= "Hellow C++



9
10
Hellow C++
.
:

:
;]char name[100
;cin >> name



:
Mohamed Abdullah

cin
cin
.
cin get
. :
;]char name[100
;) cin.get (name , 99


99

' '\n Enter
.
: getline

' '\n ) ( get
.
cin getline .
get
) (
.
:

) ( strcpy

:
CODE
>1. #include <iostream
>2. #include <cstring
;3. using namespace std
4.
)(5. int main
{ 6.
;]char string1[100

7.

;"char string2[]= "I am a good programming

8.
9.

;)strcpy(string1,string2

10.
11.

;cout << string1 << endl

12.

;cout << string2 << endl

13.

14.
15.

;return 0
}

16.

string
c :
cstring strcpy
string1
string2
string1 12 13
strcpy.
: ctype
ctype

.
:

isalpha
. :
CODE
>1. #include <iostream
>2. #include <ctype.h
;3. using namespace std
4.
)(5. int main
{ 6.
7.
;'char m='a

8.

;cin >> m

9.
10.

))if (isalpha(m

11.

; "cout << " Yes

;"else cout << "NOOOOOOO

12.
13.
14.

;cout << endl


;return 0

15.

16.


) ( m
12

. if

.
isupper : islower

)
(

:
.
.



.
for
@
:
CODE
>1. #include <iostream
>2. #include <ctype.h
;3. using namespace std
4.
)(5. int main
{ 6.
{);;(for

7.

;'char m='a

8.

;cin >> m

9.

;if (m=='@') break

10.

))else if (isupper(m

11.

;"else if(islower(m)) cout << "Small char

12.

;"else cout << "TRY AGINE

13.

; "cout << "Big char

14.
15.

;cout << endl


}
;return 0
}

16.
17.
18.

m if
.

10 @
for .
11
.
12
.
13
.

@
.

toupper : tolower


toupper tolower
.

.
. :
CODE
>1. #include <iostream
>2. #include <ctype.h
;3. using namespace std
4.
)(5. int main
{ 6.
{);;(for

7.

;'char m='a

8.

;cin >> m

9.
10.

;if (m=='@') break

11.

{ ))else if (isupper(m

12.

;"cout << "Big char\n" << "small char:\t

13.

;m=tolower(m); cout << m

14.

}
{ ))else if(islower(m

15.
16.

;"cout << "Small char\n" << "big char:\t

17.

;m=toupper(m); cout << m

18.

19.

;"else cout << "TRY AGINE

20.
21.

;cout << endl

22.

23.

;return 0

24.

25.

) (: strcat
cstring

.
.

.
CODE
>1. #include <iostream
>2. #include <cstring
;3. using namespace std
4.
)(5. int main
{ 6.
;" char word1[25]="Java and

7.

;"char word2[10]="C++

8.
9.

;)strcat(word1,word2

10.

;cout << "word1:\t" << word1 << endl

11.

"cout << "word2:\t

12.

;<< word2 << endl

;return 0

13.

14.

:
Java and C++

word1:

C++

word2:

10
strcat
.

.
:

stdio . iostream

putchar : getchar
putchar
.
:
CODE
>1. #include <iostream
;2. using namespace std
3.
)(4. int main
{ 5.
;)'putchar('a

6.

;)'putchar('\n

7.

;return 0

8.
} 9.

getchar
) ( for
. cin :
CODE
>1. #include <iostream
;2. using namespace std
3.
)(4. int main
{ 5.
;char x

6.

;)(x=getchar

7.

;)putchar(x

8.

;)'putchar('\n

9.
10.

;return 0
}

11.

getchar . 7
getchar
. Enter
) (: getch
conio.h
.



.
:
CODE
>1. #include <iostream
>2. #include <conio.h
;3. using namespace std
4.
)(5. int main
{ 6.
;char x

7.

;)(x=getch

8.

;return 0

9.

10.


.
:
.

.
.

*

.
for
.
CODE
>1. #include <iostream
>2. #include <conio.h
;3. using namespace std
4.
)(5. int main
{ 6.
;int sure=0

7.

;"char x[]="book

8.

;]char pass[4

9.

{);;(for

10.

{)for(int i=0;i<4;i++

11.

;)(pass[i]=getch

12.

;)'*'(putchar

13.

14.

{)for (i=0;i<4;i++

15.

;if (pass[i]==x[i]) sure++

16.

;else break

17.
}

18.

{)if (sure == 4

19.

;cout << "\n Password Correct"<< endl

20.

;break

21.
}

22.

;cout << endl

23.

;cout << "False....Try Againe" << endl

24.
}

;return 0

26.
}

25.

27.





.
10 for.
11 for
.
18 15

for
sure .
sure 4
19
for .

.

:
...


... .

:

CODE
// for pointer

>#include <iostream.h

) (int main

;int c=2

;cout << &c

;return 0

int 2
... c &c
.. &
c
...


2


c ...

..
c
int ...

2 2
... ..

2 6
:
CODE
// for pointer

>#include <iostream.h

) (int main

;int c=2

;cout << &c

c=4

;cout << &c

;return 0
}

9
10


stack heap ..


heap


....
stack

...
.heap




int
* pPointer
;

...

) (
... :

;int *pAge=x

x pAge
:
;pAge=&x
x
.. *pAge

;cout << *pAge

.
) (




:
CODE
>#include <iostream.h
) ( void main
{
;int p,g
;int *x
;p=5;g=7
cout << p << "\t" << g << "\n " << &p << "\t" << &g
;<< endl
;x=&p
;cout << *x << "\t" << x << endl
;x=&g
;cout << "\n\n" << *x << "\t" << x
}

p g x

)
( x
p

x p
g
.g

..
)
( ..


.

) (:
;*x=8
;cout << p
p 7 8
P
p x
8 x p
p
P
.g

** :


..
:
; int *x
x
..

x
:
1
// for pointer
2
>#include <iostream.h
3
) (void main
4
{
5
int *c
6
;*c=50
7
;cout << *c
8
}
:
// for pointer
>#include <iostream.h
) (void main
{
;int *c=new int
;*c=50
;cout << *c

1
2
3
4
5
6
7

8
}


c
50
c

new
int
.c

c
...

new new
int
int .float

** :


:
1
// for reference
2
>#include <iostream.h
3
) (void main
4
{
5
;int c
6
;c=50
7
;int &One=c
8
;cout << One
9
}
One
&
c

:
// for reference
>#include <iostream.h
) (void main
{
;int c
;c=50
;int &One=c
;cout << One << endl
;c=80
;cout << One << endl
;c=500

1
2
3
4
5
6
7
8
9
10
11

;cout << One<< endl


}

12
13

c
One c
One c
.One


.new
.
;)char &Refrence= *(new char
;'Refrence = 'x

:



:
1
;int x=5;int &refrence=x
2
;int y=6; refrence=y

x y
y
x x . 6
:
;)int &Refrence= *(new int
;Refrence =7
;Refrence =8



:
;int x=99
;&Refrence=x


.


..
..

...
delete
.....
:
;]float *number [100] [100

40

.
:
>#include <iostream.h
) ( main
{
;int *pPointer= new int
;*pPointer = 4
;delete pPointer
;return 0
}

1
2
3
4
5
6
7
8



..

...
..
..

... ..
.

&

&
*













**
:

:



.

heap
.


.

:

) (

.

:

) (
..

.
;]int Array [i] [j
i j .

... :
;]int *Array = new int [i
i .
..
Array
i
i=6 Array
:
1
>#include <iostream.h
2
) (void main
3
{

;int i
;cin >> i
;]int Array=new int [i
)for (int j=0;j<i; j++
;]cin >> Array[j
}

4
5
6
7
8
9



i Array
for .
.

:
;int **pArray

i :
;]int **pArray= new int *[i
pArray

i

:
)for (int k=0;k < i; k++
;]Array[k]= new int[j
..
.
>#include <iostream.h
) ( void main
{
;int i,j
;cin >> i >> j
; ]int **Array=new int *[i
)for (int k=0 ; k< i ; k++
;]Array[k]=new int[j
)for (k=0 ; k< i ; k++
)for (int kk=0; kk< j ; kk++
;]cin >> Array [k] [kk
}

1
2
3
4
5
6
7
8
9
10
11
12

void

. .

**
:

):(stray Pointer








) (


) (

...

.


..
.
const


:
;const int *pFirst
;int *const pSecond
;const int *const pThird

.
:
.
.

: void

void
void:
;void *pointer


.
:
;]int array[10

:
;]int *p= & array[0

pArray .

:
;)cout << *(p+1


p
.


Function
:


.
) ( main





math


main



) (
.
.
:
) ( main
:
CODE
)

( main

int
{

;statment1
;statment2
;statment3

;0

return
}

) ( main
:
return 0


.
CODE
>1. #include <iostream
;2. using namespace std
3.
)4. int max (int m,int g
{ 5.
;if (m>g)return m

6.

;else if (m<g)return g

7.

;else return g

8.
} 9.
10.

)(int main

11.

12.

;int num1,num2

13.

;cin>>num1

14.

;cin>>num2

15.

;)int max1=max(num1,num2

16.

;cout << max1 << endl

17.

;return 0

18.
}

19.

4 9 max
int
int:
:

:
;)int max (int x, int y

) (



.
) (
return
.

:


)( .

) ( main
16 max1
max
num1 num2
max 4 9
max1
max main
16 :
;)max (num1 , num2
num1 num2
4 4 :
;m = num1
;g = num2
.
:


.. int

:
-1 :
-2 :
-3 :
.
: Local Variables

}

{




.
: Global Variables


:
) (Void test1
{
;int g=1,k
}

)(void test2
{
;int b, g=2
}

test1 test2



g g

for while



.
:Static Variables




2

2
0

.
static
:
;static int number
:

:
CODE
>1. #include <iostream

;2. using namespace std


3.
)4. double binate (float b
{ 5.
;return b*b

6.
} 7.
8.

)(9. void main


{

10.

;float a

11.

;cin>> a

12.

;)double m=binate(a

13.

;cout << m << endl

14.
}

15.

) ( binate 4 7
b float
. main

.


:
CODE

1.

//

>2. #include <iostream


;3. using namespace std
4.
5. float *divides;//
;6. int times
7. //////////////////////////////
8. void HowTimes(int x); //
;)(9. void pointer
;)void TheMain(int x

10.

////////////////////////////

11.

void main() //

12.

13.

;int a

14.

;cin>> a

15.

;)TheMain(a

16.

)for(int i=0;i<times;i++

17.

;cout <<divides[i]<< endl

18.

"cout << "The Many Of How Number Divides Is:\t

19.

<<times

20.

;<<endl

21.
}

22.

///////////////////////

23.

) (void pointer

24.

25.
26.

;]divides=new float[times
}

27.

/////////////////////////

28.

)void HowTimes(int x

29.

30.
31.

)for (int i=1;i<=x;i++

32.

;if(x%i==0) ++times
}

33.

/////////////////////

34.

)TheMain(int x

35.

36.

;)HowTimes(x

37.

;)(pointer

38.

)for (int i=1,int j=0;j<times,i<=x;i++

39.
40.

{)if(x%i==0
;divides[j]=i

41.

};j++

42.
}

43.

.
:
:
: times .
:divides times
.divides

:
-1 ) : HowTimes(int x

.times
-2 ) ( : pointer divides

.
-3 ) : TheMain(int x

.divides

.
: Prototype
:
void HowTimes(int x); //

8.

;)(void pointer

9.

;)void TheMain(int x

10.


) ;(
:
-1



.
-2


int float
:
;)int test(int , float
-3
main
TheMain .35
.
:





.
:

:
-1
-2

.
) (


:
;)x, z

function (int

int


int
.

).( ,
: return Value
return
:
;)x,int z

function (int

int

int
int :
; )( 2

return


2
. int

:
;))return (function(4

function

. .

:

; )int number=function (4
; cout << number

function
:
; ) cout << function ( 4

function

.
:

. int

:
void
return
void
. void

): (::

:: :
;a=10

int

) (void function
} int a= 5

a function


function
function
::
: function
; cout << ::a


:: .
:


cin

) ( printf
.
)
(
) ( getline


.

100
100 .
)1. float rate(float a,float b ,float c=100
{ 2.
;3. float j=0
;4. j= (a*c)/b
;5. return j
} 6.


100
:
)rate ( 50 , 100

:
)rate ( 20, 100 , 1000


100

.




:
CODE
>1. #include <iostream
;2. using namespace std
3.
4.
;)5. float rate (float a,float b,float c=100
6.
)(7. void main

{ 8.
;float i,j,k,avg

9.

;"cout << "Please Enter the number?\n

10.

;cin >> i

11.

;"cout << "from:\t

12.

;cin >> j

13.

;"cout << "the Avrege:

14.

;cin >> avg

15.
16.

;)k=rate (i ,j,avg

17.

;cout << endl << k << endl

18.
19.
}

20.
21.

)float rate(float a,float b ,float c

22.

23.

;float j=0

24.

;j= (a*c)/b

25.

;return j

26.
}

27.

22 5

.
:

void :
;)return (Value
value .


.

main
plus main
. plus
>#include < iostream.h
)void plus (int num1,int num2,int *plus1,int *plus2
{
;*plus1=num1 + num2

1
2
3
4

;*plus2=num1*num2
}
) ( void mian
{
;int num1,num2,plus1,plus2
;)plus (num1,num2, &plus1 , & plus2
;cout << plus1 << endl
;cout << plus2 << endl
}

5
6
7
8
9
10
11
12
13
14

plus1 plus2 plus1


plus2
)( main plus plus
void
num1 num2
plus

) (

... plus

int ..
..

..

)( main
.... .




...
...
:
1
>#include < iostream.h
2
)void plus (int num1,int num2,int &plus1,int &plus2
3
{
4
;plus1=num1 + num2
5
;plus2=num1*num2
6
}
7
8
) ( void mian
9
{
10
;int num1,num2,plus1,plus2
11
;)plus (num1,num2, plus1 , plus2
12
;cout << plus1 << endl

;cout << plus2 << endl


}

13
14


plus
.



C++
..

:

...

...

.
:

.

.


)
(
.


:
;) ][ void arraysFunction (int



) ( main

:
CODE
>1. #include <iostream
;2. using namespace std
3.

;) ][ 4. void arraysf (int


5.
)(6. int main
{ 7.
;}int array[5]={1,2,3,4,5

8.

)for (int i=0;i<5;i++

9.
10.

;cout << array[i] << endl


;) arraysf(array

11.

)for ( i=0;i<5;i++

12.
13.

;cout << array[i] << endl

14.
;return 0

15.

16.
17.

)][void arraysf(int m

18.

19.
20.

)for (int i=0,int j=5;i<5;i++,j--

21.

;m[i]= j
}

22.

arraysf
.
8
1 . 5
9 10 .
11 arraysf void


.
20 for

. void
) ( main 12 13
:
1
2
3
4
5
5
4

3
2
1

:

) (


:
;) ] [ 6

] [ arrayFunction (int

void




.
:




for .
:
)(void Function
{
;) (Function
}


) ( main


.


for


for
) :( for
CODE
>1. #include <iostream
;2. using namespace std
3.

;)4. void function (int x


5.
)(6. int main
{ 7.
;int n=0

8.

; "cout << "Enter The Number:\t

9.

;cin >> n

10.

;)function (n

11.
12.
13.

;return 0
}

14.
15.

) void function (int x

16.

17.

;static int i=0

18.

;i++

19.

;;cout << "Number i=\t" << i << endl

20.

)if (i==x

21.
22.

; return

23.

;)function(x
}

24.


)
( :
) ( main
.10
11 function
.
18
) 0 .( for
19 ) i
.( for
20 )
( .
21
) ( main
return
) ( break .

.



for
.

)
(

.
:

.

.
:
;2! = 2 * 1
; 5! = 5 * 4 * 3 * 2 * 1

) . ( 0! = 1
0 .


:
CODE
>1. #include <iostream
;2. using namespace std
3.
;) 4. int fact(int
5.
)(6. int main
{ 7.
;int i=0

8.

;"cout << "Enter the Number:\t

9.
10.

;cin >> i

11.
;)int x=fact (i

12.

;cout << x << endl

13.
14.
15.

;return 0
}

16.
17.

)int fact (int x

18.

19.

;if (x==0) return 1

20.

;)else return x*fact(x-1

21.
}

22.


.10
x
fact
.
20
0 1
.

5
) ! ( 5 * 4
4 fact
0
1 .


.

. fibbianci
:
1
:
1
1
2
3
5
8
13 21
34
55 etc

CODE
>1. #include <iostream
;2. using namespace std
3.
;) 4. int fib(int
5.
)(6. int main
{ 7.
8.
;int i=0

9.

;"cout << "Enter the Number:\t

10.

;cin >> i

11.
12.

;endl

;)i=fib (i

13.

<< cout << i

14.
15.
16.

;return 0
}

17.
18.

)int fib (int x

19.

20.
21.

)if ( x<3

22.

;return 1

23.

;) )else return (fib (x-2) + fib (x-1

24.
}

25.

.
:


)
(.


.


.



Find


int char ... float
find find
:
;) int find (int [] , int
;)char find (char [] , char
;) float find (float [] , float



.
Abs
C stdio
: C
CODE
1. #include <iostream>
2. using namespace std;
3.
4. int Abs (int );
5. float Abs(float );
6. double Abs (double );
7.
8. int main()
9. {
10.

int Int=0;

11.

float Float=0;

12.

double Double=0;

13.
14.

cout << "Int:\t" ; cin >> Int;

15.

cout << "Float:\t"; cin >> Float;

16.

cout << "Double:\t";cin >> Double;

17.

cout << endl << endl;

18.
19.

cout << "Int:\t" << Abs(Int) << endl;

20.

cout << "Float:\t" << Abs (Float) << endl;

21.

cout << "Double:\t" << Abs(Double) << endl;

22.

cout << endl;

23.
24.

return 0;

25.

26.

int Abs(int X)

27.

28.
29.

return X<0 ? -X : X;
}

30.
31.

float Abs(float X)

32.

33.

;return X<0 ? -X : X
}

34.
35.

)double Abs (double X

36.

37.
38.

;return X<0 ? -X :X
}

39.

) ( Abs

3 5 .6
10 11 13
int float double


14 15 16
.
19 int
) ( int Abs

. 26
28 )
(
-2

- - 2
2

) ( main . 19
20 . 21



.
:

:
-1
:
;) int Abs(int , int
;) float Abs( int , int

-2
:
;)int function(int a ,int b
;)int function(int a,int b ,int c=100


.
-3 :
;)int function(int a
;)int function(const int a



.
: Inline Function




.







.



.

)
(
.
inline
:
; ) (inline int function

:



.




:
)int Abs(int X
{
;return X<0 ? -X : X
}

int
)
(



.


:
CODE
)1- template <class T> T Abs(T X
{ 2-
;return X<0 ? -X : X

3} 4-

)int Abs (int X


:
)template <class T> T Abs(T X

int
T T
T

template
><class T

T class

T

int int
. char

:



.

:

) ( Abs





.
:





.

:
)template <class T> T Big(T n1,T n2
{
;return n1 >n2 ? n1 : n2
}



1 0.5
int float



:
CODE
)1- template <class T1,class T2> T1 Big(T1 n1,T2 n2
{ 2-
;return n1 >n2 ? n1 : n2

3} 4-

class
T1
T2
.
:
T2
T2
.




.
:

) ( Abs
char
.
) (:
.

:
>#include <iostream

:
:
) (. Abs
.
:Header File

.
File
New Files C/C++ header file
. OK Abs
.
CODE
1- #ifndef AbsModule
2- #define AbsModule

;) 3- int Abs(int
;) 4- float Abs( float
;) 5- double Abs (double

6- #endif

) (

AbsModule
endif .


:
" #include "AbsModule.h


:
" #include "AbsModule.h

Abs

.
2
. AbsModule
AbsModule
. AbsModule
3
4 5
.
1 2 6



.
:

: AbsModule
CODE
>1. #include <iostream
"2. #include "AbsModule.h

;3. using namespace std


4.
5.
)6. int Abs( int X
{ 7.
8.

;return X<0 ? -X : X

} 9.
10.
)float Abs( float X

11.

12.
13.

;return X<0 ? -X : X

14.

15.
)double Abs ( double X

16.

17.
18.

;return X<0 ? -X :X

19.

iostream
std . 3
AbsModule

) " " (



). (.h
6 19
. AbsModule
main.cpp
:
CODE
>1. #include <iostream
"2. #include "AbsModule.h
;3. using namespace std
4.
)(5. int main
{ 6.
;int Int=0

7.

;float Float=0

8.

;double Double=0

9.
10.

;cout << "Int:\t" ; cin >> Int

11.

;cout << "Float:\t"; cin >> Float

12.

;cout << "Double:\t";cin >> Double

13.

;cout << endl << endl

14.
15.

;cout << "Int:\t" << Abs(Int) << endl

16.

;cout << "Float:\t" << Abs (Float) << endl

17.

;cout << "Double:\t" << Abs(Double) << endl

18.

;cout << endl

19.
20.
21.

;return 0
}

22.



. 2
:

.
....
.



:
)int (*function) (int , int



.


.
CODE
>1. #include <iostream
;2. using namespace std
3.
;) 4. double plus(double , double
;) 5. double del(double , double
;) 6. double multipy(double ,double

7. double divide(double ,double);


8.
9. int main()
10.

11.

double Num1,Num2,Value;

12.

char Operator;

13.

double (*Function) (double ,double );

14.
15.

cout << "Please Enter Num1:

";cin>>Num1;

16.

cout << "Please Enter Operator

17.

cout << "Please Enter Num2:

";cin>>Operator;

";cin >>Num2;

18.
19.

switch (Operator) {

20.

case '+': Function=plus;break;

21.

case '-': Function=del;break;

22.

case '*': Function=multipy;break;

23.

case '/': Function=divide;break;

24.

default: cout << "\nBad Command\n";return 0;

25.

26.

Value = Function (Num1,Num2);

27.
28.

cout << "Tne Value is:

" << Value << endl;

29.
30.
31.

return 0;
}

32.
33.

double plus (double a,double b)

34.

35.

return a+b;

36.

37.

double del(double a, double b)

38.

39.

return a-b;

40.

41.

double multipy(double a, double b)

42.

43.
44.

return a*b;
}

)double divide(double a,double b

45.

46.
47.

;return a/b
}

48.

15
.
13 .
switch 20 Operator
Function
. 7-4
switch
.
:

:
;)int (*Function) (int ,int

:
; )int* Function (int , int

. int

: Storage Classes

.


.
:
-1 : Automatic Variables
-2 : External Variables
-3 : Static Variables
: Automatic Variables
main
.

:
; auto int Variables

.

. auto

: Lifetime of automatic Variables



A Functio
A Functio
A .
A . Lifetime
: Visibility

.

.

.




:
;extern int Num

num int extern



Num Num

.
stack
. heap


Stack


heap



heap

:
int flaot .... double .void
void

.



.
:

.
static


0 .NULL


.


Introduction to Object Oriented
Programming
:


...

..



-

.

:


..
.

.. ..


.

:



....

..

..


...

:


.
) (
: Attributes . Behaviors
:
.

:


) (





)
( )
(




:


)
(

:

:
:




:

.

:





:






..

.

) ( :Creating a Class Instance

..


..
.


.
. Object
:
CODE
;int x=5

x int

int
. x .

.

.

** :
:
-1 :Abstraction
-2 : Inheritance
-3 :



Encapsulation

:Encapsulation



) (







..

... ..
public
private

.
) (
.




.

:
CODE
class

class Student //

public: //

;) (choosingCollege

;) (SignUp

private: //

;int itsGrade

;int itsAge

;] [char specialization

; }

10


class
student




.

. .
.


.
CODE
>#include <iostream.h

class maths

private:

;float itsNum1

;float itsNum2

6
7

public:

;)GetNum1Num2(float i,float j

;)(print

10

;}

11

)maths::GetNum1Num2(float i,float j

12

13

;itsNum1=i

14

;itsNum2=j

15

16

)(maths::print

17

18

;cout << "add:\n" << itsNum1+itsNum2 << endl

19

;cout << "subtract:\n" << itsNum1-itsNum2 << endl

20

;cout << "multiby:\n" << itsNum1*itsNum2 << endl

21

;cout << "divide:\n" << itsNum1/itsNum2 << endl

22

23
24

) ( int main

25

26

;float i,j

27

;cin >> i>>j

28

;maths a

29

;)a.GetNum1Num2(i,j

30

;)(a.print

31

;return 0

32

33

maths

.



.

)
( :
CODE

)(maths::print

17


.
.

.



.
CODE
;)a.GetNum1Num2(i,j

30


.
CODE
>#include <iostream.h

1
2

class First //

int d1; //

int d2; //

int counterd1; //

int counterd2; //

int **pArray; //

public:

10

Enter ( int s1,int s2); //

11

putArray();//

12

Loop( );//

13

print();//

14

;}

15

)First::Enter(int s1,int s2

16

17

;d1=s1

18

;d2=s2

19

20

) (First::putArray

21

22

;]pArray= new int *[d1

23

)for (counterd1=0 ; counterd1<d1;counterd1++

24

;]pArray[counterd1]= new int [d2

25

26

)(First::Loop

27

28

)for (counterd1=0;counterd1<d1;counterd1++

29

)for (counterd2=0;counterd2<d2;counterd2++

30

; ]cin >> pArray[counterd1][counterd2

31
32

33

)(First::print

34

35

)for (counterd1=0;counterd1<d1;counterd1++

36

)for (counterd2=0;counterd2<d2;counterd2++

37

;cout << pArray[counterd1][counterd2] << endl

38

39
40

) ( int main

41

42

;First a

43

;int i,j

44

;cin >> i

45

;cin >> j

46

a.Enter(i,j);//

47

;)(a.putArray

48

;)(a.Loop

49

;)(a.print

50

51

:
.








.


.


) (
Get
itsAge
:
CODE
} ;GetitsAge() { return itsAge




)
( .
set

CODE
} ;SetitsAge(int x) { itsAge=x

Get
Set Set
.




.




.
.


CODE
>#include <iostream.h

class maths

private:

;float itsNum1

;float itsNum2

6
7

public:

;)maths(float i,float j

;)(print

10

;}

11

)maths::maths(float i,float j

12

13

;itsNum1=i

14

;itsNum2=j

15

16

)(maths::print

17

18

;cout << "add:\n" << itsNum1+itsNum2 << endl

19

;cout << "subtract:\n" << itsNum1-itsNum2 << endl

20

;cout << "multiby:\n" << itsNum1*itsNum2 << endl

21

;cout << "divide:\n" << itsNum1/itsNum2 << endl

22

23
24

) ( int main

25

26

;float i,j

27

;cin >> i>>j

28

;)maths a(i,j

29

;)(a.print

30

};return 0

32

16-12
29

..
void
) (


CODE

;) i , j

maths

29


.
.
:
Constructor .. void
.

:



.
:
CODE
>#include <iostream.h
class First
{
public:
First(){cout <<"...class First has built"<< endl; }//
~First(){cout <<" class First has die";}//
;}
)(void main
{
;First m
}



first
.
:
Destructor )~( ..
void .

:

.
)( main


.

:

) (inline . inline
:
inline function
non-inline function

inline .

:this

C++

)
( .
this

.
Test a b

a
b this

.
this
this
.
this :
CODE
>1. #include <iostream
;2. using namespace std
3.
{4. class stud
5. public:
;void address(){cout << this

6.

7.
;} 8.
9.

)(int main

10.

11.

;stud a,b,c

12.

; "cout << "The address of a\t

13.

; )(a.address

14.

; "cout << endl << "The address of b\t

15.

; )(b.address

16.

; "cout << endl << "The address of c\t

17.

; )(c.address

18.

;cout << endl

19.

20.
21.

;return 0
}

22.

stud
this
address
.

:Static Members

) (Test1
) (i ..
i



Arrays A
100
Array 100 A A

.


:
CODE
>#include <iostream.h

class First

public:

static int counter;//.

) (First

};counter++

};getCounter() {return counter

;}

10

int First::counter=0;//

11

)(void main

12

13

;First a

14

;First b

15

;]First c[60

16

cout << a.getCounter();//

17

18

First
62 . counter

17 getCounter
) a ( c counter
62 .




) ( )
11 (
.

.

:Static Members Functions





.
CODE
>#include <iostream.h

class First

3
4

;static int counter

public:

};static getCounter() {return counter

) (First

};counter++

10
11

;}

12
13

;int First::counter=15

14

)(void main

15

16
17

;cout << First::getCounter()<< endl


}

18

) ( getCounter

First 7
.
:



) ( get ) ( set




) (

.
Data
Student Data
Student Student
Data


:
CODE
>#include <iostream.h

1
2

class Data

;double itsAvreg

5
6

public:

};getItsAvreg(){return itsAvreg

};setItsAvreg(double x){itsAvreg=x

9
10
;}

11

12
class Student

13

14
15

;Data itsData
public:

16

};)(getItsAvreg(){ return itsData.getItsAvreg

17

};)setItsAvreg(double x){itsData.setItsAvreg(x

18
;}

19
20

)(int main

21

22

;Student a

23

;)a.setItsAvreg(98

24

;)(cout << a.getItsAvreg

25
}

26



.
Smaltalk:
smaltalk

.
smalltalk
:
-1 :
-2
:
-3 .
-4 ) (.
-5 .


.
:



) (




) (
set . get
.
.

) (

.








...




.
:








.











250
10 !!!!


.






.

:: ::
/1
..
.
:
:
: .
: .
.
.
:
CODE
>#include <iostream.h

class Array
{
;float **arrays
;char choice
;int itsD1
;int itsD2
;int itsD1Count
;int itsD2Count
public:
;)Array(int ,int
;)(~Array
;)(Bigfunction
;)(print

Enter();
};

Array::Array(int i,int j)
{
itsD1=i;itsD2=j;
arrays=new float*[itsD1];
for (itsD2Count=0;itsD2Count<itsD2;itsD2Count++)
arrays[itsD2Count]=new float [itsD2];

}
Array::~Array()
{
delete [] arrays;
}
Array::Enter()
{
cout << "Enter the memeber of Array" << endl;
for ( itsD1Count=0;itsD1Count<itsD1;itsD1Count++)
5

for (itsD2Count=0;itsD2Count<itsD2;itsD2Count++)

cout <<"Enter the member:\t" << endl;

cin >> arrays[itsD1Count][itsD2Count];

}
}
Array::Bigfunction()
{
if (itsD1==itsD2)
{

cout << "Do you want to make the main Rayon

cin >> choice ;

Zero[y/n]";

if (choice=='y')

12

for ( itsD1Count=0;itsD1Count<itsD1;itsD1Count++)

12

arrays[itsD1Count][itsD1Count]=0;

12

print();

else

12

print();

else
;)(print
}
)(Array::print
{
;cout << endl
)for ( itsD1Count=0;itsD1Count<itsD1;itsD1Count++
{
)for (itsD2Count=0;itsD2Count<itsD2;itsD2Count++
{

12

;]cout << arrays[itsD1Count][itsD2Count

;"cout << "\t

;cout << endl

)(int main
{
;int x,y
;cout << "enter d1:\t ";cin >> x
;cout <<"enter d1:\t ";cin >>y
;)Array One(x,y
;)(One.Enter
;)(One.Bigfunction
;return 0
}

/2
) (
) (p .
:
:
.

12

4000 .
1 .
:
CODE
>#include <iostream.h

1
2

class notepad //

int index1,index2;//

char One[200][200];//

int charactor;//

public:

notepad ();//

HowMany();//

10

display();//

11
12

; }

13

) ( notepad:: notepad

14

15

;charactor=0

16

17

)(notepad::HowMany

18

19

;"cout <<"\n

20

) for (index1=0 ;index1<200;index1++

21

22

for ( index2=0;index2<256;index2++

23

24

;]cin >> One[index1][index2

25

1 if (One[index1][index2]=='1')//

26

//

27
;return 0

28

1 else charactor++;//

29

30

31

32

)(notepad::display

33

34

;cout << "The number of char you made it is\t" << charactor << endl

35

36
cout <<"\a" ;//

37

38
39

)(void main

40

41

;notepad first

42

;)(first.HowMany

43

;)( first.display

44

45

/3
fibancci


fibancci

.

:
:
-

Fibancci
.

.
first second third max
max
.
fibancci

:
1 1
2
3
5
8
13
21
34
55

time


.
CODE
>1. #include <iostream.h

2. class fibancci

3. {
4. /*

* /

5. int first,second,third;
6. int *array,max,times;
/*

7. SetTimes()

* /

8. {
9. for (times=0;second<max;times++)
10.

11.

third=second+first;

12.

first=second;

13.

second=third;

14.

15.

third=second=first=1;

16.

17.

public:

18.

fibancci():times(1),first(1),second(1),max(50){Array();}

19.

fibancci(double x):times(1),first(1),second(1),max(x)

/*

* /

{Array();}
* /

20.

/*

21.

GetTimes()

22.

{ return times;}

23.

Array()

24.

/*

* /

25.

SetTimes();

26.

array=new int[times];

27.

for(int i=0;second<max;i++)

28.

29.

array[i]=first;

30.

third=second+first;

31.

first=second;

32.

second=third;

33.
34.

35.

array[i+1]=first;

36.

cout << endl;

37.

38.

printfibancci()

39.

{cout << endl;

/* * /

)for (int i=0;i<times;i++

40.

;"cout << array[i] << "\t

41.

;cout <<"\t" << array[i+1] << endl

42.

43.
44.
;}

45.
46.

)(void main

47.

48.

;double j
;cin >> j

49.

;)fibancci a(j

50.

;)(a.printfibancci

51.

;)(a.GetTimes

52.
}

53.



) (
4 :
19

first second 1 max
50
max .
) (. Array
) ( Array ) (. SetTimes
times



for 9

for
for
)
( 15
for

) ( setTimes ) ( Array
.

26 array
27

35



.


Make your own Data Types
:

..

int float

+

...
) (
)
(
.

.

** :
)
( .

.

.
) (
) (
.
) (
)(

)
( .
.
!.
:
CODE
>#include <iostream.h

1
2

plus (int x,int m)//

;return x+m

6
7

)plus (long x,long z

;return x+z

10

11
12

)(main

13

14

;int a=10,b=20,c=0

15

;long d=30,e=40,f=0

16
17

;)c=plus (a,b

18

;)f=plus(d,e

19

;cout << c << endl << f

20

;)//f=plus (a,d

21

22
;return 0

23

24

) ( plus
int
long . 18
19
) ( plus
21
plus .

) (.
.

** :



:
CODE
>#include <iostream.h

1
2

)(main

;float m=10;long d=50

;)int j(m

6
7

;cout << "The Value of j is: " <<j <<endl

8
9

;)int dd(d

10

;cout << "The Value of dd is: " <<dd <<endl

11

;return 0

12

13


int
j m float
) dd (int d
long int

float long

int
int
;) ( getFloat
float
:
CODE
;int j

;)j.getFloat(m

int
.
. .

.
.


.

. . num
int float
.long

CODE
class num
{
;double itsNum
public:
};num(int x){itsNum=x
};num(float x){itsNum=x
};num (long x){itsNum=x
};GetItsNum() const { return itsNum
;}

)( main :
CODE
)(void main
{
;int i=12;float g=13;long k=15
;)num first(i),second(g),third(k
;

cout << first.GetItsNum() << endl


)(cout << second.GetItsNum

; << endl
;

)(cout << third.GetItsNum


}

:
CODE
)(void main
{
;int i=12;float g=13;long k=15
;num first=i,second=g,third=k
;

cout << first.GetItsNum() << endl

; << endl
;

)(cout << second.GetItsNum


)(cout << third.GetItsNum
}




.

Num .
.


:
CODE
class num
{
;double *itsNum
public:
};num(int x){itsNum=new double;*itsNum=x
};num(float x){itsNum=new double;*itsNum=x
};num (long x){itsNum=new double;*itsNum=x
};~num () { delete itsNum
};GetItsNum() const { return *itsNum
;}



CODE

)(void main

;int i=12

;num first=i,second=first

cout << first.GetItsNum() << endl

)(cout << second.GetItsNum

; << endl

7
;cout << first.itsNum << endl

;cout << second.itsNum

10

second first
) itsNum
itsNum (
first second
.
second first
.

** :
:
.




.

)
( .
Test Test1 Test2
:
;)Test2(Test1
Test2


. Test
.*N

) (Test1 ) (Test2 *N
Test1 Test2
.

N .
:
:
.

.
CODE
)num::num(const num &rhs

;itsNum=new double

;)(*itsNum=rhs.GetItsNum


.

. rhs
.
itsNum .
itsNum .

.
CODE
>#include <iostream.h

2
3

class num

public:

double *itsNum;

public:

num(int x){itsNum=new double;*itsNum=x;}

num(float x){itsNum=new double;*itsNum=x;}

10

num (long x){itsNum=new double;*itsNum=x;}

11

~num () { delete itsNum;}

12

num(const num &rhs);

13

GetItsNum() const { return *itsNum;}

14

};

15

num::num(const num &rhs)

16

17

itsNum=new double;

18

*itsNum=rhs.GetItsNum();

19

20

void main()

21

22

int i=12;

23

num first=i,

24

second=first;

25

cout << first.GetItsNum() << endl

26

cout << second.GetItsNum()

<< endl ;

27
28

cout << first.itsNum << endl;

29

cout << second.itsNum;

30

itsNum
.2928
.24
:
24

second=first;

24

second(first);

:

15 second

.first first
. 17 itsAge
) itsAge
(first
. itsAge
18 :
;)(*itsNum=rhs.GetItsNum

18

;)(*itsNum=first.GetItsNum

18

18 :
itsAge
first .

28 29 .

:
num
int float long
.
:
24

;second++

second .num

) (++ num
) ( ) (++
.num
++

.
.

:num
) (++ . num


) ( Increament :
)(num::Increament
} ; { return *itsNum++

1
2

:
;num first=4

;)(first.increament

..
:
;First++


:
-1 : ++ . - -
-2 : + * . /
.++
:

:
.
num
.
.
CODE
>#include <iostream.h

1
2

class num

4
5

;double *itsNum

public:

};num() {itsNum=new double ;itsNum=0

};num(int x){itsNum=new double;*itsNum=x

};num(float x){itsNum=new double;*itsNum=x

10

};num (long x){itsNum=new double;*itsNum=x

11

};~num () { delete itsNum

12

};void setItsNum(double x) {itsNum=&x

13

;)num(const num &rhs

14

};GetItsNum() const { return *itsNum

15

;)( num operator ++

16

;}

17
18

)num::num(const num &rhs

19

20

;itsNum=new double

21

;)(*itsNum=rhs.GetItsNum

22

23

)( num num::operator ++

24

25

;)++(*itsNum

26

;double x=*itsNum

27

;num temp

28

;)temp.setItsNum(x

29

;return temp

30

31

)(void main

32

33

;int i=12

34

;num first=i

35

;++first

36

cout << first.GetItsNum() << endl

37

;num second= ++first

38

cout << second.GetItsNum() << endl

39

40

)(
.
num . . :
(num) :
:
39

;num first

(setItsNum) :
) (
) ( . .
) ( .
(operator ++) :
. 36 38
.
)( num num::operator ++

;)++(*itsNum

;double x=*itsNum

;num temp

;)temp.setItsNum(x

;return temp

) (
.
)
void
:
) ( ( operato++r 38 .

) itsNum
( x x
setItsNum ) temp
( .
setItsNum
itsNum
) . (temp operator ++ temp
.
.
.
main .
)(void main

;int i=12

;num first=i

;++first

cout << first.GetItsNum() << endl

;num second= ++first

cout << second.GetItsNum() << endl


}

8
9

. operator ++
. first operator ++
.
:
;)num second(++first

;) ) (num second= (first.operator++

:
operator ++ first

temp
second .
. num
.
++ .

.

.

:this

this .
.
) ( operator ++ :
)( num num::operator ++

;)++(*itsNum

;return *this

num .
).(- -

:
num
38 :
38

;num second= first++

.
first second
first second . first
(operator ++
) itsAge
first
itsAge first
.second :
)m

num::num operator ++ (int

;)num temp(*this

;++itsNum

;return temp

itsNum
.
.

.
:
CODE
>#include <iostream.h

1
2

class num

;double *itsNum

public:

num() {itsNum=new double ;itsNum=0;}

num(int x){itsNum=new double;*itsNum=x;}

num(float x){itsNum=new double;*itsNum=x;}

10

num (long x){itsNum=new double;*itsNum=x;}

11
12

num (double x){itsNum=new double;*itsNum=x;}

13

~num () { delete itsNum;}

14

void setItsNum(double x) {itsNum=&x;}

15

num(const num &rhs);

16

double GetItsNum() const { return *itsNum;}

17

num operator ++ ();

18

num operator ++ (int m);

19

};

20
21

num::num(const num &rhs)

22

23

itsNum=new double;

24

*itsNum=rhs.GetItsNum();

25

26

num num::operator ++ ()

27

28

++(*itsNum);

29

return *this;

30
31
32
33

}
num num::operator++ (int m)
{

34
35
36

return num((*itsNum)++);
}

37
38

void main()

39

40

int i=12;

41

num first=i;

42

first++;

43

cout << "first++ :\t" << first.GetItsNum() << endl

44

num second= first++;

" << first.GetItsNum() << endl


;

\t" << second.GetItsNum() << endl

cout << "first++

45

cout << "second

46
}

47


this
32 .36
:
;)return num((*itsNum)++

35


num
) ( main
:
)return int (0


35 :
;)num temp (*itsNum

;)++(*itsNum

;return temp


) temp (
itsNum temp
12
double temp
. itsNum .
.
:
;num second= first++

44

.
.
. ) ( - -
.

:

.
num :
num One,Two=2,Three=3

;One= Two+Three

):(+
num . +
operator +
:

)num num::operator+ (const num &rhs

;))(return num (*itsNum+rhs.GetItsNum

main :
num One,Two=2,Three=3

;One= Two+Three

num
.
:
2

;)One=Two.operator+(const num &three

num


. + .
.
) ( +

.
.

.
num itsNum
:
}

13

{ )( ~num

.
num
itsNum


num
1. class num
{

2.
3.

;int itsNum

4.

public:

5.

};num() {itsNum=0

6.

};num(int x){itsNum=x

7.

};num(float x){itsNum=x

8.

9.

num (long x){itsNum=x;}

10.

num (double x){itsNum=x;}

11.

~num () { }

12.

void setItsNum(int x) {itsNum=x;}

13.

num(const num &rhs);

14.

double GetItsNum() const { return itsNum;}

15.

const num &operator ++ ();

16.

const num operator ++ (int m);

17.

num operator+ (const num &rhs);

18.
19.

};

20.
21.

num num::operator+ (const num &rhs)

22.

23.

return num

24.

((itsNum)+rhs.GetItsNum());

25.
26.
27.

num::num(const num &rhs)

28.

29.
30.

itsNum=rhs.GetItsNum();

31.

32.

const num& num::operator ++ ()

33.

34.

++itsNum;

35.
36.
37.

return *this;

38.

39.

const num num::operator++ (int m)

40.

41.

num temp(*this);

42.

++itsNum;

43.

return temp;

44.

( +)
.

:
1

;second=i+first

i int .
+
num itsNum
num
:
)num operator+ (double x,const num &rhs

;))(return num (x+rhs.GetItsNum

:
.num
+

.
) = (.

)=(:
)=(.

. num

= num


num


) (

.
.
CODE
>1. #include <iostream.h
2.
3. class Bird
{ 4.
;int *itsAge

5.

6. public:

};)Bird () {itsAge=new int(2

7.

};GetitsAge()const { return *itsAge

8.

};SetitsAge(int age) {*itsAge=age

9.
10.

)Bird & operator = (const Bird &rhs

11.

12.

;if (&rhs == this) return *this

13.

;*itsAge=rhs.GetitsAge();return *this

14.
15.

}
;}

16.
17.

)(void main

18.

19.

;Bird a,b

20.

;)a.SetitsAge(6

21.

;b=a

22.

=cout << "b

23.

;)(" << b.GetitsAge

24.

.
)=(
)Bird & operator = (const Bird &rhs

25.

bird
) (: main
;b=a

26.

;)b.operator =(a

27.

a b
operator a
:
{

28.

;if (&a == this) return *this

29.

;*itsAge=a.GetitsAge();return *this

30.

} //

31.

29
) b this . (b

itsAge b itsAge

a .
.b
) =(
.

:

.

int float . num
:
};num() {itsNum=0

45.

};num(int x){itsNum=x

46.

};num(float x){itsNum=x

47.

};num (long x){itsNum=x

48.

.num

num :
};num() {itsNum=new double ;itsNum=0

};num(int x){itsNum=new double;*itsNum=x

};num(float x){itsNum=new double;*itsNum=x

};num (long x){itsNum=new double;*itsNum=x

10
11
12

};num (double x){itsNum=new double;*itsNum=x

.
num int
.
int num
num :
>1. #include <iostream.h
class num

2.

3.

;int itsNum

4.

public:

5.

};num() {itsNum=0

6.

};num(int x){itsNum=x

7.

} { )( ~num

8.

};void setItsNum(int x) {itsNum=x

9.

;)num(const num &rhs

10.

};int GetItsNum() const { return itsNum

11.

;)(operator int

12.

;}

13.

)num::num(const num &rhs

14.

15.

;)(itsNum=rhs.GetItsNum

16.

17.
18.

)(num::operator int

**

**

;) )return (int (itsNum

**

**
19.

)(void main

;
;

20.

21.

;int i=12

22.

;num first=i

23.

;int j=first

24.

cout <<"j:

25.

cout <<"first:

26.

27.

;int j=first

28.

;)(int j=first.operator int

29.

"<< j << endl

"<< first.GetItsNum() << endl

24 :
:
) ( operator int first
. j
24 :
)(num::operator int

;) )return (int (itsNum


int itsNum . first
float : long
. double

.

) (
.


.

:

.

.

:
)(.

.
:
: .
** .
.
.
.
) (:



100
200



) ( :
CODE
>1. #include <iostream
;2. using namespace std

3.
4. class array
{ 5.
;int number

6.

;int *arrays

7.

8. public:
)(array
{

9.
10.

;int i=0,j=0

11.

;number=100

12.

;]arrays=new int[number

13.

)for(j=0, i=0;i<100;i++,j=10*i

14.

;arrays[i]=j

15.
}

16.

)int operator() (int x

17.

18.
19.

;if(x>number) return 0

20.

;]else return arrays[x

21.

22.
23.
;}

24.
25.

)(int main

26.

27.
28.

;array a

29.
30.

)for(int i=0;i<10;i++

31.

;cout << a(i) << endl

32.
33.

;return 0
}

34.
35.

4 array
.
17 ) (

100 0
.

Fraction
/




.

:
:
-

Fraction
.
a/b a b
int

.
num float


a
.b
a/b


a0b :
.
+ : * /


:

CODE
>1. # include <iostream.h

2. class Fraction
*/

*3. { /
;4. int up
;5. int down
;6. float num

7. public:

8. /*

*/

9. GetUp(){return up;}
10.

GetDown(){return down;}

11.

GetNum(){retur num;}

12.

SetUp(int x) {up=x;}

13.

SetDown(int x){down=x;}

14.

/*

15.

Fraction():up(1),down(1),num(1){}

16.

Fraction(int a):up(a),down(1),num(a){}

17.

Fraction(int a,int b):up(a),down(b),num(a/b){}

18.

/*

19.

Fraction operator+ (int rhs)

20.

*/

/*

return Fraction (up+rhs,down);


21.

22.

Fraction operator* ( Fraction rhs)

23.

{
return Fraction (up* rhs.GetUp() , down* rhs.GetDown()) ;

24.

}
Fraction operator/ ( Fraction rhs)// 1/2

25.

2/1

{
int m;
m=rhs.GetUp();
rhs.up=rhs.GetDown();
rhs.down=m;
Fraction temp(up* rhs.GetUp() , down* rhs.GetDown());
return temp;

26.

27.

friend Fraction operator+ (int ,Fraction&);

28.

friend ostream &operator << (ostream& ,const Fraction &);

29.

friend istream &operator >> (istream& , Fraction &);

30.

};

31.

Fraction operator+ (int rhs,Fraction &temp)

32.

33.

return Fraction (rhs+temp.up, temp.down);

34.

*/

*/

35.

)istream& operator >> (istream& E, Fraction& temp

36.

37.

;E >> temp.up

38.

;char c

39.

;)E.get(c

40.

;if (c !='/') throw

41.

;E >> temp.down

42.

;return E

43.

44.

)ostream &operator << (ostream& D ,const Fraction &temp

45.

46.

; return D << temp.up << "/" << temp.down

47.

48.

:
:
:
:
.
.
.
.
.
:
:
) :set( int
) num
(
) . SetNum(int
) ( : Get


) (. GetNum
:

.
.1


1
.

.



.
.
:
19 28

.
;)Fraction operator* (Fraction rhs

:
; ))(return Fraction (up* rhs.GetUp() , down* rhs.GetDown



*
:
;Fraction temp
;)Temp (up* rhs.GetUp() , down * rhs.Getdown
;Return temp

*




.

.
;)Fraction operator/ (Fraction rhs



Fraction
m
) ( m
) (
m


*.
)Fraction operator/ ( Fraction rhs

{
;int m

;)(m=rhs.GetUp

;)(rhs.up=rhs.GetDown

;rhs.down=m

;))(Fraction temp(up* rhs.GetUp() , down* rhs.GetDown

;return temp

8
9

;)Fraction operator+ (int rhs

+



.
.
1

)Fraction operator+ (int rhs


{
;)return Fraction (up+rhs,down
}

2
4
3

) (
:
;S=a+S

S Fraction a int
) (
.31
:
29 28



>> <<
cout .cin
cout cin
iostream cout
ostream cin istream
<< >>

. Fraction
;)& friend ostream &operator << (ostream& ,const Fraction

<<
ostream
cout Fraction

. Fraction
Example
:
; cout << Example

:
; )operator << (cout , c

) ( << operator
cout c Fraction
) ( << operator
:
; return D << temp.up << "/" << temp.down

D cout


.
;)& friend istream &operator >> (istream& , Fraction

istream
>> cin

up E cin
/

c
/



) (
throw



) ( printf
. cout

E << >>

.
)1. istream& operator >> (istream& E, Fraction& temp
{ 2.
;3. E >> temp.up
;4. char c
;)5. E.get(c

;6. if (c !='/') throw


;7. E >> temp.down
;8. return E
} 9.

:Fraction

next phase with Class Fraction:

Fraction
%90


float



Fraction :
float Fraction
flaot num
.
) (+ ) (- )
(
+ .-
Fraction
Fraction )
(1

.


string
:
c

string
char .
string
. string
string :
;string STRIG


:
;"string S1="Hellow
;string S2=S1


strcpy .
)(+
:
;S2=S1+S2



swap
string :
;)S1.swap(S2

string
:
CODE
>1. #include <iostream
>2. #include <string
;3. using namespace std

)(4. int main

{ 5.
;"6. string S1= "languge Java
;"7. string S2= "Languge C++

;8. cout <<"string1:\t\t" << S1 << endl


;S2 << endl

<< "9. cout <<"string2:\t\t

;cout << "After swaping" << endl

10.

;)S1.swap(S2

11.

;cout <<"string1:\t\t" << S1 << endl

12.

<< "cout <<"string2:\t\t

13.

;S2=S1+S2

14.

;cout <<"S2=S1+S2:\t\t" << S2 << endl

15.

;return 0

16.

17.

;S2 << endl

:
languge Java

string1:

Languge C++

string2:
After swaping

Languge C++

string1:

languge Java

string2:

Languge C++languge Java

S2=S1+S2:


string .
: string
cin

. cout
getline
cin string
'.'\n
: S1
CODE
>1. #include <iostream

>2. #include <string


;3. using namespace std
4.
)(5. int main
{ 6.
7.
;"string S1= "languge Java

8.
9.

;)'getline (cin , S1,'\n

10.

;cout << S1

11.

;return 0

12.
}

13.

10 getline cin


getline cin
.
:

find
:
CODE
>1. #include <iostream
>2. #include <string
;3. using namespace std
4.
)(5. int main
{ 6.
7.
;"string S1= "languge Java

8.
9.

;)"int x=S1.find("Java

10.

;cout << x<< endl

11.
12.
13.

;return 0
}

14.

find ) ( Java
10 Java
x 11
8
Java J
. 8

) ( size S2
:
;)(int n=S1.size

n
) char
(.


S2 :
;]char x= S2[1

1
.
:


:
;)string s1(s2

.

:
;)string S3(S1,0,6

S1
S3 S3

) (0 1

.
) (: substr
) (. substr


. :
;)S2= S1.substr ( 5,9

9 S1
. S2

) ( begin ) (: end
begin ) (end
:
;) )(string S2(S1.begin() , S1.end

) (: Capacity




string
31 20

31



) (. capacity
) ( append ) (: .... insert
append
insert
.
.
CODE
>1. #include <iostream
>2. #include <string
;3. using namespace std
4.
)(5. int main
{ 6.
; "string S1= "a lot of programmers

7.

;cout << "Sting S1\t\t" << S1 << endl

8.

"cout << "S1.size\t\t\t

9.

;<< S1.size() << endl

;cout << "S1.capacity()\t\t" << S1.capacity() << endl

10.
11.

;cout << endl

12.
13.

;)"S1.append(" love this languge

14.

;cout << "Sting S1\t\t" << S1 << endl

15.

"cout << "S1.size\t\t\t

16.

;cout << "S1.capacity()\t\t" << S1.capacity() << endl

17.

;<< S1.size() << endl

18.
19.

;cout << endl

20.
;)" S1.insert(0,"C++ Languge

21.

;cout << "Sting S1\t\t" << S1 << endl

22.

"cout << "S1.size\t\t\t

23.

;cout << "S1.capacity()\t\t" << S1.capacity() << endl

24.

;<< S1.size() << endl

25.
26.

;return 0
}

27.
28.
29.

:
a lot of programmers

1. string S1

20

2. S1.size

31

)(3. S1.capacity
4.

a lot of programmers love this lanbuge

5. string S1

38

6. S1.size

63

)(7. S1.capacity
8.

C++ a lot of programmers love this lanbuge

9. string S1

50

S1.size

10.

63

)(S1.capacity

11.

S1 7

.
8
9 10

.
14 append

S1
S1 15
16

31 50 string
. 63

21 insert

21 2223

. append

) (: replace

string
) ( replace

.


20
20
:
CODE
>1. #include <iostream
2.
>3. #include <string
;4. using namespace std
5.
)(6. int main
{ 7.
;)"string S1("The Java Programming Languge

8.

;cout << "S1 Befor\t\t" << S1 << endl

9.
10.
11.

;)"int p=S1.find("Java

12.
13.

;)string S2(S1,p,4

14.
15.

;cout << "S2\t\t\t" << S2 << endl

16.
17.

;)"S1.replace(p,S2.size(),"C++

18.
19.

;cout << "S1 NOW \t\t\t" << S1 << endl

20.
21.

;return 0
}

22.

The Jave Programming Languge


Java . C++
8 S1
.9
11 Java
. p
13 S2
Java S1
Java
replace 4



.
15 S2
. Java
replace 17
p

) ( S2.size
Java
. C++
19 S1
The C++ Programming :
. Languge
) (: erase
erase

.

erase replase
.
CODE
>1. #include <iostream
>2. #include <string
;3. using namespace std
4.
)(5. int main
{ 6.
;)"string S1("The Java Programming Languge

7.

;"string S2="C++

8.

;cout << "S1 Befor\t\t" << S1 << endl

9.

10.

;cout << "S2\t\t\t" << S2 << endl

11.
12.

;)"int p=S1.find("Java

13.
;)S1.erase(p,4

14.

;)S1.insert(p,S2

15.
16.
17.

;cout << "S1 NOW \t\t\t" << S1 << endl

18.
19.

;return 0
}

20.

14 Java
erase
.
15 S2 C++
) ( 8 S1
. Java


) ( erase .

) (: string
string
) ( capacity

string

) ( max_size :
CODE
>1. #include <iostream
>2. #include <string
;3. using namespace std
4.
)(5. int main
{ 6.
;)"string S1("When it stop

7.
8.

;cout << "S1 Now\t\t" << S1 << endl


;cout << "S1\t\t" << S1.max_size() << endl

9.
10.

11.
12.

;return 0
}

13.

S1
4294967293
Visual C++
.


Inheritance
:

.

.

:

..
..
.
.
.
...






-1 :
.
-2 :

.

:


.

.



..

.. ..

.
.

.
..




.
ATM
:
.
.

.

:




) ( )( )(
)(

UML
.

.

.

:

:
CODE
>4. #include <iostream.h
class Father

5.

6.

protected:

7.

;int itsAge

8.

public:

9.

)Father():itsAge(8

10.

} ; "{ cout <<"\n the Father ALIVE \n

11.

} ; "~Father() {cout << "\nthe Father DIEEEE

12.

} ; GetitsAge(){ return itsAge

13.

;}

14.
15.

class son: public Father

16.

{ public:

17.

};"son() { cout << "\nthe son is ALIVE\n

18.

} ; "~son() { cout <<"\nthe son die \n

19.

;}

20.
21.

)(void main

22.

23.

;son you

24.

;)(cout << endl << you.GetitsAge

25.
}

26.

:

the Father ALIVE

1.

the son is ALIVE

2.

3.

the son die

4.

the Father DIEEEEEEE

5.

Father

itsAge

itsAge son

itsAge

.
itsAge
.
13 son Father
:
Father

public

class son

27.


public




21 you
) ( main 21
you
.





CODE
>1. #include <iostream.h
2.
3. class father
{ 4.
5. public:
)(6. father
};"7. { cout << endl <<"I am Alive
)8. father(int x
{ 9.
}; " )cout << endl << " I am Alive (int

10.
11.

;}

12.
13.

class son : public father

14.

15.

public:

16.

}; " son(){ cout <<"\n Hellow son\n

17.

)son(int y): father(y

18.

19.

}; "cout << "\n Hellow son (int)\n

20.

; }

21.



int
) ( main :
1

; son

:
I am alive

son

Hellow


.
:
1

;)son(5


:
)son(int y): father(y

22.

23.

}; "cout << "\n Hellow son (int)\n

24.

22
5

.
:
A .a .int
a .
a int int
) (
.

:
} { )a (int x): A(int x


.
:

.
) (


.
CODE
1. class shape
{ 2.
3. protected:

;4. int itsX1


;5. int itsX2
6. public:
;)(7. shape
;)8. shape(int ,int
;)(9. void Draw
;}

10.

)shape::shape():itsX1(5),itsX2(6

11.

}{

12.

)shape::shape(int x,int y

13.

14.

;itsX1=x

15.

;itsX2=y

16.

17.

)(void shape::Draw

18.

19.

)for (int d1=0;d1<itsX1 ;d1++

20.

21.

)for (int d2=0;d2<itsX2;d2++

22.

;"*" << cout

23.

;cout <<endl

24.

25.

26.
27.

class square:public shape

28.

29.

public:

30.
31.

square(int x):

32.

};shape(){itsX1=itsX2=x
;}

33.

shape squere
shape

square

) ( main
square
CODE
)(void main

;1. {int x
2. do
{ 3.
; 4. cin >> x
;"5. cout << "\n
;)6. squere A(x
;)(7. A.Draw
; )8. } while (x!=0
} 9.



shape
.

) Triangle(
square




) ( Draw



:
CODE
class triangle :public squere

1.

2.

public:

3.

)triangle(int x
}{):squere( x

4.
5.

;)(void Draw

6.
7.
;} 8.
9.

)(void triangle::Draw

10.

11.

)for (int d1=0;d1<itsX1+1;d1++

12.

13.

)for (int d2=0; d2<d1;d2++


;"*"<< cout

14.

15.

};cout << endl


}

16.


) ( Draw
)(
16-10
.
:

) ( Draw
) Draw (int ) ( Draw
) Draw(int
..
.








.
) (
) ( )(
)(

) ( Draw


) ( Draw


.

) (
.

:

)( Draw
shape )( main :
CODE
)(1. void main

;2. {int x
3. do
{ 4.
; 5. cin >> x
;"6. cout << "\n
;)7. triangle A(x
;)(8. A.shape::Draw
; )9. } while (x!=0
10.

Draw

Draw square .
:

:

;)(Draw

::


shape

11.

)( :Virtual Function



.

)
(
:
CODE
>1. #include <iostream.h
2.
3. class Bird
{ 4.
5. public:
} ;"Bird():itsAge(1) { cout << "Bird Alive...\n

6.

} ;"~Bird() { cout << "Bird die...\n

7.

} ;"void fly() const { cout << "Bird fly away\n

8.

} ;"void trills() const { cout << "Bird trills!\n


protected:
;int itsAge

9.
10.
11.
12.

;}

13.
14.

class Dicky : public Bird

15.

16.

public:

17.

} ;"Dicky() { cout << "Dicky Alive...\n

18.

} ;"~Dicky() { cout << "Dicky die...\n

19.

} ;"void trills()const { cout << "ooooooooooooo!\n

20.

} ;"void fly()const { cout << "Dicky speed to...\n

21.
;}

22.
23.

)(int main

24.

25.
26.

;Bird *pDicky = new Dicky

27.

;)(pDicky->fly

28.

;)(pDicky->trills

29.
30.
31.

;return 0
}

32.

Bird Dicky
) ( .
27 Bird
Dicky

28 29 :
CODE
1. Bird Alive...
2. Dicky Alive...
3. Bird fly away
!4. Bird trills


dicky Bird
:

CODE
1. Bird Alive...
2. Dicky Alive...
3. Dicky speed to...
4. ooooooooooooo!


pDicky
trills ( ) fly()
. Bird pDicky

fly trills
:
CODE
1. #include <iostream.h>
2.
3. class Bird
4. {
5. public:
6.

Bird():itsAge(1) { cout << "Bird Alive...\n"; }

7.

virtual ~Bird() { cout << "Bird die...\n"; }

8.

virtual void fly() const { cout << "Bird fly away\n"; }

9.

virtual void trills() const { cout << "Bird trills!\n"; }

10.

protected:

11.

int itsAge;

12.
13.

};

14.
15.

class Dicky : public Bird

16.

17.

public:

18.

Dicky() { cout << "Dicky Alive...\n"; }

19.

virtual ~Dicky() { cout << "Dicky die...\n"; }

20.

void trills()const { cout << "ooooooooooooo!\n"; }

21.

void fly()const { cout << "Dicky speed to...\n"; }

22.

};

23.
24.

int main()

25.

26.
27.

Bird *pDicky = new Dicky;

;)(pDicky->fly

28.

;)(pDicky->trills

29.
30.
31.

;return 0

32.

trilld fly
virtual
.
:
virtual

:virtual
;Bird *pDicky = new Dicky

33.


Bird

virtual
.

virtual

:






)
(
:
CODE
>1. #include <iostream.h
2.
3. class Employee
{ 4.
protected:

5.

;int itsAge

6.

7. public:
8.

Employee():itsAge(0){cout << "\nHii I am Employee\n";}

9.

Employee(int x):itsAge(x)

{cout <<"\nHii

I am

Employee(int)

\n";}
10.
11.

Getme(){cout <<"\n Hiii

I am I am Employee\n";}

};

12.
13.

class prof

14.

{protected:

15.
16.

int itsAge1;
public:

17.

prof():itsAge1(0){cout << "\nHii I am prof\n";}

18.

prof(int

x):itsAge1(x)

{cout

<<"\nHii

am

prof(int)

\n";}
19.
20.

Getme(){cout <<"\n Hiii

I am I am prof\n";}

};

21.
22.

class chief:public prof,public Employee

23.

{public:

24.

chief(){cout << "\nHii I am chief\n";}

25.

chief(int x) {cout <<"\nHii I am chief(int) \n";}

26.

};

27.
28.

void main()

29.

30.

chief ml(9);

31.

ml.prof::Getme();

32.

Hii I am a proof

Hii I am Employee
Hii I am chief(int)
Hii I am I am chief
Employee
22 chief 13 3 prof




:22
:
) ( ,
:

public Employee

public prof


class chief

:
22

prof Employee
chief
30 ml
chief
25


prof
.Employee
prof
Employee
.

:
) ( Getme 31
:
;)(ml.Getme

33.


Employee prof

) ( Getme

33 ) ( Getme
Employee :
;)(ml.Employee::Getme

34.



.

:
CODE

1. class person
2. {
3. public:
4.

person():itsAge(0){cout << "\nHii I am Person\n"; }

5.

person(int x):itsAge(x){ cout <<"\nHii I am Person (int)\n";}

6.

Get() { cout << "\nGetttttttttttttttt\n";}

7. protected:
8.

int itsAge;

9. };
10.

class Employee: public person

11.

12.

public:

13.

Employee(){cout << "\nHii I am Employee\n";}

14.

Employee(int

x):person(x)

{cout

<<"\nHii

am

Employee(int) \n";}
15.
16.

Getme(){cout <<"\n Hiii

I am I am Employee\n";}

};

17.
18.

class prof: public person

19.

20.

public:

21.

prof(){cout << "\nHii I am prof\n";}

22.

prof(int x):person(x) {cout <<"\nHii I am prof(int) \n";}

23.

Getme(){cout <<"\nHiii

24.

I am I am prof\n";}

};

25.
26.

class chief:public prof,public Employee

27.

{public:

28.

chief(){cout << "\nHii I am chief\n";}

29.

chief(int

x):Employee(),prof(x)

{cout

<<"\nHii

am

chief(int) \n";}
30.

};


. prof Employee person
prof Employee
person
person

6
prof Employee Get( )

Get
) ( chief
) ( Get
person
:
CODE
;)(chief ml(9);ml.prof::Getme
;)(ml.Get

1.
2.

chief
) ( Getme prof
) ( Get person
person
.
) ( Get
chief :
CODE
)(int chief::Get

1.
{ 2.

;)(return prof::Get

3.
} 4.

) (Get
. prof
.
Get person
. Employee prof
person .
) (.

:
Employee prof person

person
. person
chief
.person
:
CODE
>#include <iostream.h

1.
2.
3.

4. class person
{ 5.

6. public:
7.

person():itsAge(0){cout << "\nHii I am Person\n"; }

8.

person(int x):itsAge(x){ cout <<"\nHii I am Person (int)\n";}

9.

int Get() { cout << "\nGetttttttttttttttt\n";return itsAge;}

10.
11.

GetItsAge(){return itsAge;}
protected:

12.

int itsAge;

13.

};

14.

class Employee: virtual public person

15.

16.

public:

17.

Employee(){cout << "\nHii I am Employee\n";}

18.

Employee(int

x):person(x+2)

{cout

<<"\nHii

am

Employee(int) \n";}
19.
20.

Getme(){cout <<"\n Hiii

I am I am Employee\n";}

};

21.
22.

class prof: virtual public person

23.

24.

public:

25.

prof(){cout << "\nHii I am prof\n";}

26.

prof(int x):person(x+2)

{cout <<"\nHii

I am

prof(int)

\n";}
27.

Getme(){cout <<"\nHiii

I am I am prof\n";}

28.
29.

};

30.
31.

class chief:public prof,public Employee

32.

{public:

33.

chief(){cout << "\nHii I am chief\n";}

34.

chief(int x):person(x*2) {cout <<"\nHii I am chief(int)

\n";}
35.
36.

Get();
};

37.
38.

int chief::Get()

39.

40.

return prof::Get();

41.

)(void main

42.

43.

;)chief ml(10

44.

;)(ml.person::Get

45.

;cout << ml.GetItsAge()<< endl

46.
}

47.

14 22
Employee prof person
18 26 person
) Employee ( prof itsAge 2
34 chief
person .
chief Employee prof
.

)
(
.

:

shape


shape



shape shape


shape ADT
Abstract Dat Type .

.







:
CODE
1.

class shape

2. {
3. protected:
4.

int d1,d2,d3,d4;

5.
6. public:
7.

shape() {d1=d2=d3=d4=5;

8.

cout << "\nHere I am shape ( ) \n" ;}

9.

shape (int x,int y)

10.

{d1=d3=x;d2=d4=y;

11.

cout << "\nHere I am SHAPE (INT,INT)\n";

12.

13.
14.

shape (int a,int b,int c,int d)

15.

16.

d1=a;d2=b;d3=c;d4=d;

17.

cout

<<

"\nHere

am

SHAPE

(INT

INT

,INT)\n";
18.

19.
20.

virtual void draw() =0;

21.

virtual ~shape(){ cout <<"\n I am diee (shape)\n " ;}

22.

};

23.
24.

class Rectangle:public shape

25.

26.

public:

27.

Rectangle(){ cout<<"\nHere I am Rectangle() \n";}

28.

Rectangle(int a,int b):shape(a,b)

29.

{cout << "\nHere I am Rectangle (INT

30.

Rectangle(int a,int b,int c,int d):shape(a,b,c,d)

31.

{ cout <<"\nHere I am Rectangle (INT,INT,INT,INT)\n";}

, INT) \n";}

INT

{ )(void draw

32.

)for (int i=0;i<d1 ;i++

33.

34.

)for (int j=0;j<d2;j++

35.

;"*" << cout

36.

;cout <<endl

37.

38.

39.

};"~Rectangle(){cout<< "\n I am diee (Rectangle)\n

40.
41.
;}

42.
43.

;"

class Circle:public shape

44.

45.

public:

46.

;" )Circle(int m){cout << "Here I am Circle (INT

47.

};itsd=d1

48.

void draw() { cout <<"\n Here I am draw a Circle has

49.

" << cout << itsd

50.

};"~Circle(){ cout <<"\nI am die Circl \n

51.

};"cm \n

private:
;int itsd

52.
53.

;}

54.

shape
Rectangle Circle


20 shape
shape

:
.ADT

.ADT

ADT .


Linked List
:


.

:
) (





1000



1000 2000

1000



.

:



char
int
) int(
2 4



) (

40
40


40
) 40 (20

15
15








) ( .

/1



.

:
:

Linked List



.


.
:
CODE
struct link
{
;int number

49.
50.
51.

;float degree

52.

;int hours

53.

;link* next

54.

;}

55.

link




.
link course


.course


:
1. struct link
{ 2.
;m

3. link
} 4.



.



.
link list
link :
CODE
1. class linklist
{

2.

private:

3.

;link* first
public:

4.
5.

)(linklist

6.

} ;{ first = NULL

7.

;)void additem(int d

8.

;)(void display

9.

;)void find (int f

10.

;)(void Enter

11.
;}

12.

linklist

.

8 ) ( additem
.
9 .
10 find .
11 Enter

.

) (:additem

:

)1. void linklist::additem(int d


{ 2.
;3. link* newlink = new link
;4. newlink->number = d
;5. newlink->next = first
;6. first = newlink

} 7.

link

5 first
next first
new link
:
;)1. additem( 5

linklist
first
additem link
5 number
) first ( next
newlink newlink

newlink first
newlink first

:
;)2. additem( 6

additem
.

newlink
newlink

first

newlink ) (5
) next (newlink
newlink
6 newlink
5 first
newlink

:
;)3. additem( 7


next newlink
first
).newlink(6
) newlink(5
) newlink(6 ) newlink(7
.
first
) newlink(7 ) ( additem

) newlink(6 ) newlink(6 next
) newlink(5



next ) newlink(5
.

) (:Enter

:
)(1. void linklist::Enter
{

2.

;"cout << "Enter its Degree:

3.

;cin >> first->degree

4.

;" cout << "Enter its hours:

5.

;cin >> first->hours

6.
7.
8.

9.

) (:Display
:
)(1. void linklist::display
{

2.

;link* temp = first

3.

;"cout << "\n\n--------------------------------------------\n

4.

) while( temp != NULL

5.
6.

{
;cout <<"Number Of Course:\t" << temp->number << endl

7.

;cout << "its degree:\t\t" << temp->degree << endl

8.

;cout << "its Hours:\t\t " << temp->hours << endl

9.

;"cout << "--------------------------------------------\n

10.

;temp = temp->next

11.

12.
13.

temp
first ) newlink(7
while
temp NULL
temp
7 10 11
first temp
temp
) newlink(5
3 :
5. 11

) (:Find
:
)1. void linklist::find(int f
{ 2.
;3. int m=1;link* temp=first
{ ) 4. while( temp != NULL
)if (f==temp->number

5.

{ 6.
;"7. cout << "It is exisit\n
;8. cout <<"Number Of Course:\t" << temp->number << endl
;9. cout << "its degree:\t\t" << temp->degree << endl
;cout << "its Hours:\t\t " << temp->hours << endl

10.

11.

cout << "-----------------------------------------------

;"\n
;m++

12.

};break

13.

;temp = temp->next

14.

15.

;"if(m==1) cout << "\n\n(SORRY)....Not Exisit\n

16.

17.



m 1
link first Display
5

if

m if while
m
m
16
.

- :main( ) -


.
)(1. int main
{ 2.
;3. linklist li
;4. int m
;5. int i=1
;6. char choice
7. do
{ 8.
;9. cout << "ENTER YOUR CHOICE:" << endl
cout << "(a)for entre data\t(b)for
;"

10.

search\t(c)print\t(d)END:
;cin >> choice

11.

)switch (choice

12.

13.

case 'a':

14.

);;( for

15.

16.

;"cout << "Enter the number of Course?:

17.

;cin >> m

18.

;if (m==0) break

19.

;)li.additem(m

20.

;)(li.Enter

21.

22.

;break

23.

case 'b':

24.

;int n

25.

);;(for

26.

27.

;"cout << "Do you want to search?\t

28.

;cin >> n

29.

;if (n==0) break

30.

;cout << "\nJust wati a minute" << endl

31.

;)li.find(n

32.

33.

;break

34.

case 'c':

35.

;li.display();break

36.

case 'd':

37.

;i=2

38.

39.

;)}while (i==1

40.

;return 0

41.

42.

:



] [
Display






.

.
:

.

.
stack

. :
>1. #include <iostream
;2. using namespace std
3.
;4. const int max=20
5. class Stack
{ 6.
;]7. int st[max
;8. int top
9. public:
};Stack(){top=-1

10.

};void push( int element ){st[++top]=element

11.

};]int pop() { return st[top--

12.

;}

13.
14.

)(int main

15.

16.

;Stack temp

17.
18.

;)temp.push(10

19.

;)temp.push(11

20.

;)temp.push(12

21.

;)temp.push(13

22.

;)temp.push(14

23.

;cout << "First:\t " << temp.pop() << endl

24.

;cout << "Second:\t " << temp.pop() << endl

25.

26.

;cout << "third:\t " << temp.pop() << endl


;cout <<"fourth:\t" << temp.pop() << endl

27.

;cout << "fifth:\t" << temp.pop() << endl

28.
29.
30.

;return 0
}

31.
32.


) (.
stack int

.
Stack .
CODE
;1. const int max=20
2. template <class T> class Stack
{ 3.
;]4. T st[max
;5. int top
6. public:
};7. Stack(){top=-1
;};8. void push( T element ){st[++top]=element
; )(9. T pop
;}

10.

Stack T
.
9
:
)(1. template <class T> T Stack<T>::pop
{ 2.
;]3. return st[top--
} 4.


> template <class T
Stack
> <T .
.

)(: main
)(1. int main
{ 2.
;Stack <int> temp

3.
4.

;)temp.push(10

5.

;)temp.push(11

6.

;)temp.push(12

7.
8.

;cout << "First:\t " << temp.pop() << endl

9.

;cout << "Second:\t " << temp.pop() << endl

10.

;cout << "third:\t " << temp.pop() << endl

11.
12.

;return 0

13.

14.


. 3
> <

Fraction .
struct
. class
:

.

.



.
) (
.

.
:
CODE
>1. #include <iostream
;2. using namespace std
3. /**********************************/

4. template <class T>struct link


5.

6.

T element;

7.

link* next;

8.

};

9. /*********************************/
10.

template <class T> class linklist

11.

12.

private:

13.

link <T>* first;

14.

public:

15.

linklist()

16.

{ first = NULL; }

17.

void additem(T d);

18.

void display();

19.

};

20.

/**********************************/

21.

template <class T> void linklist<T>::additem(T x)

22.

23.

link<T> *newlink = new link<T>;

24.

newlink->element = x;

25.

newlink->next = first;

26.

first = newlink;

27.

28.

/*************************************/

29.
30.

template <class T> void linklist<T>::display()

31.

32.

link<T> *temp = first;

33.

cout << "\n\n---------------------------------------\n";

34.

while( temp != NULL )

35.

36.

cout << endl <<" number:\t" << temp->element;

37.

temp = temp->next;

38.

39.
40.
41.

}
/*******************************************/

;"

)(int main

42.

43.

;linklist<double> li

44.

;int m

45.

;int i=1

46.

;char choice

47.

do

48.

49.

;cout << "ENTER YOUR CHOICE:" << endl

50.

cout << "(a)for entre data\t(b)for print\t(c)END:

51.

;cin >> choice

52.

)switch (choice

53.

54.

case 'a':

55.

);;( for

56.

57.

;"cout << "\nEnter the element you want to add it:

58.

;cin >> m

59.

;if (m==0) break

60.
61.

;)li.additem(m

62.

63.

;break

64.

case 'b':

65.

;li.display();break

66.
67.

case 'c':

68.

;i=2

69.

70.

;)}while (i==1

71.

;return 0

72.

73.

link
.

.
linkedlist
13
.

:



<< >> linke
linkedlist


.




.

.
CODE
1. class courses
{ 2.
;3. int number
;4. float grade
;5. int hours
6.
public:

7.

;)(courses

8.

;)courses(int a

9.

;)courses (const courses& rhs

10.

;int getNumber()const

11.

; float getGrade()const

12.

; int getHours()const

13.

};setNumber(const int a){number=a

14.

};setHours(const int a) {hours=a

15.

};setGrade(const float a){grade=a

16.

;)& courses &operator= (const courses

17.

;)& friend ostream &operator << (ostream& ,const courses

18.

;)friend istream &operator >> (istream& E, courses &temp

19.
20.
;}

21.

}{)courses::courses (): number(0),grade(0),hours(0

22.

23.

courses::courses (int a):number(a),grade(0),hours(0){}

24.

courses::courses (const courses& rhs)

25.

{number=rhs.getNumber();

26.

grade=rhs.getGrade();

27.

hours=rhs.getHours();

28.

29.
30.

int courses::getNumber()const {return number;}

31.

float courses::getGrade()const {return grade;}

32.

int courses::getHours()const {return hours;}

33.
34.

courses& courses::operator= (const courses& rhs)

35.

36.

if(this==&rhs) return *this;

37.

number=rhs.getNumber();

38.

grade=rhs.getGrade();

39.

hours=rhs.getHours();

40.

return *this;

41.

42.
43.

//////////////////////////////////////

44.

istream& operator >> ( istream& E, courses& temp)

45.

46.

float i=0;int j=0;


cout << "Enter its grade:";

47.

cin >> i;temp.setGrade(i);

48.

cout << "Enter its hours: ";

49.

cin >> j;temp.setHours(j);

50.

return E;

51.

52.

///////////////////////////////////////

53.

ostream &operator << (ostream& D , courses &temp)

54.

55.

D <<"Number Of Course:\t" << temp.getNumber() <<

endl;
56.

D << "its degree:\t\t" << temp.getGrade() << endl;

57.

D << "its Hours:\t\t " << temp.getHours() << endl;

58.
59.

D << "--------------------------------------------\n";

; return D

60.

61.


10 .


10 .24
<< >>
18 19
44 . 53
courses
.


.
)( main .
CODE
)(1. int main
{ 2.
;linklist<courses> course

3.

;courses a

4.

;char b

5.
6.

do

7.

8.

;cin >> a

9.

;)course.additem(a

10.

;" ?)cout << "\nAdd another (y/n

11.

;cin >> b

12.

;)'} while(b != 'n

13.

;)(course.display

14.

;cout << endl

15.

;return 0

16.

17.

) ( main
iostream
>>
std . iostream.h


Handling Exceptions In C++
:


.
:


.


.


.
:



) (


.


:
. . . . :

:
-1 : try
try
:

{ try
;statement1
;statement2
;..etc
}
-2 :
try

catch catch
catch
:
)catch (Argument
{
;statement1
;statement2
//What to do about this exception
}
-3 :

) try ( catch
try
. catch
:



:
-1 .
-2
.
CODE
>1- #include <iostream
;2- using namespace std
3)(4- int main
{ 5-
;int TheYear

67-

;" cout << "TheYear

8-

;cin >> TheYear

910-

11-

{ try

12-

)if(TheYear <= 0

13-

;throw

14-

;"cout << "\nTheYear: " << TheYear << "\n\n

15-

1617-

{)catch(...

18-

1920-

;"cout << "\n

2122-

;return 0
}

23-

TheYear
.
11 try
. 15
try
catch 17

.

12 throw
if .
catch ) (...
) catch (



.

throw


:
CODE
>1. #include <iostream
;2. using namespace std
3.
)(4. int main

{ 5.
;int TheYear

6.
7.

;" cout << "TheYear

8.

;)cin.exceptions(cin.failbit

9.
10.
11.
12.

{ try
;cin >> TheYear

13.

)if(TheYear <= 0

14.
15.

; "throw "Hellow

16.

;"cout << "\nTheYear: " << TheYear << "\n\n

17.

18.
19.
20.

{)catch(...

21.

;"cerr << "\nSomthing is not right\n

22.

23.
24.

;"cout << "\n

25.
26.

;return 0
}

27.

9
cin


.
15 throw
throw
catch catch
catch 20
catch .
21
cout cerr
cout

cout .

catch:



0 :
CODE
>1. #include <iostream
;2. using namespace std
3.
)(4. int main
{ 5.
;int TheYear

6.
7.

;" cout << "TheYear

8.

;)cin.exceptions(cin.failbit

9.
10.
11.
12.

{ try
;cin >> TheYear

13.

)if(TheYear <= 0

14.
15.

; "throw "Bad Value

16.

;"cout << "\nTheYear: " << TheYear << "\n\n

17.

18.
19.

{)catch (char *Error

20.

;cerr << Error << endl

21.

22.
23.
24.

{)catch(...

25.

;"cerr << "\nSomthing is not right\n

26.

27.
28.

;"cout << "\n

29.
30.

;return 0
}

31.


0 if 14
.15
15 throw Bad Value
0
throw .
catch

. 19
throw catch
19 throw
:
{)catch (char *Error

catch
.
25 .
:
throw
throw catch
.
:







)


(:
CODE
>1. #include <iostream
;2. using namespace std
3.
)(4. int main
{ 5.
;)cin.exceptions(cin.failbit

6.

;float *Course

7.

;int NumberOfCourse

8.

;float Total

9.

10.

float Avg;

11.
12.
13.

try {

14.

cout << "please Enter the number of course:\t";

15.

cin >> NumberOfCourse;

16.

Course=new float[NumberOfCourse];

17.

for (int i=0;i<NumberOfCourse;i++)

18.

19.

try{

20.

cout << "Enter the grade of couse" << i+1

21.

<< ": \t";

22.

cin >> Course[i];

23.
24.

Total=+Course[i];

25.

26.

catch(...)

27.

28.

cerr << "\nBad Value\n";

29.

cin.clear();

30.

char Bad[5];

31.

cin >> Bad;

32.

i--;

33.
34.

35.
36.

37.

Avg=Total/NumberOfCourse;

38.

cout << endl;

39.

cout << "The Total of grade:\t" << Total;

40.

cout << "\nThe Avg:\t" << Avg;

41.

cout << endl;

42.

43.

catch(...)

44.

45.
46.
47.

cerr << "Somthing not right" << endl;


}
return 0;

48.


:
6 cin
.
10-7
7

.
13 . try
. try
16
13 16
. 26
for 17



.
19 try
. for
. for
24-20

22
) ( abs



catch .
29 clear
.
30 .
31

.30
32 for
for

.
.
:

throw
catch throw
catch
int char ... float
10 catch



int ... float




.
Stack 0
100
.
CODE
>1. #include <iostream
;2. using namespace std
;3. const int element=100
4. class Stack
{ 5.
;]int array[element

6.

7. public:
)Stack(){ for(int i=0;i<element;i++

8.

};array[i]=i

9.

)getElement(int x

10.

11.
12.

)if (x>100

13.

;)(throw xThrow
;)(else if (x<0) throw sThrow

14.

;]else return array[x

15.
}

16.

;}{class xThrow

17.

;}{class sThrow

18.
;}

19.
20.

)(int main

21.

22.

;int i=0; Stack a

23.

{);;(for

24.

{try

25.

;"cout << "Choise The element\n

26.

;cin >> i

27.

;cout << "The element:\t" << a.getElement(i) << endl

28.

29.

{)catch(Stack::sThrow

30.
31.

;"cout << "Small Element\n


}

32.

{)catch(Stack::xThrow

33.
34.

;"cout << "Big Element\n


}

35.

36.
37.
38.
39.

;return 0
}

40.

Stack
100

.
Stack 4 .19
Stack
xThrow sThrow 17 . 18
) ( getElement
12
100
xThrow 14
100
sThrow 15
.
) ( main i
int a . Stck
.23
for

.24
. 36
try 25
27
.

28 getElement

.
100
13 catch 33

catch
. Stack::xThrow
100
Stack::sThrow
catch . 33

.



.

.
Stack


. Stack

:




.

. catch
CODE
>1. #include <iostream
;2. using namespace std
;3. const int element=100
4. class Stack
{ 5.
;]int array[element

6.

7. public:
)Stack(){ for(int i=0;i<element;i++

8.

};array[i]=i

9.

};"~Stack() { cout << "\n dieeeee\n

10.

)getElement(int x

11.

12.

13.

if (x>100)

14.

throw xThrow();

15.

else if (x<0) throw sThrow();

16.

else return array[x];

17.

18.

class xThrow{public:

19.

xThrow(){}

20.

xfalse(){

21.

};

22.

class sThrow{

23.

public:

cout << "Big Element\n";}

24.

sThrow(){}

25.

sfalse() {cout << "Small Element\n";}

26.
27.

};
};

28.
29.

int main()

30.

31.

int i=0; Stack a;

32.

for(;;){

33.

try{

34.

cout << "Choise The element\n";

35.

cin >> i;

36.

cout << "The element:\t" << a.getElement(i) << endl;

37.

38.

catch(Stack::sThrow a){

39.

a.sfalse();

40.

41.

catch(Stack::xThrow a){

42.

a.xfalse();

43.

44.

45.

return 0;

46.


Stack

Stack
Stack
.
xThrow
. xfalse
sThrow

. sfalse
catch
sThrow 32
catch . 41
sfalse xfalse
.


.


Handling With Files In C++
:










.

:ios

ios
) (
ios istream ostream
iostream
fsreampas
ifstream ofstream
istream ostream .

:Formatted File I/O


formatted

.

12.5
float 12.5
' '2' '1 ' '. ''5
4

.
CODE
>#include <iostream

>#include <fstream
>#include <string
;using namespace std

)(int main
{
;'char x = 's
;int d = 77
;double i = 3.14
;"string String1 = "Hellow
;"string String2 = "World

;)"ofstream fout("data.txt
fout << x
<< d
' ' <<
<< i
<< String1
' ' <<
;<< String2
;"cout << "File Completed\n
;return 0
}


data.txt
fout ofstream


<<
cout



.

: data
CODE
>1. #include <fstream
>2. #include <iostream

>3. #include <string


;4. using namespace std

)(5. int main


{ 6.
;7. char m
;8. int i
;9. double j
;string chara

10.

;string chara2

11.

;)"ifstream fin("data.txt

12.

;fin >> m >> i >> j >> chara >> chara2

13.

cout << m << endl

14.

<< i << endl

15.

<< j << endl

16.

<< chara << endl

17.

;<< chara2 << endl

18.

;return 0

19.

20.



ifstream

fin




.

:
""\n
cin

getline :
CODE
>1. #include <fstream

>2. #include <iostream


>3. #include <string
;4. using namespace std

)(5. int main


{ 6.
;)"7. ofstream fout("first.txt
"8. fout << "HELLOW EVREY ONE\n
"<< "You Are Here\n

9.

"<< "in my program\n

10.

11.

;return 0

12.

13.

first
89 10 ""\n


:
CODE
>1. #include <fstream
>2. #include <iostream
>3. #include <string
;4. using namespace std

)(int main
{

5.
6.

;]char Array[80

7.

;)"ifstream fin("first.txt

8.

) )(while ( !fin.eof

9.

10.

;)fin.getline(Array,80

11.

;cout << Array << endl

12.

13.

14.

;return 0

15.

16.

first 8 first


9 while
) ( !fin.eof
11
getline " "\n
Array
12
.
) (:eof
end of file
) EOF (

.
: Binary File



.

.

float int


.
:
write
ofstream ) ( read

.ifstream


.
CODE
>1. include <iostream
>2. #include <fstream
;3. using namespace std
)(4. void main
{ 5.

;]6. int Array[40


;7. int j=0

)8. for(int i=0;i<40;i++


{ 9.
;j=i*10

10.

;Array[i]=j

11.

12.

)for (i=0;i<40;i++

13.

14.

;cout << Array[i] << endl

15.

16.

;)ofstream fout("test.dat", ios::binary

17.

)fout.write (reinterpret_cast<char*>(Array),40*sizeof(int

18.
;)

;)(fout.close

19.

20.

40
10
17
) (ofstream



. ios::binary 18
Array test.dat
. write
:write
) ( write
.

Array
18
18 :
;) fout.write ( (char*) &Array, sizeof Array

19


.

test
60
40
40 int 60 int
:
CODE
>1. #include <iostream
>2. #include <fstream
;3. using namespace std
4.
)(5. void main
{ 6.
;]int Array[60

7.

;int i=0

8.
9.

;)ifstream fin("test.dat", ios::binary

10.

;) )fin.read( (char*) &Array, 40*sizeof(int

11.
12.

)for(i=40;i<60;i++
;Array[i]=i*10

13.
14.
15.

)for(i=0;i<60;i++
;cout <<Array[i]<<endl

16.
17.
18.

;)(fin.close

19.

20.

10
11 Array
read sizeof
Array Array 60
int 40
int 40 int
160 16 17
Array 16 17

Array 19
.

:

.

.
CODE
>1. #include <iostream
>2. #include <fstream
;3. using namespace std
4.
5. class Stud
{ 6.
;]char itsname[100

7.

;int itsmark

8.
9.

public:

10.

)(Data

11.

12.

; " cout << "Enter his name:

13.

;cin >> itsname

14.

; " cout << "Enter his mark:

15.

;cin >> itsmark

16.
17.

}
;}

18.
19.

)(void main

20.

21.

;Stud Ahmed

22.

;)(Ahmed.Data

23.

;)ofstream fout ("Student.dat",ios::binary

24.

;)fout.write((char*) &Ahmed, sizeof Ahmed

25.

;)(fout.close

26.
}

27.


Stud
25

:

CODE
>1. #include <iostream
>2. #include <fstream
;3. using namespace std
4.
5. class Student
{ 6.
;]char itsname[100

7.

;int itsmark

8.
9.

public:

10.

)(DisplayData

11.

12.

; " cout << "his name:

13.

;cout << itsname << endl

14.

; " cout << "his mark:

15.

;cout << itsmark << endl

16.
17.

}
;}

18.
19.

)(void main

20.

21.

;Student Saeed

22.

;)ifstream fin ("Student.dat",ios::binary

23.

;)fin.read((char*) &Saeed, sizeof Saeed

24.

;)(Saeed.DisplayData

25.

;)(fin.close

26.
}

27.



24 student.dat
. Saeed
:
student stud
student






stud
student


student
%100
.
:




:
CODE
>1. #include <iostream
>2. #include <fstream
;3. using namespace std
4.
5. class Student
{ 6.
7.
;]char itsname[100

8.

;int itsmark

9.
10.
11.

public:

12.

)(GetData

13.

14.

; " cout << "Enter his name:

15.

;cin >> itsname

16.

; " cout << "Enter his mark:

17.

;cin >> itsmark

18.
}

19.

)(DisplayData

20.

21.

; " cout << "his name:

22.

;cout << itsname << endl

23.

; " cout << "his mark:

24.

25.

cout << itsmark << endl;

26.

27.
28.

};

29.
30.

void main()

31.

32.

char ch;

33.

Student Ahmed;

34.

fstream file;

35.
36.

file.open("Student.DAT", ios::app | ios::out |

37.

ios::in | ios::binary );

38.

do

39.

40.

cout << "\nEnter The Data Of Student:\n";

41.

Ahmed.GetData();

42.
43.

file.write( (char*) &Ahmed, sizeof Ahmed );

44.

cout << "Enter another person (y/n)? ";

45.

cin >> ch;

46.

47.

while(ch=='y');

48.
49.

file.seekg(0);

50.
51.

file.read((char*) &Ahmed, sizeof Ahmed );

52.

while( !file.eof() )

53.

54.

cout << "\nStudent:";

55.

Ahmed.DisplayData();

56.

file.read( (char*) &Ahmed, sizeof Ahmed );

57.

58.
59.

cout << endl;


}


Student
fstream file

34
fstream ifstream
. ofstream
) (: open
fstream



36 open

ios::app
ios::out )
(ofstream
ios::int )
( ifstream ios::binary
The Mode Bits

.


ios::app

ios::ate

ios::trunce

ios::noceate
ios::noreplace
) ( ifstream
ios::in
) ( ofstream
ios::out
.
ios::binary
:43
Student


.
:
)( seekg fstream

0
49
51
while
.
while 56
while .

:File Pointers

int Put Pointer
get pointer
.
.the current position :

.
seekg( ) : ) (. tellg
) ( Seekg


0
104
Student
2 2
2

) ( seekg
ios::beg
ios::cur ios::end


:
.

;)fin.seekg(-50,ios::end

50
.
) ( tellg
.

Student
. .
CODE
>1. #include <fstream
>2. #include <iostream
;3. using namespace std
4.
5. class Student
{ 6.
7.
;]char itsname[100

8.

9.

int itsmark;

10.
11.
12.

public:

13.

GetData()

14.

15.

cout << "Enter his name: " ;

16.

cin >> itsname;

17.

cout << "Enter his mark: " ;

18.

cin >> itsmark;

19.

20.

DisplayData()

21.

22.

cout << "his name: " ;

23.

cout << itsname << endl;

24.

cout << "his mark: " ;

25.

cout << itsmark << endl;

26.

27.
28.

};

29.
30.

void main()

31.

32.

Student Ahmed;

33.

ifstream looking;

34.

looking.open("Student.dat",ios::in || ios::binary);

35.
36.

looking.seekg(0,ios::end);

37.
38.

int endF=looking.tellg();

39.

int x= endF /sizeof(Ahmed);

40.

cout << "There are " << x << " Student:\n";

41.
42.

cout << "Enter the number of student you want to display

his data\n";
43.

cin >> x;

44.

int position = (x-1) * sizeof(Ahmed);

45.

looking.seekg(position);

46.
;)looking.read( (char*) &Ahmed, sizeof Ahmed

47.

;)(Ahmed.DisplayData

48.

49.



Stuudent

3
3 Student

.
33 looking fstream
34 36
) ( tellg
38
39 .

43 44
45
.

.
:




.
:
Student
) ( main
Student :
CODE
>1. #include <fstream
>2. #include <iostream
;3. using namespace std
4.
5. class Student
{ 6.
7.
;]char itsname[100

8.

9.

int itsmark;

10.
11.
12.

public:

13.

GetData()

14.

15.

cout << "Enter his name: " ;

16.

cin >> itsname;

17.

cout << "Enter his mark: " ;

18.

cin >> itsmark;

19.

20.

DisplayData()

21.

22.

cout << "his name: " ;

23.

cout << itsname << endl;

24.

cout << "his mark: " ;

25.

cout << itsmark << endl;

26.

27.

DataIn(int x)

28.

29.

ifstream infile;

30.

infile.open("Student.dat", ios::binary);

31.

infile.seekg( x*sizeof(Student) );

32.

infile.read( (char*)this, sizeof(*this) );

33.

34.

DataOut()

35.

36.

ofstream outfile;

37.
38.

outfile.open("Student.dat", ios::app | ios::binary);

39.

outfile.write( (char*)this, sizeof(*this) );

40.

41.

static int ManyInFile()

42.

43.

ifstream infile;

44.

infile.open("Student.dat", ios::binary);

45.

infile.seekg(0, ios::end);

46.

return (int)infile.tellg() / sizeof(Student);

47.

48.
;}

49.
50.

)(int main

51.

52.

;Student Ahmed

53.

;char x

54.
55.

{ do

56.

;"cout << "Enter data for Student:\n

57.

;)(Ahmed.GetData

58.

;)(Ahmed.DataOut

59.

;" ?)cout << "Register another (y/n

60.

;cin >> x

61.

62.

;)'while(x=='y

63.
;)(int n = Student::ManyInFile

64.

;"cout << "There are " << n << " persons in file\n

65.

)for(int j=0; j<n; j++

66.

67.

;cout << "\Student " << j

68.

;)Ahmed.DataIn(j

69.

;)(Ahmed.DisplayData

70.

71.

;cout << endl

72.

;return 0

73.

74.



80 81



.
:

.

Test.dat

. if
:
;1. ifstream file
;)file.open(a:test.dat

2.
3.

) if( !file
;"cout << "\nCant open test.DAT
else
;"cout << "\nFile opened successfully.

4.
5.
6.
7.

if test
5 . 7

.


Standard Template Library

:
STD

iostream string


.
. std

.
:

:

Iterators
.
:




:



.
: Iterators



. .
:
:


vector

list
deque

queue
stack

map
set

.

string .

) (begin
) (end
) (front
) (back
) (push_back
) (pop_back
) (push_front
) (pop_front

)insert(a , b
)insert(a,b,c
)insert(a,first,last
)erase(a
)erase(a,b
) (clear
) (size
) (empty
) (capacity
)swap(a,b
)find(m

b a

c b
a
first last
a

a
a b



1




a b

m

: vector


.




:
CODE
>1. #include <iostream
>2. #include <vector
;3. using namespace std
4.
)(5. int main
{ 6.
;vector <double> v

7.

;double k=0

8.
9.

"?cout <<"please enter your grade in all course


;"<< " (for out pree 0)\n

10.
11.

12.
13.

{)for (int i=1;;i++

14.

cout << "please enter your grade in course" << i

15.

;"<< ":\t
;cin >> k

16.

;if (k==0)break

17.

;)v.push_back(k

18.
19.

20.
;)(int j=v.size

21.

;double total=0 ,avg=0

22.

)for (i=0;i<j;i++

23.
24.

;]total+=v[i

25.
26.

;avg=total/j

27.
;cout << endl << endl

28.

;cout << "Your total of grades is:\t\t" << total << endl

29.

;cout << "Your Avg is\t\t\t\t" << avg << endl

30.
31.
32.

;return 0
}

33.


. vector
.
7 vector

double v
.
for
16 0
for
.vector
18
v ) (. push_back
21 for

) (. size
23 24
] [ .
. 26

29 . 30

string
.

) (. push_front
: Lists
list
. list


:
CODE
>1. #include <iostream
>2. #include <list
;3. using namespace std
4.
;5. typedef list<int> grade
6.
)(7. int main
{ 8.
;grade v
;double k=0

9.
10.
11.

"?cout <<"please enter your grade in all course


;"<< " (for out pree 0)\n

12.
13.
14.

{)for (int i=1;;i++


cout << "please enter your grade in course" << i

15.
16.
17.

;"<< ":\t
;cin >> k

18.

;if (k==0)break

19.

;)v.push_back(k

20.
}

21.
22.

;)(int j=v.size

23.

;double total=0 ,avg=0

24.

;)(for (grade::const_iterator ci = v.begin

25.

26.

)ci != v.end(); ++ci

27.

;)total+=(*ci

28.

;avg=total/j

29.
;cout << endl << endl

30.

;cout << "Your total of grades is:\t\t" << total << endl

31.

;cout << "Your Avg is\t\t\t\t" << avg << endl

32.
33.
34.

;return 0
}

35.


5
grade
.

25 . 26



: 25
;)(grade::const_iterator ci = v.begin

;)(list<int>::const_iterator ci = v.begin


list
const_iterator
.
for 26
ci list
for
.
27 . total

.
.

: deque


)
(
.

:
reverse( ) merge( )
:
CODE
1. #include <iostream>
2. #include <list>
3. using namespace std;
4.
5. int main()
6.

7.

int j,i;

8.

list<int> list1, list2;

9.
10.

for(j=0,i=0; j<4; j++,i=j*2)

11.

list1.push_back( i );

12.

for(j=0,i=0; j<5; j++,i=j*3)

13.

list2.push_back( i );

14.

//list1: 0, 2, 4, 6

//list2: 0, 3, 6, 9, 12

cout << "list1:\t";

15.
16.

for(list<int>::const_iterator c=list1.begin();

c!=list1.end();
17.

++c)

18.

cout << *c << "\t";

19.

cout << endl;

20.

cout << "list2:\t";

21.
22.

for(list<int>::const_iterator a=list2.begin();

a!=list2.end();
23.

++a)

24.

cout <<

25.

cout << endl;

*a <<"\t";

26.
27.

list1.reverse();

// list1: 6 4 2 1

28.

list1.merge(list2);

//list1+=list2

29.
30.
31.

int size = list1.size();

) )(while( !list1.empty

32.

33.

;' ' << )(cout << list1.front

34.

;)(list1.pop_front

35.

36.

;cout << endl

37.

;return 0

38.

39.

list1 list2
11 13 25 16
.
27 ) ( reverse
.
28 ) ( merge list1
. list2
while 32 35
list1 34
35

.

: Associative Containers
vector deque list


) (.










) (
.



.

.

set multset map . multmap


set
map
.
.
: set
set

push pop
.
insert


:
CODE
>1. #include <iostream
>2. #include <set
>3. #include <string
;4. using namespace std
5.
)(6. int main
{ 7.
;set <string> names

8.

;)"names.insert("Mohamed

9.

;)"names.insert("Ahmed

10.

;)"names.insert("Sultan

11.

;)"names.insert("Emad

12.

;)"names.insert("Thamier

13.
14.

;string a

15.

;set<string>::const_iterator i

16.
17.

)for (i=names.begin();i!=names.end();++i
;cout << *i << endl

18.
19.

;char sure

20.

{);;(for

21.

<< cout

22.

;cin >> sure

23.

;""\nDo you want to add another(y/n):\n

24.
25.

26.

{)'if(sure=='y
;cin >> a

27.

};)names.insert(a

28.

;else if (sure=='n') break

29.

;"else cout << "Try againe\n

30.

31.
32.

);;(for

33.

34.

;"cout << "Do you want to find a name\n

35.

;cin >> sure

36.

{)'if(sure=='y

37.

;"cout << "Enter the name\t

38.

;cin >> a

39.

;)i=names.find(a

40.

;"if ( i== names.end()) cout << "Not in there\n

41.

;"else cout << "we found it\n

42.

43.

;else if (sure=='n') break

44.

;"else cout << "try againe please\n

45.
}

46.

;"cout << endl << "Think for using this\n

47.
48.
49.

;return 0
}

50.

set
.
8 set . names
18- 9 insert 5
string . names
15 string
16 i
.
18 19 for
.

for
. 21
23 y n

30 for
.
29 for
.n
y
a 27
names . 28
for

. 33

y 39
.
40 find

.i
41
i
end

. 42
47 .

: map



string

:
CODE
>1. #include <iostream
>2. #include <map
>3. #include <string
;4. using namespace std
5.
)(6. int main
{ 7.
;string a;double x

8.

;}"string name[]={"Ahmed","Iman","Amani","Mohamed","Fadi

9.

;}double numOfTel[]={12548,15879,13648,14785,5826

10.
11.

;map<string, double> mapTel

12.

;map<string, double>::iterator i

13.

14.
)for(int j=0; j<5; j++

15.

16.

;]a = name[j

17.

;]x = numOfTel[j

18.

;mapTel[a] = x

19.

20.
21.

;" cout << "Enter name:

22.

;cin >> a

23.

;]x = mapTel[a

24.

;"cout << "Number_Of_Tel: " << x << "\n

25.
26.

;cout << endl

27.

)for(i = mapTel.begin(); i != mapTel.end(); i++

28.
29.

;"cout << (*i).first << ' ' << (*i).second << "\n
;return 0

30.

31.


.
. map
string
) (.
12 map
:
;map<string, int> mapTel





.
13 i
.12
20-15
.
23 .

x
0 x . 24
x .25
28 29 . map



.
: Algorithms
STL
.

STL .


.


) (
printf
.
CODE
>1. #include <iostream
>2. #include <string
;3. using namespace std
4.
>5. template <class T
{ 6. class prin
public:

7.

)void operator() (const T& t

8.

9.
10.

; cout << t

11.

}
;}

12.

;prin <string> print

13.
14.

)(int main

15.

16.

;string a

17.

;cin >> a

18.

;)print(a

19.

;)"print("\n

20.
21.
22.

;return 0
}

23.


printf
.
print

. algorithm
functional .
) (: find




: int
CODE
>1. #include <iostream
>2. #include <algorithm
;3. using namespace std
4.
)(5. int main
{ 6.
;}int number[]={1,5,8,10,85,100,89

7.

;int a

8.
9.
10.

;"cout << "Enter the number\n


;cin >> a

11.

;)int* num=find(number,number+7,a

12.

;cout << "The number in\t" << (num-number) << endl

13.
14.
15.

;return 0

16.
}

17.

algorithm .

.11
12
find find


find .
13 .

) (: sort
sort
. :
CODE
>1. #include <iostream
>2. #include <algorithm
;3. using namespace std
4.
5.
)(6. int main
{ 7.
;}int number[]={1,45,80,40,-1,60,55

8.
9.
10.

)for(int a=0;a<7;a++

11.

;"cout << number[a] << "\t

12.
;cout << endl

13.

;"cout << "The array after sorting\n

14.

;)sort(number,number+7

15.
16.
17.

)for( a=0;a<7;a++

18.

;"cout << number[a] << "\t

19.
;cout << endl

20.

;return 0

21.
22.
}

23.

45

1. 1

:
55

60

80

60

-1

40

80

2. The array after sorting


55

45

40

3. -1 1


) (: count

.



:
CODE
>1. #include <iostream
>2. #include <algorithm
;3. using namespace std
4.
5.
)(6. int main
{ 7.
;}int number[]={1,40,80,40,40,60,55

8.
9.
10.

)for(int a=0;a<7;a++

11.

;"cout << number[a] << "\t

12.
;cout << endl

13.

;)int n=count(number,number+7,40

14.
15.
16.

;cout << "The times of 40 is:\n" << n << endl

17.
18.

;return 0

19.
}

20.

:
55

60

40

40

80

40

The times of 40 is:


3

40 count
3.
) (: for_each




:

CODE
>1. #include <iostream
>2. #include <algorithm
;3. using namespace std
4.
>5. template <class T
{ 6. class prin
public:

7.

)void operator() (const T& t

8.

9.
10.

; cout << t

11.

}
;}

12.

;prin <int> print

13.
14.
15.

;}Int[]={1,2,3,4,5

)(int main

16.

17.
18.

int

19.
20.
21.

;"cout << "for_each()\n

22.
23.

;)for_each(Int, Int+5, print

24.
25.

;cout << endl

26.
27.

;return 0
}

28.

prin
. 5 .12
13 .
for_each 23
print
for .


functional
.


Program Example
:



ATM

/





.

:
:



.



.


.



.




.






. Stack

.
:
CODE
>1. template <class T
2. class array
{ 3.
;int _size

4.

;int _capacity

5.

;T *arr

6.

;T *arr2

7.

;)chapter(int m

8.

;)(void allocater

9.

;)void allocater(int x

10.

;)( void alloce

11.

public:

12.

;)(array

13.

;)array(int m

14.

void save();//

15.

void load();//

16.

;)(int size

17.

;)(int capacity

18.

void erase(int x);//

19.

void push_back(T x);//

20.

void clean(); //

21.

int find (T x)const;//

22.

void operator()(int m);//

23.

array<T> operator+ ( array<T>& rhs);//

24.

;)array<T> &operator=(array<T> &rhs

25.

T &operator[](int x);//

26.

;}

27.


.
: _size

.
: _capacity


.
: arr


: arr2

.
:

. .
) : ( chapter


chapter


31
30 30 60
90 31 930
.



:
>1. template <class T
{)2. int array<T>::chapter(int m
;if (m<0) throw

3.

;else if (m<30) return 30

4.

;else if (m<60) return 60

5.

;else if (m<90) return 90

6.

;else if (m<120)return 120

7.

;else if (m<150)return 150

8.

;else if (m<180)return 180

9.

;else if (m<210)return 210

10.

;else if (m<240)return 240

11.

;else if (m<270)return 270

12.

;else if (m<300)return 300

13.

;else if (m<330)return 330

14.

;else if (m<360)return 360

15.

;else if (m<390)return 390

16.

;else if (m<420)return 420

17.

;else if (m<450)return 450

18.

;else if (m<480)return 480

19.

;else if (m<510)return 510

20.

;else if (m<540)return 540

21.

;else if (m<570)return 570

22.

;else if (m<600)return 600

23.

;else if (m<630)return 630

24.

;else if (m<660)return 660

25.

;else if (m<690)return 690

26.

;else if (m<720)return 720

27.

;else if (m<750)return 750

28.

;else if (m<780)return 780

29.

;else if (m<810)return 810

30.

;else if (m<840)return 840

31.

;else if (m<870)return 870

32.

;else if (m<900)return 900

33.

;else if (m<930)return 930

34.

;else throw

35.
}

36.



.
: array




:
>1. template <class T
)(2. array<T>::array

3.

;_size=1

4.

;)_capacity=chapter(1

5.

;]arr=new T[_capacity

6.

;arr[0]=0

7.
}

8.


.

.
1 2
. array
4 .
5 chapter
1
30 . _capacity
6
.

. 7

.
:
>1. template <class T
)2. array<T>::array(int m):_size(m),_capacity(0
{

3.

;)_capacity=chapter(m

4.

;int d=0

5.

;]arr=new T[_capacity

6.

)for (int i=0;i<_capacity;i++


;arr[i]=d

7.
8.

9.

1 2 _size

capacity 0

.

chapter . _capacity
6 arr



.
7 8
arr

.
: push_back






:
>1. template <class T
)2. void array<T>::push_back(T x
{

3.

)if(_size+1<_capacity

4.

;arr[++_size]=x

5.

};else{ allocater();arr[++_size]=x

6.
7.
}

8.

1 2
.
4
_size
.


allocater

) (
6

) ( .
: allocater



.
:

>1. template <class T


)(2. void array<T>::allocater
{

3.

;arr2=arr

4.

;_capacity+=30

5.

;]arr=new T[_capacity

6.

)for(int i=0;i<_capacity-30;i++

7.
8.

;]arr[i]=arr2[i

9.

;delete[] arr2

10.

;arr2=0

11.
12.

1 .2


.
3 arr
.


.
5
_capacity ) ( 30

.
_capacity .
6 arr
arr2 .
7 8 arr2
. arr
9 10


.


_capacity
_capacity
. 30
:
>1. template <class T
)2. void array<T>::allocater(int x
{

3.

;int m=_capacity

4.

;arr2=arr

5.

;)_capacity=chapter(x

6.

;]arr=new T[_capacity

7.

)for(int i=0;i<m;i++

8.

;]arr[i]=arr2[i

9.

;delete[] arr2

10.

;arr2=0

11.
12.
13.


allocater 4 .6


.
) (:


operator

.
) (



.
void

.
:
>1. template <class T
)2. void array<T>::operator()(int m
{

3.
4.

;arr2=arr

5.

;)_capacity=chapter(m

6.

;_size=m

7.

;]arr=new T[_capacity

8.

)for(int i=0;i<_capacity;i++

9.

;arr[i]=0

10.

;delete []arr2

11.

;arr2=0

12.
13.
14.

1 .2
5




.
6
chapter . _capacity
7
. _size
8 .
9 10 0
.
11 12
.
) = (:



.



.

.
:
>1. template <class T
)2. array<T>& array<T>::operator=(array<T> &rhs
{

3.

;)(_size=rhs.size

4.

;)(_capacity=rhs.capacity

5.

;int i=0

6.

;)( alloce

7.

)for( i=0;i<_capacity;i++

8.

;]arr[i]=rhs[i

9.

10.

;return *this

11.

1 . 2
4
.
5 _capacity
_capacity
. chapter
7 ) ( alloce

_capacity
_capacity .
.
8 9
for
_capacity _size .
10 =
. this
) (: alloce




= .
:
>1. template <class T
)( 2. void array<T>::alloce
{

3.

;delete [] arr

4.

;arr=0

5.

;]arr=new T[_capacity

6.

)for (int i=0;i<_capacity;i++

7.
8.

;arr[i]=0
}

9.

1 .2
4 5
.

.
7 8 .

] [ :





.


int .

_capacity
. _size

.
:
>1. template <class T
)2. T& array<T>::operator[](int x
{
{)if(x>_size

3.
4.

};]if (x<_capacity){_size=x+1; return arr[x

5.

else

6.

};]{allocater(x);_size=x+1;return arr[x

7.

;]else if(x<0) return arr[0

8.

;]else return arr[x

9.

10.

1 2
x . int
4
_size
arr

. arr
4 if

arr
_size
.

6
allocater

chapter
_size ) ( x .
if 4
8

-1

.

.

9
.
) (: find


.

. int
:
>1. template <class T
2. int array<T>::find (T x)const
;{int i=0
)for( i=0;i< _size;i++

3.
4.
5.

;if (x==arr[i]) return i


;return -1

6.

7.

1 . 2
] [ 4 5
.

-1 .
) (: clean

.
:
>1. template <class T
)(2. void array<T>::clean
{
;arr2=arr

3.
4.

;_capacity=30

5.

;_size=0

6.

;]arr=new T[_capacity

7.

)for(int i=0;i<_capacity;i++

8.

;arr[i]=0

9.

;delete []arr2

10.

;arr2=0

11.
12.

clean
.
) (: erase





.
74

93

50

10

12

20

2
10 .
.

74

93

50

12

20


5 .
) (. erase
:
>1. template <class T
)2. void array<T>::erase(int x
{

3.

;if ((x>_size)||(x<0)) return

4.

;_size=_size-1

5.

;arr[x]=0

6.

)for(int i=x;i<_size;i++

7.
8.

;]arr[i]=arr[i+1

9.
}

10.

int
1 . 2
4


.
5 .

7 . 8
. erase
: +






.
.
:
>1. template <class T
)2. array<T> array<T>::operator+ ( array<T>& rhs
{

3.

;)(int i=_size+rhs.size

4.

;)array<T> a(i

5.

)for(int j=0;j<_size;j++

6.

;]a[j]=arr[j

7.

;int k=0
)for(k=0,j=j;j<i;j++,k++
;]a[j]=rhs[k

8.
9.
10.
11.

;return a
}

12.

1 .2
4 i
i
.
5 a
. i
6 7 )
( . a
9 10 )
( . a


for a
for .
11 . a
) (: save


.



.
:
>1. template <class T
)(2. void array<T>::save
{

3.

;)ofstream a("file",ios::binary

4.

{)for(int i=0;i<_size;i++

5.

;) a.write( (char*) &arr[i], sizeof T

6.

7.

;)(a.close

8.
}

9.

1 2
.
4 ofstream
file
5 7
. for
8 .
) (: load




.
:
>1. template <class T
{)(2. void array<T>::load
;)ifstream a("file",ios::binary

3.

;int j=sizeof T

4.

;int i=0

5.

{))(while (!a.eof

6.

;a.read( (char*) &arr[i], sizeof T );i++

7.

8.

;)(a.close

9.
10.

;_size=i-1

11.

1 . 2
ifstream
file .
6 7 8
.







.


.
) ( size ) (: capacity


.
.
>1. template <class T
};2. int array<T>::size(){return _size
*/

*3. /

) (capacity

>4. template <class T


};5. int array<T>::capacity(){return _capacity

.
) (: main
.
)(1. int main
{ 2.
;)array <int> a(4

3.

)for(int i=0;i<a.size();i++

4.

;a[i]=i*2

5.

6.
;)array <int> b(5

7.

)for( i=0;i<b.size();i++

8.

;b[i]=i*4

9.

;)array <int> c(40

10.
11.
12.

;c=b+a

13.
14.

)for( i=0;i<c.size();i++
;cout << i << "::::\t\t" <<c[i] << endl

15.
16.
17.

;"cout << "(c):\n

18.

;cout << "size():::::::" << c.size() << endl

19.

;cout << "capacity():::" << c.capacity() << endl

20.
21.

;return 0

22.

23.
24.

:

.
<< >>
.
= .
.

.

::
( ) [ ] -> .
Sizeof & * -- ++ + - ~ !
* / %
+ >= > <
<=
8=
==
&
|
&&
||
? :
= += -= *= /= %= |= <<= >>=
throw
,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

:
auto
break
case
catch
char
class
const
continue
default
delete
do
double
else
enum
extern
float
for
friend
goto
if
int
long
mutable
new
operator
private
protected
public
register
return
short
signed
sizeof

static
struct
switch
template
this
throw
typdef
union
unsigned
virtual
void
volatile
while



The Preprocessor
:


.
#
. include
define:
define
.
:
#define MAX 50


MAX 50 :
;]int arr[MAX

:
]int arr[50

.
:

.

.

.
:
CODE
>1. #include <iostream
2. #define POWER(x) x*x
3. #define POWER3(x) x*x*x
;4. using namespace std
5.
6.

)(7. int main


{ 8.
;int a=0,b=0

9.
10.

;cout << "Enter a:\t"; cin >> a

11.

;cout << "Enter b:\t";cin >> b

12.
13.
14.

;cout << endl << endl

15.
;cout << "power:\t\t" << POWER(a) << endl

16.

;cout << "power3:\t\t" << POWER3(b) << endl

17.
18.
19.

;return 0
}

20.

define
.
16 17
POWER
.


.

sultan_altaif@yahoo.com

You might also like