Professional Documents
Culture Documents
C++ Book Arabic
C++ Book Arabic
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.
4.
5.
6.
7.
8.
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
.
: 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
.
)(1
.
Hellow Worlad
I am a programmer
) :
(
:
CODE
>1. #include <iostream
;2. using namespace std
\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
) 7- if (i > j
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-
13-
)(int main
14-
15-
16-
17-
) if (i > j
18-
19-
20-
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
) 7- if (i > j
10-
11-
12-
{ )else if ( j=i
13-
14-
else
15-
;return 0
16-
17-
10 15
else if
else
.
else/if
38-30 :
{
) 1- if (i > j
} 3-
{
)4- if (j > i
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-
9-
;cin >> a
10-
11-
;cin >> b
12-
13-
;cin >> x
141516-
1718-
21-
22-
23-
24-
else
25-
2627-
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-
6-
;char x
78-
9-
10-
cin >> a;
11-
12-
cin >> b;
13-
14-
cin >> x;
1516-
1718-
1920-
switch (x) {
21-
case '+':
22-
23-
break;
24-
case '-':
25-
26-
break;
27-
case '*':
28-
29-
break;
30-
case'/':
31-
32-
break;
33-
default:
34-
35-
3637-
383940-
return 0;
}
: switch
1- switch (x) {
2-
case '+':
3-
4-
break;
5-
case '-':
6-
;break
7-
case '*':
;cout << a*b
;break
8910-
case'/':
11-
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-
10-
1112-
13-
mark='A';
1415-
16-
mark='B';
1718-
19-
mark='C';
2021-
22-
mark='D';
2324-
25-
mark='F';
2627-
28-
29-
30-
else
31-
return 0 ;}
32-
33-
34-
3536-
return 0;
37-
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-
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-
13-
;total=total+degree
14-
;i++
15-
)if (i<5
16-
;goto ss
1718-
19-
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-
4-
;total=total+degree
5-
;i++
6-
7-
.
.
:
. do/while
CODE
>1- #include <iostream
;2- using namespace std
3-
6-
;int i=0
7-
8-
9-
10-
11-
do
12-
13-
;"\t\t
14-
;"\t\t
cout << i
15-
16-
17-
;i++
18-
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.
7.
8.
{)'while (d!='.
;cin >> d
9.
10.
11.
12.
13.
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.
8.
9.
10.
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.
11.
12.
13.
14.
15.
16.
17.
18.
;number=min
19.
20.
21.
22.
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.
9.
10.
11.
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.
12.
13.
14.
15.
16.
max=number;
17.
18.
19.
min=number;
20.
21.
22.
23.
24.
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.
8.
9.
10.
11.
12.
13.
)if (number%i==0
14.
;break
15.
16.
)if (i==1
; " are
17.
18.
19.
else
20.
21.
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.
8.
9.
10.
11.
12.
)for (int i=0 ; i<=number ; i++
13.
14.
15.
))if (!(i%2==0
16.
;continue
; cout << "The number is: " << i
17.
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.
9.
10.
11.
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.
9.
;int Abs
10.
11.
;else Abs=Number
12.
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.
;"
9.
10.
11.
;)Val=floor(Num
12.
13.
14.
;)Val=ceil(Num
15.
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.
;"
9.
10.
11.
;)Val=sqrt(Num
12.
13.
14.
; "
15.
16.
17.
;)Val=pow(Num , Val
18.
19.
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
; ,total=0 , i
; "grades of stud:\n
10
; ] total=total+stud[ i
11
12
;Avrege=total /10
13
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
;size [3]=0
6
7
;cout << " Enter the department " << j+1 << endl
10
11
12
;cout << " Employee number " << i+1 << endl
; ] cin >> employee[ i ] [ j
13
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:
22
23
for
for
for
i < 5
for
for 16 19
] size [i
size
19
sum
.sum
:
.
:
:
:
)
(
.
.
found
:
CODE
>#include <iostream.h
) (main
3
4
; index
;int search
5
6
;cout << "Please enter the mark you want it\t" << endl
9
)for ( index=0;index<10;index++
10
11
12
13
;found=1
14
;break
15
16
else
17
; found=0
18
19
; << index++
20
)if (found=1
21
22
else
23
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.
9.
10.
11.
12.
13.
14.
15.
16.
17.
;]array[j]=array[i
18.
;array[i]=x
19.
;sure=1
20.
21.
}
}
22.
23.
24.
25.
;"cout << "Here is the Array after sorted\n
26.
)for (i=0;i<5;i++
27.
28.
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.
8.
9.
;)strcpy(string1,string2
10.
11.
12.
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.
12.
13.
14.
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.
10.
))else if (isupper(m
11.
12.
13.
14.
15.
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.
11.
{ ))else if (isupper(m
12.
13.
14.
}
{ ))else if(islower(m
15.
16.
17.
18.
19.
20.
21.
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.
11.
12.
;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.
16.
;else break
17.
}
18.
{)if (sure == 4
19.
20.
;break
21.
}
22.
23.
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
;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
c=4
;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
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.
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
6.
} 7.
8.
10.
;float a
11.
;cin>> a
12.
;)double m=binate(a
13.
14.
}
15.
) ( binate 4 7
b float
. main
.
:
CODE
1.
//
10.
////////////////////////////
11.
void main() //
12.
13.
;int a
14.
;cin>> a
15.
;)TheMain(a
16.
)for(int i=0;i<times;i++
17.
18.
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.
32.
;if(x%i==0) ++times
}
33.
/////////////////////
34.
)TheMain(int x
35.
36.
;)HowTimes(x
37.
;)(pointer
38.
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.
10.
;cin >> i
11.
12.
;cin >> j
13.
14.
15.
16.
;)k=rate (i ,j,avg
17.
18.
19.
}
20.
21.
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
13
14
plus
.
C++
..
:
...
...
.
:
.
.
)
(
.
:
;) ][ void arraysFunction (int
) ( main
:
CODE
>1. #include <iostream
;2. using namespace std
3.
8.
9.
10.
11.
)for ( i=0;i<5;i++
12.
13.
14.
;return 0
15.
16.
17.
)][void arraysf(int m
18.
19.
20.
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.
8.
9.
;cin >> n
10.
;)function (n
11.
12.
13.
;return 0
}
14.
15.
16.
17.
18.
;i++
19.
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.
9.
10.
;cin >> i
11.
;)int x=fact (i
12.
13.
14.
15.
;return 0
}
16.
17.
18.
19.
20.
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.
10.
;cin >> i
11.
12.
;endl
;)i=fib (i
13.
14.
15.
16.
;return 0
}
17.
18.
19.
20.
21.
)if ( x<3
22.
;return 1
23.
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.
15.
16.
17.
18.
19.
20.
21.
22.
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.
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
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
;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.
11.
12.
13.
14.
15.
16.
17.
18.
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
11.
double Num1,Num2,Value;
12.
char Operator;
13.
14.
15.
";cin>>Num1;
16.
17.
";cin>>Operator;
";cin >>Num2;
18.
19.
switch (Operator) {
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
return 0;
}
32.
33.
34.
35.
return a+b;
36.
37.
38.
39.
return a-b;
40.
41.
42.
43.
44.
return a*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
Stack
heap
heap
:
int flaot .... double .void
void
.
.
:
.
static
0 .NULL
.
Introduction to Object Oriented
Programming
:
...
..
-
.
:
..
.
.. ..
.
:
....
..
..
...
:
.
) (
: Attributes . Behaviors
:
.
:
) (
)
( )
(
:
)
(
:
:
:
:
.
:
:
..
.
..
..
.
.
. 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
10
;}
11
)maths::GetNum1Num2(float i,float j
12
13
;itsNum1=i
14
;itsNum2=j
15
16
)(maths::print
17
18
19
20
21
22
23
24
) ( int main
25
26
;float i,j
27
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
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
23
24
25
26
)(First::Loop
27
28
)for (counterd1=0;counterd1<d1;counterd1++
29
)for (counterd2=0;counterd2<d2;counterd2++
30
31
32
33
)(First::print
34
35
)for (counterd1=0;counterd1<d1;counterd1++
36
)for (counterd2=0;counterd2<d2;counterd2++
37
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
10
;}
11
)maths::maths(float i,float j
12
13
;itsNum1=i
14
;itsNum2=j
15
16
)(maths::print
17
18
19
20
21
22
23
24
) ( int main
25
26
;float i,j
27
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.
13.
; )(a.address
14.
15.
; )(b.address
16.
17.
; )(c.address
18.
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:
) (First
};counter++
;}
10
int First::counter=0;//
11
)(void main
12
13
;First a
14
;First b
15
;]First c[60
16
17
18
First
62 . counter
17 getCounter
) a ( c counter
62 .
) ( )
11 (
.
.
.
CODE
>#include <iostream.h
class First
3
4
public:
) (First
};counter++
10
11
;}
12
13
;int First::counter=15
14
)(void main
15
16
17
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
17
};)setItsAvreg(double x){itsData.setItsAvreg(x
18
;}
19
20
)(int main
21
22
;Student a
23
;)a.setItsAvreg(98
24
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++)
}
}
Array::Bigfunction()
{
if (itsD1==itsD2)
{
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
)(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
21
22
for ( index2=0;index2<256;index2++
23
24
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.
37.
38.
printfibancci()
39.
/* * /
40.
41.
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
;return x+m
6
7
;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
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
;)int j(m
6
7
8
9
;)int dd(d
10
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
;
; << endl
;
:
CODE
)(void main
{
;int i=12;float g=13;long k=15
;num first=i,second=g,third=k
;
; << endl
;
.
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
; << endl
7
;cout << first.itsNum << endl
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:
10
11
12
13
14
};
15
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
26
<< endl ;
27
28
29
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:
10
11
12
13
14
15
16
;}
17
18
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
37
38
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
8
9
. operator ++
. first operator ++
.
:
;)num second(++first
:
operator ++ first
temp
second .
. num
.
++ .
.
.
:this
this .
.
) ( operator ++ :
)( num num::operator ++
;)++(*itsNum
;return *this
num .
).(- -
:
num
38 :
38
.
first second
first second . first
(operator ++
) itsAge
first
itsAge first
.second :
)m
;)num temp(*this
;++itsNum
;return temp
itsNum
.
.
.
:
CODE
>#include <iostream.h
1
2
class num
;double *itsNum
public:
10
11
12
13
14
15
16
17
18
19
};
20
21
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
44
45
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 +
:
main :
num One,Two=2,Three=3
;One= Two+Three
num
.
:
2
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.
10.
11.
~num () { }
12.
13.
14.
15.
16.
17.
18.
19.
};
20.
21.
22.
23.
return num
24.
((itsNum)+rhs.GetItsNum());
25.
26.
27.
28.
29.
30.
itsNum=rhs.GetItsNum();
31.
32.
33.
34.
++itsNum;
35.
36.
37.
return *this;
38.
39.
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
:
.num
+
.
) = (.
)=(:
)=(.
. num
= num
num
) (
.
.
CODE
>1. #include <iostream.h
2.
3. class Bird
{ 4.
;int *itsAge
5.
6. public:
7.
8.
9.
10.
11.
12.
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.
23.
24.
.
)=(
)Bird & operator = (const Bird &rhs
25.
bird
) (: main
;b=a
26.
;)b.operator =(a
27.
a b
operator a
:
{
28.
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.
48.
.num
num :
};num() {itsNum=new double ;itsNum=0
10
11
12
.
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.
9.
10.
11.
;)(operator int
12.
;}
13.
14.
15.
;)(itsNum=rhs.GetItsNum
16.
17.
18.
)(num::operator int
**
**
**
**
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.
29.
24 :
:
) ( operator int first
. j
24 :
)(num::operator int
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.
17.
18.
19.
;if(x>number) return 0
20.
21.
22.
23.
;}
24.
25.
)(int main
26.
27.
28.
;array a
29.
30.
)for(int i=0;i<10;i++
31.
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.
18.
/*
19.
20.
*/
/*
22.
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.
28.
29.
30.
};
31.
32.
33.
34.
*/
*/
35.
36.
37.
;E >> temp.up
38.
;char c
39.
;)E.get(c
40.
41.
;E >> temp.down
42.
;return E
43.
44.
45.
46.
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
;return temp
8
9
+
.
.
1
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
: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
{ 5.
;"6. string S1= "languge Java
;"7. string S2= "Languge C++
10.
;)S1.swap(S2
11.
12.
13.
;S2=S1+S2
14.
15.
;return 0
16.
17.
:
languge Java
string1:
Languge C++
string2:
After swaping
Languge C++
string1:
languge Java
string2:
S2=S1+S2:
string .
: string
cin
. cout
getline
cin string
'.'\n
: S1
CODE
>1. #include <iostream
8.
9.
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.
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.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
;)" S1.insert(0,"C++ Languge
21.
22.
23.
24.
25.
26.
;return 0
}
27.
28.
29.
:
a lot of programmers
1. string S1
20
2. S1.size
31
)(3. S1.capacity
4.
5. string S1
38
6. S1.size
63
)(7. S1.capacity
8.
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.
9.
10.
11.
;)"int p=S1.find("Java
12.
13.
;)string S2(S1,p,4
14.
15.
16.
17.
;)"S1.replace(p,S2.size(),"C++
18.
19.
20.
21.
;return 0
}
22.
7.
;"string S2="C++
8.
9.
10.
11.
12.
;)"int p=S1.find("Java
13.
;)S1.erase(p,4
14.
;)S1.insert(p,S2
15.
16.
17.
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.
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.
11.
12.
13.
;}
14.
15.
16.
{ public:
17.
18.
19.
;}
20.
21.
)(void main
22.
23.
;son you
24.
25.
}
26.
:
the Father ALIVE
1.
2.
3.
4.
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.
14.
15.
public:
16.
17.
18.
19.
20.
; }
21.
int
) ( main :
1
; son
:
I am alive
son
Hellow
.
:
1
;)son(5
:
)son(int y): father(y
22.
23.
24.
22
5
.
:
A .a .int
a .
a int int
) (
.
:
} { )a (int x): A(int x
.
:
.
) (
.
CODE
1. class shape
{ 2.
3. protected:
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.
20.
21.
22.
23.
;cout <<endl
24.
25.
26.
27.
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.
12.
13.
14.
15.
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.
7.
8.
9.
10.
11.
12.
;}
13.
14.
15.
16.
public:
17.
18.
19.
20.
21.
;}
22.
23.
)(int main
24.
25.
26.
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.
7.
8.
9.
10.
protected:
11.
int itsAge;
12.
13.
};
14.
15.
16.
17.
public:
18.
19.
20.
21.
22.
};
23.
24.
int main()
25.
26.
27.
;)(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.
9.
Employee(int x):itsAge(x)
{cout <<"\nHii
I am
Employee(int)
\n";}
10.
11.
I am I am Employee\n";}
};
12.
13.
class prof
14.
{protected:
15.
16.
int itsAge1;
public:
17.
18.
prof(int
x):itsAge1(x)
{cout
<<"\nHii
am
prof(int)
\n";}
19.
20.
I am I am prof\n";}
};
21.
22.
23.
{public:
24.
25.
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.
5.
6.
7. protected:
8.
int itsAge;
9. };
10.
11.
12.
public:
13.
14.
Employee(int
x):person(x)
{cout
<<"\nHii
am
Employee(int) \n";}
15.
16.
I am I am Employee\n";}
};
17.
18.
19.
20.
public:
21.
22.
23.
Getme(){cout <<"\nHiii
24.
I am I am prof\n";}
};
25.
26.
27.
{public:
28.
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.
8.
9.
10.
11.
GetItsAge(){return itsAge;}
protected:
12.
int itsAge;
13.
};
14.
15.
16.
public:
17.
18.
Employee(int
x):person(x+2)
{cout
<<"\nHii
am
Employee(int) \n";}
19.
20.
I am I am Employee\n";}
};
21.
22.
23.
24.
public:
25.
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.
32.
{public:
33.
34.
\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.
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.
9.
10.
{d1=d3=x;d2=d4=y;
11.
12.
13.
14.
15.
16.
d1=a;d2=b;d3=c;d4=d;
17.
cout
<<
"\nHere
am
SHAPE
(INT
INT
,INT)\n";
18.
19.
20.
21.
22.
};
23.
24.
25.
26.
public:
27.
28.
29.
30.
31.
, INT) \n";}
INT
{ )(void draw
32.
33.
34.
35.
36.
;cout <<endl
37.
38.
39.
40.
41.
;}
42.
43.
;"
44.
45.
public:
46.
47.
};itsd=d1
48.
49.
50.
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.
10.
;)(void Enter
11.
;}
12.
linklist
.
8 ) ( additem
.
9 .
10 find .
11 Enter
.
) (:additem
:
} 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.
3.
4.
5.
6.
7.
8.
9.
) (:Display
:
)(1. void linklist::display
{
2.
3.
4.
5.
6.
{
;cout <<"Number Of Course:\t" << temp->number << endl
7.
8.
9.
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.
;"\n
;m++
12.
};break
13.
;temp = temp->next
14.
15.
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.
17.
;cin >> m
18.
19.
;)li.additem(m
20.
;)(li.Enter
21.
22.
;break
23.
case 'b':
24.
;int n
25.
);;(for
26.
27.
28.
;cin >> n
29.
30.
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.
11.
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.
24.
25.
26.
27.
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.
9.
10.
11.
12.
;return 0
13.
14.
. 3
> <
Fraction .
struct
. class
:
.
.
.
) (
.
.
:
CODE
>1. #include <iostream
;2. using namespace std
3. /**********************************/
6.
T element;
7.
link* next;
8.
};
9. /*********************************/
10.
11.
12.
private:
13.
14.
public:
15.
linklist()
16.
{ first = NULL; }
17.
18.
void display();
19.
};
20.
/**********************************/
21.
22.
23.
24.
newlink->element = x;
25.
newlink->next = first;
26.
first = newlink;
27.
28.
/*************************************/
29.
30.
31.
32.
33.
34.
35.
36.
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.
50.
51.
52.
)switch (choice
53.
54.
case 'a':
55.
);;( for
56.
57.
58.
;cin >> m
59.
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.
10.
;int getNumber()const
11.
; float getGrade()const
12.
; int getHours()const
13.
14.
15.
16.
17.
18.
19.
20.
;}
21.
22.
23.
24.
25.
{number=rhs.getNumber();
26.
grade=rhs.getGrade();
27.
hours=rhs.getHours();
28.
29.
30.
31.
32.
33.
34.
35.
36.
37.
number=rhs.getNumber();
38.
grade=rhs.getGrade();
39.
hours=rhs.getHours();
40.
return *this;
41.
42.
43.
//////////////////////////////////////
44.
45.
46.
47.
48.
49.
50.
return E;
51.
52.
///////////////////////////////////////
53.
54.
55.
endl;
56.
57.
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.
11.
;cin >> b
12.
13.
;)(course.display
14.
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-
8-
910-
11-
{ try
12-
)if(TheYear <= 0
13-
;throw
14-
15-
1617-
{)catch(...
18-
1920-
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.
8.
;)cin.exceptions(cin.failbit
9.
10.
11.
12.
{ try
;cin >> TheYear
13.
)if(TheYear <= 0
14.
15.
; "throw "Hellow
16.
17.
18.
19.
20.
{)catch(...
21.
22.
23.
24.
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.
8.
;)cin.exceptions(cin.failbit
9.
10.
11.
12.
{ try
;cin >> TheYear
13.
)if(TheYear <= 0
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
{)catch(...
25.
26.
27.
28.
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.
15.
16.
Course=new float[NumberOfCourse];
17.
18.
19.
try{
20.
21.
22.
23.
24.
Total=+Course[i];
25.
26.
catch(...)
27.
28.
29.
cin.clear();
30.
char Bad[5];
31.
32.
i--;
33.
34.
35.
36.
37.
Avg=Total/NumberOfCourse;
38.
39.
40.
41.
42.
43.
catch(...)
44.
45.
46.
47.
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.
15.
}
16.
;}{class xThrow
17.
;}{class sThrow
18.
;}
19.
20.
)(int main
21.
22.
23.
{);;(for
24.
{try
25.
26.
;cin >> i
27.
28.
29.
{)catch(Stack::sThrow
30.
31.
32.
{)catch(Stack::xThrow
33.
34.
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.
10.
)getElement(int x
11.
12.
13.
if (x>100)
14.
throw xThrow();
15.
16.
17.
18.
class xThrow{public:
19.
xThrow(){}
20.
xfalse(){
21.
};
22.
class sThrow{
23.
public:
24.
sThrow(){}
25.
26.
27.
};
};
28.
29.
int main()
30.
31.
32.
for(;;){
33.
try{
34.
35.
cin >> i;
36.
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 .
>#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
10.
;string chara2
11.
;)"ifstream fin("data.txt
12.
13.
14.
15.
16.
17.
18.
;return 0
19.
20.
ifstream
fin
.
:
""\n
cin
getline :
CODE
>1. #include <fstream
9.
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.
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.
10.
;Array[i]=j
11.
12.
)for (i=0;i<40;i++
13.
14.
15.
16.
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.
10.
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.
13.
14.
15.
16.
17.
}
;}
18.
19.
)(void main
20.
21.
;Stud Ahmed
22.
;)(Ahmed.Data
23.
24.
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.
13.
14.
15.
16.
17.
}
;}
18.
19.
)(void main
20.
21.
;Student Saeed
22.
23.
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.
15.
16.
17.
18.
}
19.
)(DisplayData
20.
21.
22.
23.
24.
25.
26.
27.
28.
};
29.
30.
void main()
31.
32.
char ch;
33.
Student Ahmed;
34.
fstream file;
35.
36.
37.
ios::in | ios::binary );
38.
do
39.
40.
41.
Ahmed.GetData();
42.
43.
44.
45.
46.
47.
while(ch=='y');
48.
49.
file.seekg(0);
50.
51.
52.
while( !file.eof() )
53.
54.
55.
Ahmed.DisplayData();
56.
57.
58.
59.
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.
16.
17.
18.
19.
20.
DisplayData()
21.
22.
23.
24.
25.
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.
40.
41.
42.
his data\n";
43.
cin >> x;
44.
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.
16.
17.
18.
19.
20.
DisplayData()
21.
22.
23.
24.
25.
26.
27.
DataIn(int x)
28.
29.
ifstream infile;
30.
infile.open("Student.dat", ios::binary);
31.
infile.seekg( x*sizeof(Student) );
32.
33.
34.
DataOut()
35.
36.
ofstream outfile;
37.
38.
39.
40.
41.
42.
43.
ifstream infile;
44.
infile.open("Student.dat", ios::binary);
45.
infile.seekg(0, ios::end);
46.
47.
48.
;}
49.
50.
)(int main
51.
52.
;Student Ahmed
53.
;char x
54.
55.
{ do
56.
57.
;)(Ahmed.GetData
58.
;)(Ahmed.DataOut
59.
60.
;cin >> x
61.
62.
;)'while(x=='y
63.
;)(int n = Student::ManyInFile
64.
;"cout << "There are " << n << " persons in file\n
65.
66.
67.
68.
;)Ahmed.DataIn(j
69.
;)(Ahmed.DisplayData
70.
71.
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.
10.
11.
12.
13.
14.
15.
;"<< ":\t
;cin >> k
16.
;if (k==0)break
17.
;)v.push_back(k
18.
19.
20.
;)(int j=v.size
21.
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.
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.
12.
13.
14.
15.
16.
17.
;"<< ":\t
;cin >> k
18.
;if (k==0)break
19.
;)v.push_back(k
20.
}
21.
22.
;)(int j=v.size
23.
24.
25.
26.
27.
;)total+=(*ci
28.
;avg=total/j
29.
;cout << endl << endl
30.
;cout << "Your total of grades is:\t\t" << total << endl
31.
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.
9.
10.
11.
list1.push_back( i );
12.
13.
list2.push_back( i );
14.
//list1: 0, 2, 4, 6
//list2: 0, 3, 6, 9, 12
15.
16.
for(list<int>::const_iterator c=list1.begin();
c!=list1.end();
17.
++c)
18.
19.
20.
21.
22.
for(list<int>::const_iterator a=list2.begin();
a!=list2.end();
23.
++a)
24.
cout <<
25.
*a <<"\t";
26.
27.
list1.reverse();
// list1: 6 4 2 1
28.
list1.merge(list2);
//list1+=list2
29.
30.
31.
) )(while( !list1.empty
32.
33.
34.
;)(list1.pop_front
35.
36.
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
) (.
) (
.
.
.
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.
23.
24.
25.
26.
{)'if(sure=='y
;cin >> a
27.
};)names.insert(a
28.
29.
30.
31.
32.
);;(for
33.
34.
35.
36.
{)'if(sure=='y
37.
38.
;cin >> a
39.
;)i=names.find(a
40.
41.
42.
43.
44.
45.
}
46.
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.
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.
22.
;cin >> a
23.
;]x = mapTel[a
24.
25.
26.
27.
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.
8.
9.
10.
; cout << t
11.
}
;}
12.
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.
11.
;)int* num=find(number,number+7,a
12.
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.
12.
;cout << endl
13.
14.
;)sort(number,number+7
15.
16.
17.
)for( a=0;a<7;a++
18.
19.
;cout << endl
20.
;return 0
21.
22.
}
23.
45
1. 1
:
55
60
80
60
-1
40
80
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.
12.
;cout << endl
13.
;)int n=count(number,number+7,40
14.
15.
16.
17.
18.
;return 0
19.
}
20.
:
55
60
40
40
80
40
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.
8.
9.
10.
; cout << t
11.
}
;}
12.
13.
14.
15.
;}Int[]={1,2,3,4,5
)(int main
16.
17.
18.
int
19.
20.
21.
22.
23.
24.
25.
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.
11.
public:
12.
;)(array
13.
;)array(int m
14.
void save();//
15.
void load();//
16.
;)(int size
17.
;)(int capacity
18.
19.
20.
void clean(); //
21.
22.
23.
24.
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.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
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.
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
.
:
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.
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.
5.
else
6.
};]{allocater(x);_size=x+1;return arr[x
7.
8.
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.
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.
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.
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.
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
.
) (: 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.
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.
18.
19.
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.
9.
10.
11.
12.
13.
14.
15.
;cout << "power:\t\t" << POWER(a) << endl
16.
17.
18.
19.
;return 0
}
20.
define
.
16 17
POWER
.
.
sultan_altaif@yahoo.com