Title: ??sa???? st? C
1??sa???? st? C
??d??? T?µata ???ep?????????? ?????aµµa
?etapt???a??? Sp??d?? ?ad????e?t??????a?
2????? p????aµµat?sµ??
- ??µ?µ???? p????aµµat?sµ??
- (structured programming)
- ??t??e?µe??st?ef?? p????aµµat?sµ??
- (object oriented programming)
??d??? T?µata ???ep?????????? - ??sa???? st?
C ?????aµµa ?etapt???a??? Sp??d??
?ad????e?t??????a?
3????? p????aµµat?sµ??
??µ?µ???? p????aµµat?sµ??
- ??ad??ast???? (procedural) ???sse? p????aµµat?sµ??
- ?????aµµa ? ??sta e?t???? (instructions)
- ?e???a p?????µµata ? ?p?p?????µµata
s??a?t?se?? (functions) ?p????t??e?
(subroutines) d?ad??as?e? (procedures)
- ?µad?p???s? ?p?p????aµµ?t?? se modules
??d??? T?µata ???ep?????????? - ??sa???? st?
C ?????aµµa ?etapt???a??? Sp??d??
?ad????e?t??????a?
4????? p????aµµat?sµ??
??µ?µ???? p????aµµat?sµ??
? ????p????t?ta!!!
??d??? T?µata ???ep?????????? - ??sa???? st?
C ?????aµµa ?etapt???a??? Sp??d??
?ad????e?t??????a?
5????? p????aµµat?sµ??
??µ?µ???? p????aµµat?sµ??
- ?µfas? st?? e????e?e?, ??? sta ded?µ??a
- ?ed?µ??a p??spe??s?µa ap? ??e? t?? s??a?t?se??
- ?etaß??? t?? e?d??? t?? ded?µ???? ?
- ???a?? ???? (!) t?? s??a?t?se??
- ??µ??????a ???? t?p?? ded?µ????
??d??? T?µata ???ep?????????? - ??sa???? st?
C ?????aµµa ?etapt???a??? Sp??d??
?ad????e?t??????a?
6????? p????aµµat?sµ??
??t??e?µe??st?ef?? p????aµµat?sµ??
- S??d?asµ?? ded?µ???? ?a? s??a?t?se?? p??
- e?e????? se a?t? se ??a a?t??e?µe??
- S??a?t?se?? µ??? t?? a?t??e?µ????
- ?p?????? ?a? e???????s? t?? ded?µ????
??d??? T?µata ???ep?????????? - ??sa???? st?
C ?????aµµa ?etapt???a??? Sp??d??
?ad????e?t??????a?
7?a?a?t???st??? ?S ???ss??
- ??µ??????a ???? t?p?? ded?µ????
- ????µ??f?sµ?? ?a? ?p??ßas?
??d??? T?µata ???ep?????????? - ??sa???? st?
C ?????aµµa ?etapt???a??? Sp??d??
?ad????e?t??????a?
8?a?a?t???st??? ?S ???ss??
??t??e?µe?a
- St???e?a t?? pe??ß?????t?? ???st?-?/?
- ?atas?e??? p????aµµat?st????
- ??p?? ded?µ???? ???sµ???? ap? t? ???st?
??d??? T?µata ???ep?????????? - ??sa???? st?
C ?????aµµa ?etapt???a??? Sp??d??
?ad????e?t??????a?
9?a?a?t???st??? ?S ???ss??
???e??
- S??????? pa??µ???? a?t??e?µ????
- ???e a?t??e?µe?? e??a? µ???? µ?a? t????
??d??? T?µata ???ep?????????? - ??sa???? st?
C ?????aµµa ?etapt???a??? Sp??d??
?ad????e?t??????a?
10?a?a?t???st??? ?S ???ss??
???????µ???t?ta
- S??????? pa??µ???? a?t??e?µ????
a????? (base) ?a? pa?????e? (derived) t??e??
- S??s? pa??????? t???? ?a? a?t??e?µ????
- ?µad?p???s? ?????? d?ad??as???
??d??? T?µata ???ep?????????? - ??sa???? st?
C ?????aµµa ?etapt???a??? Sp??d??
?ad????e?t??????a?
11?a?a?t???st??? ?S ???ss??
?pa?a???s?µ?p???s?
- ???s? ?p?????sa? t???? se ???? p????aµµa
- ???????µ???t?ta ? e?e????a
??d??? T?µata ???ep?????????? - ??sa???? st?
C ?????aµµa ?etapt???a??? Sp??d??
?ad????e?t??????a?
12?a?a?t???st??? ?S ???ss??
????µ??f?sµ?? ?a? ?p??ßas?
- ??e????sa s????t?s? a?????? t?? ded?µ????
- ?e?est?? ?p??ßas?? (overloading)
??d??? T?µata ???ep?????????? - ??sa???? st?
C ?????aµµa ?etapt???a??? Sp??d??
?ad????e?t??????a?
13??µ??????a e?te??s?µ?? p?????µµat??
- S????af? p??a??? ??d??a
- (source code)
- ?eta???tt?s? se a?t??e?µe???? ??d??a
- (object code)
- S??des? (linking) se e?te??s?µ? ??d??a
- (executable code)
??d??? T?µata ???ep?????????? - ??sa???? st?
C ?????aµµa ?etapt???a??? Sp??d??
?ad????e?t??????a?
14??µ??????a e?te??s?µ?? p?????µµat??
// my first program include ltiostream.hgt void
main( ) cout ltlt ...
??d??? T?µata ???ep?????????? - ??sa???? st?
C ?????aµµa ?etapt???a??? Sp??d??
?ad????e?t??????a?
15????? p????aµµat?sµ?? µe t? C
??µ? p?????µµat??
- ???e?a ?efa??d?? (header files)
??d??? T?µata ???ep?????????? - ??sa???? st?
C ?????aµµa ?etapt???a??? Sp??d??
?ad????e?t??????a?
16??µ? p?????µµat?? t?? C
?d???e? p??epe?e??ast?
s?????
a??e?a ?efa??d??
// my first program include ltiostream.hgt void
main( ) cout ltlt "..."
?
?e??? d?ast?µata
s????t?s?
p??tas?
??d??? T?µata ???ep?????????? - ??sa???? st?
C ?????aµµa ?etapt???a??? Sp??d??
?ad????e?t??????a?
17????? p????aµµat?sµ?? µe t? C
?etaß??t??
- ????s? ?a? ???sµ?? µetaß??t??
??d??? T?µata ???ep?????????? - ??sa???? st?
C ?????aµµa ?etapt???a??? Sp??d??
?ad????e?t??????a?
18????? p????aµµat?sµ?? µe t? C
??p?? µetaß??t??
a???????e? d?af???? (escape sequences) \a bell \b
backspace \f feed \n new line \r carriage
return \t tab \\ backslash \ single
quotation \ double quotation \xdd hexadecimal
representation
??d??? T?µata ???ep?????????? - ??sa???? st?
C ?????aµµa ?etapt???a??? Sp??d??
?ad????e?t??????a?
19????? p????aµµat?sµ?? µe t? C
??p?? µetaß??t??
??d??? T?µata ???ep?????????? - ??sa???? st?
C ?????aµµa ?etapt???a??? Sp??d??
?ad????e?t??????a?
20????? p????aµµat?sµ?? µe t? C
??p?? µetaß??t?? (32bit)
Type Size (bits) Range Sample
applications unsigned char 8 0 lt X lt
255 Small numbers and full PC character
set char 8 -128 lt X lt 127 Very small numbers
and ASCII characters short int 16 -32,768
lt X lt 32,767 Counting, small
numbers, loop control unsigned int 32 0 lt
X lt 4,294,967,295 Large numbers and
loops int 32 -2,147,483,648 lt X lt
2,147,483,647 Counting, small numbers, loop
control unsigned long 32 0 lt X lt
4,294,967,295 Astronomical distances enum 32 -2,
147,483,648 lt X lt 2,147,483,647 Ordered sets of
values long 32 -2,147,483,648 lt X lt
2,147,483,647 Large numbers, populations float 32
1.18 10-38 lt X lt 3.40 1038 Scientific
(7-digit) precision double 64 2.23 10-308 lt
X lt 1.79 10308 Scientific (15-digit)
precision long double 80 3.37 10-4932 lt X lt
1.18 104932 Financial (18-digit) precision
??d??? T?µata ???ep?????????? - ??sa???? st?
C ?????aµµa ?etapt???a??? Sp??d??
?ad????e?t??????a?
21????? p????aµµat?sµ?? µe t? C
??s?d?? - ???d??
??d??? T?µata ???ep?????????? - ??sa???? st?
C ?????aµµa ?etapt???a??? Sp??d??
?ad????e?t??????a?
22??s?d?? - ???d??
// calculation of circle area include
ltiostream.hgt void main( ) float radius,
area const float PI3.14159 cout ltlt "Please,
give the circle radius " cin gtgt
radius areaPIradiusradius cout ltlt "The
area is " ltlt area
??d??? T?µata ???ep?????????? - ??sa???? st?
C ?????aµµa ?etapt???a??? Sp??d??
?ad????e?t??????a?
23??s?d?? - ???d??
// calculation of circle area include
ltiostream.hgt define PI 3.14159 void main(
) float radius, area cout ltlt "Please, give
the circle radius " cin gtgt radius areaPIrad
iusradius cout ltlt "The area is " ltlt area
??d??? T?µata ???ep?????????? - ??sa???? st?
C ?????aµµa ?etapt???a??? Sp??d??
?ad????e?t??????a?
24???f?p???s? e?s?d?? - e??d??
// calculation of circle characteristics include
ltiostream.hgt define PI 3.14159 void main( )
float radius,area,circum cout ltlt "Please,
give the circle radius " cin gtgt radius
cout ltlt "The circle with radius " ltlt radius ltlt
endl areaPIradiusradius cout ltlt "has an
area of " ltlt area ltlt endl circum2PIradius
cout ltlt "and a circumference of " ltlt circum
??d??? T?µata ???ep?????????? - ??sa???? st?
C ?????aµµa ?etapt???a??? Sp??d??
?ad????e?t??????a?
25???f?p???s? e?s?d?? - e??d??
// calculation of circle characteristics include
ltiostream.hgt include ltiomanip.hgt define PI
3.14159 void main( ) float
radius,area,circum cout ltlt "Please, give the
circle radius " cin gtgt radius
areaPIradiusradius circum2PIradius
cout ltlt left ltlt setw(24) ltlt "The circle with
radius " ltlt fixed ltlt right ltlt setw(12) ltlt
setprecision(3) ltlt radius ltlt endl ltlt left
ltlt setw(24) ltlt "has an area of " ltlt fixed
ltlt right ltlt setw(12) ltlt setprecision(3) ltlt area
ltlt endl ltlt left ltlt setw(24) ltlt "and a
circumference of " ltlt fixed ltlt right ltlt
setw(12) ltlt setprecision(3) ltlt circum ltlt endl
??d??? T?µata ???ep?????????? - ??sa???? st?
C ?????aµµa ?etapt???a??? Sp??d??
?ad????e?t??????a?
26????? p????aµµat?sµ?? µe t? C
?etat??p? t?p?? µetaß??t??
??d??? T?µata ???ep?????????? - ??sa???? st?
C ?????aµµa ?etapt???a??? Sp??d??
?ad????e?t??????a?
27?etat??p? t?p?? µetaß??t??
include ltiostream.hgt void main( ) int
var11000000, var2 var2(var11000000)/1000000
cout ltlt "var2 " ltlt var2 ltlt endl
??d??? T?µata ???ep?????????? - ??sa???? st?
C ?????aµµa ?etapt???a??? Sp??d??
?ad????e?t??????a?
28?etat??p? t?p?? µetaß??t??
include ltiostream.hgt void main( ) int
var11000000, var2 var2(float(var1)1000000)/
1000000 cout ltlt "var2 " ltlt var2 ltlt endl
??d??? T?µata ???ep?????????? - ??sa???? st?
C ?????aµµa ?etapt???a??? Sp??d??
?ad????e?t??????a?
29????? p????aµµat?sµ?? µe t? C
?as???? te?est??
- ?e?est?? a???s?? ?a? µe??s??
- ???e?? ?efa??da? ltmath.hgt
??d??? T?µata ???ep?????????? - ??sa???? st?
C ?????aµµa ?etapt???a??? Sp??d??
?ad????e?t??????a?
30?????? ?a? ?p?f?se??
?? p??t?se?? e?????? pe???????
??????? pa?ast?se?? p?? pe???aµß?????
s?s?et?st????? te?est?? ?a? e??a?
a???e?? ? ?e?de??
??d??? T?µata ???ep?????????? - ??sa???? st?
C ?????aµµa ?etapt???a??? Sp??d??
?ad????e?t??????a?
31?????? for
for(a????? t?µ?pa??stas? e??????pa??stas?
µetaß????) s?µa ß?????
?a??de??µa for(int i0ilt100i) cout ltlt i
ltlt endl
??d??? T?µata ???ep?????????? - ??sa???? st?
C ?????aµµa ?etapt???a??? Sp??d??
?ad????e?t??????a?
32?????? while
while(pa??stas? e??????) s?µa ß?????
?a??de??µa int i0 while(ilt100) cout ltlt i
ltlt endl i
??d??? T?µata ???ep?????????? - ??sa???? st?
C ?????aµµa ?etapt???a??? Sp??d??
?ad????e?t??????a?
33?????? do-while
do s?µa ß????? while(pa??stas?
e??????)
?a??de??µa int i0 do cout ltlt i ltlt
endl i while(ilt100)
??d??? T?µata ???ep?????????? - ??sa???? st?
C ?????aµµa ?etapt???a??? Sp??d??
?ad????e?t??????a?
34?p?fas? µe t?? p??tas? if
if (pa??stas? e??????) s?µa if p??tas??
?a??de??µa for(int i0ilt10i) if ((i
3) 0) cout ltlt i ltlt is a multiple of 3
ltlt endl
??d??? T?µata ???ep?????????? - ??sa???? st?
C ?????aµµa ?etapt???a??? Sp??d??
?ad????e?t??????a?
35?p?fas? µe t?? p??tas? if-else
if (pa??stas? e??????) s?µa if
p??tas?? else s?µa else
p??tas??
?a??de??µa for(int i0ilt10i) if ((i 3)
0) cout ltlt i ltlt is cout ltlt i ltlt is a
multiple of 3 ltlt endl else not a multiple of
3 ltlt endl
??d??? T?µata ???ep?????????? - ??sa???? st?
C ?????aµµa ?etapt???a??? Sp??d??
?ad????e?t??????a?
36?p?fas? µe t?? p??tas? switch
switch (µetaß??t? a???a?? ? ?a?a?t??a) case
1 p??tas? ..
break case 2 p??tas?
.. break default
p??tas? .. p??tas?
??d??? T?µata ???ep?????????? - ??sa???? st?
C ?????aµµa ?etapt???a??? Sp??d??
?ad????e?t??????a?
37?p?fas? µe t?? p??tas? switch
?a??de??µa switch (c) case y cout ltlt
you have pressed \y\ ltlt endl break
case Y cout ltlt you have pressed \Y\
ltlt endl break default cout ltlt c ltlt
is not a valid choice! ltlt endl
??d??? T?µata ???ep?????????? - ??sa???? st?
C ?????aµµa ?etapt???a??? Sp??d??
?ad????e?t??????a?
38?p?fas? µe pa??stas? ?p? s??????
ap?t??esµa(pa??stas? e??????)?pa??stas?
1pa??stas? 2
?a??de??µa min (alphaltbeta)? Alpha beta
??d??? T?µata ???ep?????????? - ??sa???? st?
C ?????aµµa ?etapt???a??? Sp??d??
?ad????e?t??????a?
39?? p??t?se?? break ?a? continue
- ? p??tas? break p???a?e? ???d? ap? ??a ß????
- ? p??tas? continue µetaf??e? t? ??? t??
p?????µµat?? st?? a??? e??? ß?????
??d??? T?µata ???ep?????????? - ??sa???? st?
C ?????aµµa ?etapt???a??? Sp??d??
?ad????e?t??????a?
40??µ?? (Structures)
???sd????sµ?? µ?a? d?µ??
struct ???µa d?µ?? µ??? d?µ??
?a??de??µa struct Course int CourseCode int
Semester int Instructor
??d??? T?µata ???ep?????????? - ??sa???? st?
C ?????aµµa ?etapt???a??? Sp??d??
?ad????e?t??????a?
41??µ?? (Structures)
???sµ?? µetaß??t?? t?p?? d?µ??
?a??de??µa ... Course Telecommunications ...
???sp??as? µe??? d?µ??
?a??de??µa ... Telecommunications.Semester4 ...
??d??? T?µata ???ep?????????? - ??sa???? st?
C ?????aµµa ?etapt???a??? Sp??d??
?ad????e?t??????a?
42??µ?? (Structures)
S??d?asµ?? p??sd????sµ?? ?a? ???sµ??
?a??de??µa struct int CourseCode int
Semester int Instructor Telecommunications
??????p???s? d?µ??
?a??de??µa ... Course Telecommunications353,4,1
2 ...
??d??? T?µata ???ep?????????? - ??sa???? st?
C ?????aµµa ?etapt???a??? Sp??d??
?ad????e?t??????a?
43??µ?? (Structures)
???ete? d?µ??
?a??de??µa struct Point int x int
y struct StraightLine Point
StartPoint Point EndPoint
??d??? T?µata ???ep?????????? - ??sa???? st?
C ?????aµµa ?etapt???a??? Sp??d??
?ad????e?t??????a?
44??µ?? (Structures)
???sp??as? ???et?? d?µ??
?a??de??µa ... MyStraightLine.StartPoint.x0 MyS
traightLine.StartPoint.y0 ...
??????p???s? ???et?? d?µ??
?a??de??µa ... StraightLine MyStraightLine0,0
,1,1 ...
??d??? T?µata ???ep?????????? - ??sa???? st?
C ?????aµµa ?etapt???a??? Sp??d??
?ad????e?t??????a?
45??µ?? (Structures)
- ??µ?? ?a? t??e?? a?t??e?µ????
??d??? T?µata ???ep?????????? - ??sa???? st?
C ?????aµµa ?etapt???a??? Sp??d??
?ad????e?t??????a?
46?pa???µ?t?? (enumerated) t?p?? ded?µ????
S??ta??
enum ???µa µetaß??t?? ??sta t?µ??-µe???
?a??de??µa enum DayOfTheWeek Sun,Mon,Tue,Wed,Th
u,Fri,Sat ... DayOfTheWeek day1,day2 ...
??d??? T?µata ???ep?????????? - ??sa???? st?
C ?????aµµa ?etapt???a??? Sp??d??
?ad????e?t??????a?
47?pa???µ?t?? (enumerated) t?p?? ded?µ????
- ?etaß??t?? ?s?d??aµe? µe a?e?a????
?a??de??µa enum Position low-1,middle,high
??d??? T?µata ???ep?????????? - ??sa???? st?
C ?????aµµa ?etapt???a??? Sp??d??
?ad????e?t??????a?
48S??a?t?se??
????s? s????t?s??
?a??de??µa void anyFunction(int, int )
???s? s????t?s??
?a??de??µa anyFunction(10,a)
??d??? T?µata ???ep?????????? - ??sa???? st?
C ?????aµµa ?etapt???a??? Sp??d??
?ad????e?t??????a?
49S??a?t?se??
???sµ?? s????t?s??
t?p??-ep?st?ef.-t?µ?? ???µa-s????t?s??
(???sµata) ...
p??t?se?? ...
??d??? T?µata ???ep?????????? - ??sa???? st?
C ?????aµµa ?etapt???a??? Sp??d??
?ad????e?t??????a?
50S??a?t?se??
???sµ?? s????t?s??
?a??de??µa void RepeatChar(char ch, int n)
for(int j0 jltn j) cout ltlt ch cout ltlt
endl
??d??? T?µata ???ep?????????? - ??sa???? st?
C ?????aµµa ?etapt???a??? Sp??d??
?ad????e?t??????a?
51S??a?t?se??
- ?etaß?ßas? µetaß??t?? µe t?µ?
?a??de??µa void DisplayCourse(Course
AnyCourse) cout ltlt The code of the course
is ltlt AnyCourse.CourseCode ltlt endl
...
??d??? T?µata ???ep?????????? - ??sa???? st?
C ?????aµµa ?etapt???a??? Sp??d??
?ad????e?t??????a?
52S??a?t?se??
?a??de??µa float InchToCentimetre(float
InchVal) float CmValInchVal2.54 return
CmVal
??d??? T?µata ???ep?????????? - ??sa???? st?
C ?????aµµa ?etapt???a??? Sp??d??
?ad????e?t??????a?
53S??a?t?se??
?p?st??f? µetaß??t?? d?µ??
?a??de??µa Course InputCourse() Course
NewCourse cin gtgt NewCourse.Code gtgt
NewCourse.Semester gtgt NewCourse.Instructor
return NewCourse
??d??? T?µata ???ep?????????? - ??sa???? st?
C ?????aµµa ?etapt???a??? Sp??d??
?ad????e?t??????a?
54S??a?t?se??
?etaß?ßas? ???sµ?t?? µe a?af???
?a??de??µa void Swap(float Val1, float val2)
float temp tempVal2 Val2Val1
Val1temp
??d??? T?µata ???ep?????????? - ??sa???? st?
C ?????aµµa ?etapt???a??? Sp??d??
?ad????e?t??????a?
55S??a?t?se??
S??a?t?se?? µe ?p??ßas?
??af??et???? a???µ?? ???sµ?t??
?a??de??µa float logarithm(float Number)
return log(Number) float logarithm(float
Number, int Base) return log(Number)/log(Base)
??d??? T?µata ???ep?????????? - ??sa???? st?
C ?????aµµa ?etapt???a??? Sp??d??
?ad????e?t??????a?
56S??a?t?se??
S??a?t?se?? µe ?p??ßas?
??af??et??? e?d? ???sµ?t??
?a??de??µa float square(int Number)
return NumberNumber float square(float
Number) return NumberNumber
??d??? T?µata ???ep?????????? - ??sa???? st?
C ?????aµµa ?etapt???a??? Sp??d??
?ad????e?t??????a?
57S??a?t?se??
???ep??e?µ??a ???sµata
?a??de??µa float power(float, int2) ... float
power(float Number, int Exp) float
Result1 for (int i0iltExpi)
ResultResultNumber return Result
??d??? T?µata ???ep?????????? - ??sa???? st?
C ?????aµµa ?etapt???a??? Sp??d??
?ad????e?t??????a?
58?etaß??t??
- ??t?µate? (t?p????) µetaß??t??
- ???te????? (?a???????) µetaß??t??
Stat???? a?t?µate?
Stat???? e??te?????
??d??? T?µata ???ep?????????? - ??sa???? st?
C ?????aµµa ?etapt???a??? Sp??d??
?ad????e?t??????a?
59???a?e?
- ?µad?p???s? st???e??? ?d??? t?p??
- ???sp??as? st???e??? µe de??t?
- ? t?p?? e?te ßas???? e?te ap? t?? ???st?
??d??? T?µata ???ep?????????? - ??sa???? st?
C ?????aµµa ?etapt???a??? Sp??d??
?ad????e?t??????a?
60???a?e?
???sµ?? p??a?a
t?p??-ded?µ???? ???µa-p??a?aµ??e???-p??a?a
?a??de??µa int Numbers4
??d??? T?µata ???ep?????????? - ??sa???? st?
C ?????aµµa ?etapt???a??? Sp??d??
?ad????e?t??????a?
61???a?e?
????d??stat?? p??a?a? st? µ??µ? t?? ?/?
0xDEF8
Numbers0
25
Numbers1
3
0xDEF9
17
Numbers2
0xDEF?
Numbers3
0xDEF?
500
??d??? T?µata ???ep?????????? - ??sa???? st?
C ?????aµµa ?etapt???a??? Sp??d??
?ad????e?t??????a?
62???a?e?
???sp??as? st???e??? p??a?a
?a??de??µa ... int Numbers4 ... Numbers025
... cin gtgt Numbers1 ... cout Numbers3 ...
??d??? T?µata ???ep?????????? - ??sa???? st?
C ?????aµµa ?etapt???a??? Sp??d??
?ad????e?t??????a?
63???a?e?
??????p???s? p??a?a (1/2)
?a??de??µa ... int Numbers41,2,3 ...
??d??? T?µata ???ep?????????? - ??sa???? st?
C ?????aµµa ?etapt???a??? Sp??d??
?ad????e?t??????a?
64???a?e?
??????p???s? p??a?a (2/2)
?a??de??µa ... int Numbers1,2,3 ...
??d??? T?µata ???ep?????????? - ??sa???? st?
C ?????aµµa ?etapt???a??? Sp??d??
?ad????e?t??????a?
65???a?e?
????d??stat?? p??a?e?
?a??de??µa ... int NumbersNxNy ... Numbers
24123 ... cin gtgt Numbersij ... cout
Numbers12j ...
??d??? T?µata ???ep?????????? - ??sa???? st?
C ?????aµµa ?etapt???a??? Sp??d??
?ad????e?t??????a?
66???a?e?
??sd??stat?? p??a?a? st? µ??µ? t?? ?/?
??d??? T?µata ???ep?????????? - ??sa???? st?
C ?????aµµa ?etapt???a??? Sp??d??
?ad????e?t??????a?
67???a?e?
??????p???s? p???d??stat?? p??a?a
?a??de??µa ... int Numbers231,25,37,12,1
5,18 ...
??d??? T?µata ???ep?????????? - ??sa???? st?
C ?????aµµa ?etapt???a??? Sp??d??
?ad????e?t??????a?
68???a?e? ?a? s??a?t?se??
include ltiostream.hgt const int TotalFloats10
// number of the float numbers! float
meanNumber(float FloatNumbersTotalFloats) void
main( ) float NumbersTotalFloats cout
ltlt Give the numbers ltlt endl for (int
i0iltTotalFloatsi) cout ltlt Number ltlt
(i1) ltlt is cin gtgt Numbersi
cout ltlt The mean is ltlt meanNumber(Numbers) ltlt
endl float meanNumber(float
FloatNumbersTotalFloats) float
TotalSum0.0 for (int i0iltTotalFloatsi)
TotalSumFloatNumbersi return
TotalSum/TotalFloats
??d??? T?µata ???ep?????????? - ??sa???? st?
C ?????aµµa ?etapt???a??? Sp??d??
?ad????e?t??????a?
69???a?e?
???s???!!!
??d??? T?µata ???ep?????????? - ??sa???? st?
C ?????aµµa ?etapt???a??? Sp??d??
?ad????e?t??????a?
70???a?e? d?µ??
include ltiostream.hgt const int TotalCourses3
// number of the float numbers! struct course
int CourseCode int Semester course
InputCourse() void main( ) course
AllCoursesTotalCourses cout ltlt Input the
courses ltlt endl for (int i0iltTotalCourses
i) cout ltlt Course ltlt (i1) ltlt ltlt
endl AllCoursesiInputCourse()
cout ltlt AllCourses2.CourseCode ltlt
endl course InputCourse() course
AnyCourse cout ltlt Course code cin gtgt
AnyCourse.CourseCode ... return AnyCourse
??d??? T?µata ???ep?????????? - ??sa???? st?
C ?????aµµa ?etapt???a??? Sp??d??
?ad????e?t??????a?
71??fa???µ?t???
??fa???µ?t???? µetaß??t??
char ???µa-µetaß??t??µ??e???1
?a??de??µa char str120
??d??? T?µata ???ep?????????? - ??sa???? st?
C ?????aµµa ?etapt???a??? Sp??d??
?ad????e?t??????a?
72??fa???µ?t???
??fa???µ?t???? st? µ??µ? t?? ?/?
0xDEF8
str0
G
str1
o
0xDEF9
d
str2
0xDEF?
str3
0xDEF?
\0
??d??? T?µata ???ep?????????? - ??sa???? st?
C ?????aµµa ?etapt???a??? Sp??d??
?ad????e?t??????a?
73??fa???µ?t???
??sa???? ?a? eµf???s? a?fa???µ?t????
?a?ade??µata ... char str120 ... cin gtgt
str ... cin gtgt setw(120) gtgt str ... cout ltlt str
??d??? T?µata ???ep?????????? - ??sa???? st?
C ?????aµµa ?etapt???a??? Sp??d??
?ad????e?t??????a?
74??fa???µ?t???
??fa???µ?t???? sta?e???
?a?ade??µata char strhello char
strh,e,l,l,o
??d??? T?µata ???ep?????????? - ??sa???? st?
C ?????aµµa ?etapt???a??? Sp??d??
?ad????e?t??????a?
75??fa???µ?t???
??sa???? µe t? s?????t?s? cinget()
?a?ade??µata ... char str100 ... cin.get(str,
100) ... cin.get(str,100,)
??d??? T?µata ???ep?????????? - ??sa???? st?
C ?????aµµa ?etapt???a??? Sp??d??
?ad????e?t??????a?
76??t???af? a?fa???µ?t????
include ltiostream.hgt include ltstring.hgt const
int StringMax100 void main( ) char
String1This is the string char
String2StringMax int i
for(i0iltstrlen(String1)i)
String2iString1i String2i\0 cout
ltlt endl ltlt String2 ltlt endl
??d??? T?µata ???ep?????????? - ??sa???? st?
C ?????aµµa ?etapt???a??? Sp??d??
?ad????e?t??????a?
77???a?e? a?fa???µ?t????
include ltiostream.hgt const int
StringMax100 const int MaxNames6 void main(
) char NamesMaxNamesStringMax
akis,makis,takis,vakis,sakis,lakis
int i cout ltlt The Names areltlt endl
for(i0iltMaxNamesi) cout ltlt Namesi ltlt
endl
??d??? T?µata ???ep?????????? - ??sa???? st?
C ?????aµµa ?etapt???a??? Sp??d??
?ad????e?t??????a?
78???e??
???sµ?? t????
class ???µa-t???? private
... protected public
??d??? T?µata ???ep?????????? - ??sa???? st?
C ?????aµµa ?etapt???a??? Sp??d??
?ad????e?t??????a?
79???e??
???sµ?? t????
?a??de??µa class Simple private int
data public void setdata(int d)
datad void showdata() cout ltlt data ltlt
endl
??d??? T?µata ???ep?????????? - ??sa???? st?
C ?????aµµa ?etapt???a??? Sp??d??
?ad????e?t??????a?
80???e??
???sµ?? t???? (1/2)
?a??de??µa a??e??? ?efa??da? (Simple.h) class
Simple private int data public
void setdata(int ) void showdata()
??d??? T?µata ???ep?????????? - ??sa???? st?
C ?????aµµa ?etapt???a??? Sp??d??
?ad????e?t??????a?
81???e??
???sµ?? t???? (2/2)
?a??de??µa p??a??? ??d??a include
Simple.h void Simplesetdata(int d)
datad void Simpleshowdata() cout ltlt
data ltlt endl
??d??? T?µata ???ep?????????? - ??sa???? st?
C ?????aµµa ?etapt???a??? Sp??d??
?ad????e?t??????a?
82??t??e?µe?a
????d?? e??at?stas?? (1/2)
?a??de??µa a??e??? ?efa??da? (Counter.h) incl
ude ltiostream.hgt class Counter private
unsigned int count public Counter( )
count 0 void inc_count( )
count int get_count( ) return
count
??d??? T?µata ???ep?????????? - ??sa???? st?
C ?????aµµa ?etapt???a??? Sp??d??
?ad????e?t??????a?
83??t??e?µe?a
????d?? e??at?stas?? (2/2)
?a??de??µa p??a??? ??d??a include
Counter.h void main( ) Counter c1, c2
cout ltlt "\nc1" ltlt c1.get_count( ) cout ltlt
"\nc2" ltlt c2.get_count( ) c1.inc_count( )
c2.inc_count( ) c2.inc_count( ) cout ltlt
"\nc1" ltlt c1.get_count( ) cout ltlt "\nc2" ltlt
c2.get_count( )
??d??? T?µata ???ep?????????? - ??sa???? st?
C ?????aµµa ?etapt???a??? Sp??d??
?ad????e?t??????a?
84??t??e?µe?a
????d?? ap?s??des??
?a??de??µa a??e??? ?efa??da? (Counter.h) incl
ude ltiostream.hgt class Counter private
unsigned int count public Counter( )
count 0 ... Counter()
??d??? T?µata ???ep?????????? - ??sa???? st?
C ?????aµµa ?etapt???a??? Sp??d??
?ad????e?t??????a?
85??t??e?µe?a ?? ???sµata (1/3)
?a??de??µa a??e??? ?efa??da? (Distance.h) clas
s Distance private int feet
float inches public Distance( )
Distance(int ft, float in) void getdist(
) void showdist( ) void add_dist(
Distance, Distance )
??d??? T?µata ???ep?????????? - ??sa???? st?
C ?????aµµa ?etapt???a??? Sp??d??
?ad????e?t??????a?
86??t??e?µe?a ?? ???sµata (2/3)
?a??de??µa p??a??? ??d??a (Distance.cpp) inclu
de Distance.h DistanceDistance(int ft, float
in) feet ft inches in void
Distancegetdist( ) cout ltlt "\nGive feet "
cin gtgt feet cout ltlt Give inches " cin gtgt
inches void Distanceshowdist( ) cout ltlt
feet ltlt "\'-" ltlt inches ltlt '\"' void
Distanceadd_dist(Distance d2, Distance d3)
inches d2.inches d3.inches feet 0
if(inches gt 12.0) inches - 12.0
feet feet d2.feet d3.feet
??d??? T?µata ???ep?????????? - ??sa???? st?
C ?????aµµa ?etapt???a??? Sp??d??
?ad????e?t??????a?
87??t??e?µe?a ?? ???sµata (3/3)
?a??de??µa p??a??? ??d??a (MainProgram.cpp) i
nclude Distance.h void main( ) Distance
dist1, dist3 Distance dist2(11, 6.25)
dist1.getdist( ) dist3.add_dist(dist1,
dist2) cout ltlt "\ndist1 " dist1.showdist(
) cout ltlt "\ndist2 " dist2.showdist( )
cout ltlt "\ndist3 " dist3.showdist( )
??d??? T?µata ???ep?????????? - ??sa???? st?
C ?????aµµa ?etapt???a??? Sp??d??
?ad????e?t??????a?
88?p?st??f? a?t??e?µ???? (1/3)
?a??de??µa a??e??? ?efa??da? (Distance.h) clas
s Distance private int feet
float inches public Distance( )
Distance(int ft, float in) void getdist(
) void showdist( ) Distance add_dist(
Distance )
??d??? T?µata ???ep?????????? - ??sa???? st?
C ?????aµµa ?etapt???a??? Sp??d??
?ad????e?t??????a?
89?p?st??f? a?t??e?µ???? (2/3)
?a??de??µa p??a??? ??d??a (Distance.cpp) inclu
de Distance.h DistanceDistance(int ft, float
in) feet ft inches in ... Distance
Distanceadd_dist(Distance d2) Distance
temp temp.inchesinches d2.inches
if(temp.inches gt 12.0) temp.inches -
12.0 temp.feet1 temp.feet feet
d2.feet return temp
??d??? T?µata ???ep?????????? - ??sa???? st?
C ?????aµµa ?etapt???a??? Sp??d??
?ad????e?t??????a?
90?p?st??f? a?t??e?µ???? (3/3)
?a??de??µa p??a??? ??d??a (MainProgram.cpp) i
nclude Distance.h void main( ) Distance
dist1, dist3 Distance dist2(11, 6.25)
dist1.getdist( ) dist3dist1.add_dist(dist2)
cout ltlt "\ndist1 " dist1.showdist( ) cout ltlt
"\ndist2 " dist2.showdist( ) cout ltlt
"\ndist3 " dist3.showdist( )
??d??? T?µata ???ep?????????? - ??sa???? st?
C ?????aµµa ?etapt???a??? Sp??d??
?ad????e?t??????a?
91???e?? ?a? ??t??e?µe?a st? ???µ?
- ?etaß??t?? pe??pt?s?? - ?µ?t?pa
- ?p????e?s? ded?µ???? - µe??d??
??d??? T?µata ???ep?????????? - ??sa???? st?
C ?????aµµa ?etapt???a??? Sp??d??
?ad????e?t??????a?
92???e?? ?a? ??t??e?µe?a st? ???µ?
Stat??? ded?µ??a t????
?a??de??µa include ltiostream.hgt class foo
private static int count public foo( )
count int getcount( ) return count
void main( ) foo f1, f2, f3 cout ltlt
"\ncount is " ltlt f1.getcount( ) cout ltlt
"\ncount is " ltlt f2.getcount( ) cout ltlt
"\ncount is " ltlt f3.getcount( )
??d??? T?µata ???ep?????????? - ??sa???? st?
C ?????aµµa ?etapt???a??? Sp??d??
?ad????e?t??????a?
93?p??ßas? te?est??
?p??ßas? µ??ad?a??? te?est?? (1/2)
?a??de??µa include ltiostream.hgt class
Counter private unsigned int count
public Counter( ) count 0 int
get_count( ) return count void operator
( ) count void main( ) Counter
c2 cout ltlt "\nc2" ltlt c2.get_count( ) c2
c2 cout ltlt "\nc2" ltlt c2.get_count( )
??d??? T?µata ???ep?????????? - ??sa???? st?
C ?????aµµa ?etapt???a??? Sp??d??
?ad????e?t??????a?
94?p??ßas? te?est??
?p??ßas? µ??ad?a??? te?est?? (2/2)
?a??de??µa include ltiostream.hgt class
Counter ... Counter operator ( )
count Counter temp Counter temp.count
count return temp void main( )
Counter c1,c2 cout ltlt "\nc2" ltlt
c2.get_count( ) c2 c1c2 cout ltlt
"\nc2" ltlt c2.get_count( )
??d??? T?µata ???ep?????????? - ??sa???? st?
C ?????aµµa ?etapt???a??? Sp??d??
?ad????e?t??????a?
95?p??ßas? d?ad???? te?est?? (1/3)
Distance.h
include ltiostream.hgt class Distance
private int feet float inches
public Distance( ) feet 0 inches
0.0 Distance(int ft, float in) feet
ft inches in void getdist( )
cout ltlt give feet " cin gtgt feet cout
ltlt give inches " cin gtgt inches
void showdist( ) cout ltlt feet ltlt "\'-" ltlt
inches ltlt '\"' Distance operator (
Distance )
??d??? T?µata ???ep?????????? - ??sa???? st?
C ?????aµµa ?etapt???a??? Sp??d??
?ad????e?t??????a?
96?p??ßas? d?ad???? te?est?? (2/3)
Distance.cpp
include Distance.h Distance
Distanceoperator (Distance d2) int f
feet d2.feet float i inches
d2.inches if(i gt 12.0)
i - 12.0 f
return Distance(f,i)
??d??? T?µata ???ep?????????? - ??sa???? st?
C ?????aµµa ?etapt???a??? Sp??d??
?ad????e?t??????a?
97?p??ßas? d?ad???? te?est?? (3/3)
MainProgram.cpp
include Distance.h void main( )
Distance dist1, dist3, dist4
dist1.getdist( ) Distance
dist2(11, 6.25) dist3 dist1 dist2
dist4 dist1 dist2 dist3
cout ltlt "\ndist1 " dist1.showdist( ) cout
ltlt "\ndist2 " dist2.showdist( ) cout ltlt
"\ndist3 " dist3.showdist( ) cout ltlt
"\ndist4 " dist4.showdist( )
??d??? T?µata ???ep?????????? - ??sa???? st?
C ?????aµµa ?etapt???a??? Sp??d??
?ad????e?t??????a?
98?etat??p? ded?µ????
a?t??e?µe?? p?????sµ?? objectA
objectB a?t??e?µe?? p????e?s??
??d??? T?µata ???ep?????????? - ??sa???? st?
C ?????aµµa ?etapt???a??? Sp??d??
?ad????e?t??????a?
99?etat??p? ded?µ????
?etat??p?? ??p?? a?t??e?µe?? a?t??e?µe?? p
?????sµ?? p????e?s?? ßas???? se
ßas??? - - ßas???? se t??? constructor - t?
?? se ßas??? - µ???d?? µetat??p?? t??? se
t??? constructor µ???d?? µetat??p??
??d??? T?µata ???ep?????????? - ??sa???? st?
C ?????aµµa ?etapt???a??? Sp??d??
?ad????e?t??????a?
100???????µ???t?ta
- ??ad??as?a d?µ??????a? pa?a????? t??e?? (derived
classes) - ap?
- ßas???? t??e?? (base classes)
- ?pa?a???s?µ?p???s? ??d??a
??d??? T?µata ???ep?????????? - ??sa???? st?
C ?????aµµa ?etapt???a??? Sp??d??
?ad????e?t??????a?
101???????µ???t?ta
S??ta??
?a??de??µa include Counter.h class
CounterDown public Counter public Counter
operator--() count-- return
Counter(count)
??d??? T?µata ???ep?????????? - ??sa???? st?
C ?????aµµa ?etapt???a??? Sp??d??
?ad????e?t??????a?
102???????µ???t?ta
- ???sp??as? µe??? ßas???? t????
- ????d?? e??at?stas??
- ????d?? ßas???? t????
- ?a????st?? p??sßas??
- ?a????st?? t??? pa?????? t??? a?t??e?µe?? e?t??
t???? - public
- protected -
- private - -
??d??? T?µata ???ep?????????? - ??sa???? st?
C ?????aµµa ?etapt???a??? Sp??d??
?ad????e?t??????a?
103???????µ???t?ta
????d?? e??at?stas?? pa??????? t????
?a??de??µa include Counter.h class CountDn
public Counter public CountDn( )
Counter( ) CountDn(int c) Counter(c)
CountDn operator -- ( ) count--
return CountDn(count)
??d??? T?µata ???ep?????????? - ??sa???? st?
C ?????aµµa ?etapt???a??? Sp??d??
?ad????e?t??????a?
104???????µ???t?ta
- ?p??ßas? µe??d?? ßas???? t????
- ??????s? µeta?? overloaded µe??d??
??d??? T?µata ???ep?????????? - ??sa???? st?
C ?????aµµa ?etapt???a??? Sp??d??
?ad????e?t??????a?
105??µ?s?a ?a? ?d??t??? ???????µ???t?ta(1/2)
class A private int privdataA
protected int protdataA public
int pubdataA class B public A
public void funct( ) int
a a privdataA a protdataA
a pubdataA class
C private A public void funct( )
int a a privdataA
a protdataA a pubdataA
??d??? T?µata ???ep?????????? - ??sa???? st?
C ?????aµµa ?etapt???a??? Sp??d??
?ad????e?t??????a?
106??µ?s?a ?a? ?d??t??? ???????µ???t?ta (2/2)
void main( ) int a B objB a
objB.privdataA a objB.protdataA a
objB.pubdataA C objC a
objC.privdataA a objC.protdataA a
objC.pubdataA
??d??? T?µata ???ep?????????? - ??sa???? st?
C ?????aµµa ?etapt???a??? Sp??d??
?ad????e?t??????a?
107?e??te?
- ????s?µa?t? - µ???t??? a???µ?s?
??d??? T?µata ???ep?????????? - ??sa???? st?
C ?????aµµa ?etapt???a??? Sp??d??
?ad????e?t??????a?
108?e??te?
??p?? µetaß??t?? (32bit)
Type Size (bits) Range Sample
applications unsigned char 8 0 lt X lt
255 Small numbers and full PC character
set char 8 -128 lt X lt 127 Very small numbers
and ASCII characters short int 16 -32,768
lt X lt 32,767 Counting, small
numbers, loop control unsigned int 32 0 lt
X lt 4,294,967,295 Large numbers and
loops int 32 -2,147,483,648 lt X lt
2,147,483,647 Counting, small numbers, loop
control unsigned long 32 0 lt X lt
4,294,967,295 Astronomical distances enum 32 -2,
147,483,648 lt X lt 2,147,483,647 Ordered sets of
values long 32 -2,147,483,648 lt X lt
2,147,483,647 Large numbers, populations float 32
1.18 10-38 lt X lt 3.40 1038 Scientific
(7-digit) precision double 64 2.23 10-308 lt
X lt 1.79 10308 Scientific (15-digit)
precision long double 80 3.37 10-4932 lt X lt
1.18 104932 Financial (18-digit) precision
??d??? T?µata ???ep?????????? - ??sa???? st?
C ?????aµµa ?etapt???a??? Sp??d??
?ad????e?t??????a?
109?e??te?
?e?est?? d?e????s??
?a??de??µa include ltiostream.hgt void main()
int var1 double fvar1.0 cout ltlt var ltlt
endl ltlt fvar ltlt endl
??d??? T?µata ???ep?????????? - ??sa???? st?
C ?????aµµa ?etapt???a??? Sp??d??
?ad????e?t??????a?
110?e??te?
?etaß??t?? de??t?
?a??de??µa include ltiostream.hgt void main()
int var1 int p_var double fvar1.0
double p_fvar p_varvar p_fvarfvar
cout ltlt p_var ltlt endl ltlt p_fvar ltlt endl
??d??? T?µata ???ep?????????? - ??sa???? st?
C ?????aµµa ?etapt???a??? Sp??d??
?ad????e?t??????a?
111?e??te?
???sp??as? µetaß??t?? p?? de???e? ? de??t??
?e?est?? ?µµes?? a?af???? (1/2)
?a??de??µa include ltiostream.hgt void main()
int var1 int p_var p_varvar cout ltlt
variable in address ltlt p_var ltlt has
the value ltlt p_var ltlt endl
??d??? T?µata ???ep?????????? - ??sa???? st?
C ?????aµµa ?etapt???a??? Sp??d??
?ad????e?t??????a?
112?e??te?
???sp??as? µetaß??t?? p?? de???e? ? de??t??
?e?est?? ?µµes?? a?af???? (2/2)
?a??de??µa include ltiostream.hgt void main()
int var1,var2 int p_var p_varvar1 //
d?se st? de??t? p_var t? // d?e????s? t??
µetaß??t?? var1 p_var1 // d?se st?
µetaß??t? st?? ?p??a de???e? ? // de??t??
p_var (d??. st? µetaß??t? var1 // t?? t?µ? 1
var2p_var // ?s?d??aµ? µe var21
cout ltlt var1 ltlt var1 ltlt endl ltlt var2 ltlt
var2 ltlt endl
??d??? T?µata ???ep?????????? - ??sa???? st?
C ?????aµµa ?etapt???a??? Sp??d??
?ad????e?t??????a?
113?e??te?
?e??t?? ??a t?p? void
?a??de??µa include ltiostream.hgt void main()
int var1 int p_var double fvar1.0
void p_void p_varfvar // ?????
p_voidfvar // s?st?
??d??? T?µata ???ep?????????? - ??sa???? st?
C ?????aµµa ?etapt???a??? Sp??d??
?ad????e?t??????a?
114?e??te?
?e??te? ??a p??a?e? (1/2)
?a??de??µa include ltiostream.hgt void main()
int i,iarray 2, 45, 39 for(i0ilt3i)
cout ltlt iarrayi ltlt endl for(i0ilt3i)
cout ltlt (iarrayi) ltlt endl
for(i0ilt3i) cout ltlt (iarray) ltlt endl
// ?????
??d??? T?µata ???ep?????????? - ??sa???? st?
C ?????aµµa ?etapt???a??? Sp??d??
?ad????e?t??????a?
115?e??te?
?e??te? ??a p??a?e? (2/2)
?a??de??µa include ltiostream.hgt void main()
int i,iarray 2, 45, 39 int parray
parrayiarray for(i0ilt3i) cout ltlt
(parrayi) ltlt endl for(i0ilt3i) cout
ltlt (parray) ltlt endl // s?st?
??d??? T?µata ???ep?????????? - ??sa???? st?
C ?????aµµa ?etapt???a??? Sp??d??
?ad????e?t??????a?
116?e??te?
?etaß?ßas? µetaß??t?? µe a?af???
?a??de??µa include ltiostream.hgt void
centimize(double) void main() double
var2.3 cout ltlt var ltlt var ltlt inches ltlt
endl centimize(var) cout ltlt var ltlt var
ltlt cm ltlt endl void centimize(double v)
vv2.54
??d??? T?µata ???ep?????????? - ??sa???? st?
C ?????aµµa ?etapt???a??? Sp??d??
?ad????e?t??????a?
117?e??te?
?etaß?ßas? µetaß??t?? µe de??te?
?a??de??µa include ltiostream.hgt void
centimize(double) void main() double
var2.3 cout ltlt var ltlt var ltlt inches ltlt
endl centimize(var) cout ltlt var ltlt var
ltlt cm ltlt endl void centimize(double
dpntr) dpntr(dpntr)2.54
??d??? T?µata ???ep?????????? - ??sa???? st?
C ?????aµµa ?etapt???a??? Sp??d??
?ad????e?t??????a?
118?etaß?ßas? p?????? µe de??te?
include ltiostream.hgt const int MAX10 void
centimize(double, int) void main() int
N,i double inchArrayMAX cout ltlt Give the
number of array elements cin gtgt N cout
ltlt Give the array elements in inches ltlt endl
for(i0iltNi) cin gtgt (inchArrayi)
centimize(inchArray,N) cout ltlt Array elements
in cm ltlt endl for(i0iltNi) cout ltlt
(inchArrayi) ltlt endl void
centimize(double array, int arraySize)
for(int i0iltarraySizei)
(arrayi)(arrayi)2.54
??d??? T?µata ???ep?????????? - ??sa???? st?
C ?????aµµa ?etapt???a??? Sp??d??
?ad????e?t??????a?
119?e??te?
?etaß?ßas? a?fa???µ?t???? µe de??te?
?a??de??µa include ltiostream.hgt void
dispstr(char) void main( ) char str
I am a string" dispstr(str) void
dispstr(char ps) while( ps ) cout ltlt
ps cout ltlt endl
??d??? T?µata ???ep?????????? - ??sa???? st?
C ?????aµµa ?etapt???a??? Sp??d??
?ad????e?t??????a?
120??a?e???s? µ??µ??
- ?? te?est?? new ?a? delete
??d??? T?µata ???ep?????????? - ??sa???? st?
C ?????aµµa ?etapt???a??? Sp??d??
?ad????e?t??????a?
121??a?e???s? µ??µ?? (1/4)
include ltiostream.hgt void centimize(double,
int) void main() int N,i double
inchArray cout ltlt Give the number of array
elements cin gtgt N inchArraynew
doubleN cout ltlt Give the array elements in
inches ltlt endl for(i0iltNi) cin gtgt
(inchArrayi) centimize(inchArray,N) cout
ltlt Array elements in cm ltlt endl
for(i0iltNi) cout ltlt (inchArrayi) ltlt
endl delete inchArray void
centimize(double array, int arraySize)
for(int i0iltarraySizei)
(arrayi)(arrayi)2.54
??d??? T?µata ???ep?????????? - ??sa???? st?
C ?????aµµa ?etapt???a??? Sp??d??
?ad????e?t??????a?
122??a?e???s? µ??µ?? (2/4)
String.h
MainProg.cpp
include ltiostream.hgt include ltstring.hgt class
String private char str public
String(char s) int length strlen(s)
str new charlength1
strcpy(str, s) String( )
delete str void display( )
cout ltlt str
include String.h void main( ) String s
First String sp1s String sp2
new String(Second) cout ltlt endl
s.display( ) cout ltlt endl sp1-gtdisplay(
) cout ltlt endl sp2-gtdisplay( )
delete sp2
??d??? T?µata ???ep?????????? - ??sa???? st?
C ?????aµµa ?etapt???a??? Sp??d??
?ad????e?t??????a?
123??a?e???s? µ??µ?? (3/4)
String.h
MainProg.cpp
include ltiostream.hgt include ltstring.hgt class
String private char str public
String(char s) int length strlen(s)
str new charlength1
strcpy(str, s) String( )
delete str void display( )
cout ltlt str
include String.h void main( ) String
s2 s1new String(First) s2new
String(Second) cout ltlt endl
s1-gtdisplay( ) cout ltlt endl
s2-gtdisplay( ) delete s1 delete s2
??d??? T?µata ???ep?????????? - ??sa???? st?
C ?????aµµa ?etapt???a??? Sp??d??
?ad????e?t??????a?
124??a?e???s? µ??µ?? (4/4)
String.h
MainProg.cpp
include String.h void main( ) int
i,nNames char line100 String names
cout ltlt Number of names cin gtgt nNames
namesnew StringnNames for(i0iltnNamesi)
cin gtgt line namesinew
String(line) cout ltlt The names are
ltlt endl for(i0iltnNamesi)
si-gtdisplay( ) cout ltlt endl
include ltiostream.hgt include ltstring.hgt class
String private char str public
String(char s) int length strlen(s)
str new charlength1
strcpy(str, s) String( )
delete str void display( )
cout ltlt str
??d??? T?µata ???ep?????????? - ??sa???? st?
C ?????aµµa ?etapt???a??? Sp??d??
?ad????e?t??????a?
125???e?a ?a? ?e?µata
- ??af??et??? ??? ded?µ???? ? ??af??et??? ?e?µa
- ??af??et??? ?e?µa
- ? ??af??et??? t???
??d??? T?µata ???ep?????????? - ??sa???? st?
C ?????aµµa ?etapt???a??? Sp??d??
?ad????e?t??????a?
126???e?a ?a? ?e?µata
???e?a d?s???
e?s?d??
???d??
?fstream, ltlt put() write()
ifstream, gtgt get() read()
a??e?? d?s???
??d??? T?µata ???ep?????????? - ??sa???? st?
C ?????aµµa ?etapt???a??? Sp??d??
?ad????e?t??????a?
127??s?d??/???d?? a?fa???µ?t????
include ltfstream.hgt void main( ) const
LineSize80 char bufferLineSize ofstream
outfile("TEST.TXT") outfile ltlt "Check this
line\n" outfile.close() ifstream
infile("TEST.TXT") while(infile)
infile.getline(buffer,LineSize) cout ltlt
buffer infile.close()
??d??? T?µata ???ep?????????? - ??sa???? st?
C ?????aµµa ?etapt???a??? Sp??d??
?ad????e?t??????a?
128??s?d??/???d?? a?t??e?µ????
Person.h
MainProg.cpp
include ltiostream.hgt include ltstring.hgt class
Person private char name int age
public Person(char s, int a) int
length strlen(s) name new
charlength1 strcpy(name, s)
agea Person() Person( )
delete name void show(void)
cout ltlt name ltlt , ltlt age ltlt endl
include Person.h include ltfstream.hgt void
main( ) Person p1(Name,30),p2 ofstream
outfile(TEST.DAT) outfile.write((char )p1,
sizeof(p1)) outfile.close() ifstream
infile(TEST.DAT) infile.read((char )p2,
size of(p2)) infile.close() p2.show()
??d??? T?µata ???ep?????????? - ??sa???? st?
C ?????aµµa ?etapt???a??? Sp??d??
?ad????e?t??????a?
129?p??ßas? te?est?? e?sa?????/e?a?????
Person.h
Person.cpp
include ltiostream.hgt include ltstring.hgt Person
Person(char s, int a) int length
strlen(s) name new charlength1
strcpy(name, s) agea PersonPerson( )
delete name ostream operator ltlt (ostream
s, Person p) cout ltlt name ltlt ,
ltlt age ltlt endl return s
include ltiostream.hgt include ltstring.hgt class
Person private char name int age
public Person(char s, int a) Person()
Person() friend ostream operator ltlt
(ostream s, Person p)
??d??? T?µata ???ep?????????? - ??sa???? st?
C ?????aµµa ?etapt???a??? Sp??d??
?ad????e?t??????a?
130???a ??µata
- F??e? s??a?t?se?? ?a? t??e??
- ???at?stas? µe a?t???af?
- (?a? ? te?est?? this)
??d??? T?µata ???ep?????????? - ??sa???? st?
C ?????aµµa ?etapt???a??? Sp??d??
?ad????e?t??????a?