Upload
gilberthufana446877
View
235
Download
0
Embed Size (px)
Citation preview
7/24/2019 Programming in CPP
1/64
Engr. Gilbert R. Hufana
Speaker
2/23/2008 1Programming in C++
7/24/2019 Programming in CPP
2/64
a!e" on t#e C programming language.$#e C++ programming language %a! in&ente"
b' (arne Strou!troup ) 1**.$#e ,r!t &er!ion of C++ %a! u!e" internall' in
-$$ in -ugu!t 1*83.$#e ,r!t ommerial implementation %a!relea!e" in 1*8.
$#e C++ language !tan"ar"! are no% #an"le"b' t#e -merian ational Stan"ar"! n!titute
-S45 an" t#e nternational Stan"ar"!6rgani7ation S64. $#i! i! %#' 'ou often #earpure C++ referre" to a! -S Stan"ar" C++5 orS6 Stan"ar" C++.
2/23/2008Programming in C++ 2
7/24/2019 Programming in CPP
3/64
C++ i! e!!entiall' C taken to t#e net le&el. C++ !upport! ob(et orientation. C++ !port! man' ot#er impro&ement! o&er C.
9or eample5 C++ #an"le! !tring! better t#an
C5 an" #a! a more robu!t eeption #an"ling. C o"e %ill ompile ,ne in mo!t C++ ompiler!5
but t#e re&er!e i! not true. C++ !upport! all C omman"! an" al!o #a!
man' a""ition!. :ou ma' fre;uentl' !ee ol"!t'le C o"e mie" in %it# C++ o"e5 e!peiall'in program! %ritten b' programmer! %#ooriginall' !tarte" in C.
2/23/2008Programming in C++ 3
7/24/2019 Programming in CPP
4/64
C++ i! an programming language i;
cout
7/24/2019 Programming in CPP
27/64
- program i! u!uall' not limite" to a linear!e;uene of in!trution!. >uring it! proe!!it ma' bifurate5 repeat o"e or take"ei!ion!.
9or t#at purpo!e5 C++ pro&i"e! ontrol!truture! t#at !er&e to !peif' %#at #a! tobe "one b' our program5 %#en an" un"er
%#i# irum!tane!.
2/23/2008Programming in C++ 2
7/24/2019 Programming in CPP
28/64
$#e if ke'%or" i! u!e" to eeute a!tatement or blok onl' if a on"ition i!ful,lle". t! form i!F if (condition) statement
EampleFif ( 11 -!!)
cout
7/24/2019 Programming in CPP
29/64
sitc* (epression){case constant-' group of statements -;
$rea%;
case constant' group of statements ;
$rea%; 7 7 7
default' default group of statements
2/23/2008Programming in C++ 2*
7/24/2019 Programming in CPP
30/64
oop! #a&e a! purpo!e to repeat a!tatement a ertain number of time! or%#ile a on"ition i! ful,lle". 'he (hile loop
'he do)*hile loop
'he +or loop
2/23/2008Programming in C++ 30
7/24/2019 Programming in CPP
31/64
t! format i!F*ile (epression) {statement;
funtionalit' i! !impl' to repeat !tatement%#ile t#e on"ition !et in epre!!ion i! true.
EampleF*ile (n>!) {
cout
7/24/2019 Programming in CPP
32/64
t! format i!F do {statement; *ile (condition);
t! funtionalit' i! eatl' t#e !ame a! t#e%#ile loop5 eept t#at on"ition in t#e "o)%#ile loop i! e&aluate" after t#e eeutionof !tatement in!tea" of before5 granting atlea!t one eeution of !tatement e&en if
on"ition i! ne&er ful,lle".
2/23/2008Programming in C++ 32
7/24/2019 Programming in CPP
33/64
t! format i!F for (initiali0ation; condition; increase){statement;
it! main funtion i! to repeat !tatement
%#ile on"ition remain! true5 like t#e %#ileloop.
for (int n1-!; n>!; n==){cout
7/24/2019 Programming in CPP
34/64
T!ing break %e an lea&e a loop e&en if t#eon"ition for it! en" i! not ful,lle".
t an be u!e" to en" an in,nite loop5 or tofore it to en" before it! natural en".for (n1-!; n>!; n==){
cout
7/24/2019 Programming in CPP
35/64
- funtion i! a group of !tatement! t#at i!eeute" %#en it i! alle" from !ome pointof t#e program. $#e follo%ing i! it! formatF
type name ( parameter-. parameter. 777) {statements $'pe of funtion
ame of funtion
9untion parameter!4
Statement! t#at make! up t#e funtion
2/23/2008Programming in C++ 3
7/24/2019 Programming in CPP
36/64
// function eample#include using namespace std;int addition (int a. int $){int r; r1a+$;return (r);
int main (){int 0;
0 1 addition (2.6);cout
7/24/2019 Programming in CPP
37/64
T!e &oi"
,oid printmessage (){
cout
7/24/2019 Programming in CPP
38/64
' &alue
' referene
2/23/2008Programming in C++ 38
7/24/2019 Programming in CPP
39/64
n C++ t%o "iBerent funtion! an #a&e t#e!ame name if t#eir parameter t'pe! ornumber are "iBerent.
$#at mean! t#at 'ou an gi&e t#e !amename to more t#an one funtion if t#e'#a&e eit#er a "iBerent number ofparameter! or "iBerent t'pe! in t#eir
parameter!.
2/23/2008Programming in C++ 3*
7/24/2019 Programming in CPP
40/64
-n arra' i! a !erie! of element! of t#e !amet'pe plae" in ontiguou! memor' loation!t#at an be in"i&i"uall' referene" b' a""ingan in"e to a uni;ue i"enti,er.
int $illy ?2@; // "elaring an arra' int $illy ?2@ 1 { -:. . AA. 8!. -!A-; //arra' initiali7ation
a 1 $illy?@;//ae!!ing t#e &alue C++ !upport! multi)"imen!ional arra' -rra'! an be u!e" a! argument!/parameter!
2/23/2008Programming in C++ =0
7/24/2019 Programming in CPP
41/64
Enumeration! reate ne% "ata t'pe! toontain !omet#ing "iBerent t#at i! notlimite" to t#e &alue! fun"amental "atat'pe! ma' take.
t! form i! t#e follo%ingF
enum enumerationBname {
,alue-. ,alue. ,alue6. 7 7
o$ectBnames;
2/23/2008Programming in C++ =1
7/24/2019 Programming in CPP
42/64
enum colorsBt {$lac%. $lue. green.cyan. red. purple. yello. *ite;
colorsBt mycolor;
mycolor 1 $lue;
if (mycolor 11 green){
mycolor 1 red;
2/23/2008Programming in C++ =2
7/24/2019 Programming in CPP
43/64
7/24/2019 Programming in CPP
44/64
class Cectangle {int . y;
pu$lic'
,oid setB,alues (int.int);int area (,oid);
rect;
2/23/2008Programming in C++ ==
7/24/2019 Programming in CPP
45/64
-fter t#e pre&iou! "elaration! of Cectanglean" rect5 %e an refer %it#in t#e bo"' oft#e program to an' of t#e publi member!of t#e ob(et recta! if t#e' %ere normal
funtion! or normal &ariable!5 (u!t b'putting t#e ob(et! name follo%e" b' a "ot.4 an" t#en t#e name of t#e member.
9or eampleF
rect7setB,alues (6.8);
myarea 1 rect7area();
2/23/2008Programming in C++ =
7/24/2019 Programming in CPP
46/64
// classes eample
#include
using namespace std;
class Cectangle {
int . y;pu$lic'
,oid setB,alues(int.int);
int area () {
return (y);
;
2/23/2008Programming in C++ =A
,oid Cectangle''setB,alues
(int a. int $) { 1 a; y 1 $;
int main () {Cectangle rect;
rect7setB,alues (6.8);
cout
7/24/2019 Programming in CPP
47/64
6b(et! generall' nee" to initiali7e &ariable! ora!!ign "'nami memor' "uring t#eir proe!! ofreation to beome operati&e an" to a&oi"returning unepete" &alue! "uring t#eireeution.
n or"er to a&oi" t#at5 a la!! an inlu"e a!peial funtion alle" constructor5 %#i# i!automatiall' alle" %#ene&er a ne% ob(et oft#i! la!! i! reate".
his constructor function must have the samename as the class, and cannot have an! returnt!"e; not even void.
2/23/2008Programming in C++ =
7/24/2019 Programming in CPP
48/64
class Cectangle {int idt*. *eig*t;pu$lic'Cectangle (int.int);
int area () {return (idt**eig*t);
;
Cectangle''Cectangle (int a. int $) {idt* 1 a;*eig*t 1 $;
2/23/2008Programming in C++ =8
Constructor
7/24/2019 Programming in CPP
49/64
7/24/2019 Programming in CPP
50/64
7/24/2019 Programming in CPP
51/64
class Cectangle {int idt*. *eig*t;pu$lic'Cectangle (int.int);DCectangle ();int area () {return (idt* *eig*t);
;Cectangle''Cectangle (int a. int $) {idt* 1 a; *eig*t 1 $;Cectangle''DCectangle () {delete idt*; delete *eig*t;
2/23/2008Programming in C++ 1
Destructor
7/24/2019 Programming in CPP
52/64
int main 4 VCRetangle ret 35=45 retb 5A4W
out OO ret areaF OO ret.area4 OOen"lW out OO retb areaF OOretb.area4 OO en"lW
return 0W
X
2/23/2008Programming in C++ 2
7/24/2019 Programming in CPP
53/64
Pri&ate an" protete" member! of a la!! annotbe ae!!e" from out!i"e t#e !ame la!! in%#i# t#e' are "elare".
Ho%e&er5 t#i! rule "oe! not aBet friends.
9rien"! are funtion! or la!!e! "elare" a! !u#. f %e %ant to "elare an eternal funtion a!
frien" of a la!!5 t#u! allo%ing t#i! funtion to#a&e ae!! to t#e pri&ate an" protete"
member! of t#i! la!!5 %e "o it b' "elaring aprotot'pe of t#i! eternal funtion %it#in t#ela!!5 an" pree"ing it %it# t#e ke'%or" friend.
2/23/2008Programming in C++ 3
7/24/2019 Programming in CPP
54/64
class Cectangle {int idt*. *eig*t;pu$lic',oid setB,alues (int. int);int area () {return (idt* *eig*t);friend Cectangle duplicate (Cectangle);
;
,oid Cectangle''setB,alues (int a. int $) {idt* 1 a; *eig*t 1 $;
Cectangle duplicate (Cectangle rectparam){Cectangle rectres;rectres7idt* 1 rectparam7idt*;rectres7*eig*t 1 rectparam7*eig*t;return (rectres);
2/23/2008Programming in C++ =
7/24/2019 Programming in CPP
55/64
int main () {Cectangle rect. rect$;rect7setB,alues (.6);rect$ 1 duplicate (rect);
cout
7/24/2019 Programming in CPP
56/64
$#e frien" funtion! an !er&e5 for eample5to on"ut operation! bet%een t%o "iBerentla!!e!.
Generall'5 t#e u!e of frien" funtion! i! out
of an ob(et)oriente" programmingmet#o"olog'5 !o %#ene&er po!!ible it i!better to u!e member! of t#e !ame la!! toperform operation! %it# t#em.
Su# a! in t#e pre&iou! eample5 it %oul"#a&e been !#orter to integrate duplicate()%it#in t#e la!! Cectangle7
2/23/2008Programming in C++ A
7/24/2019 Programming in CPP
57/64
- ke' feature of C++ la!!e! i! in#eritane. n#eritane allo%! to reate la!!e! %#i# are
"eri&e" from ot#er la!!e!5 !o t#at t#e'automatiall' inlu"e !ome of it! parent!
member!5 plu! it! o%n. 9or eample5 %e are going to !uppo!e t#at %e%ant to "elare a !erie! of la!!e! t#at "e!ribepol'gon! like our Cectangle5 or likeC3riangle.
$#e' #a&e ertain ommon propertie!5 !u# a!bot# an be "e!ribe" b' mean! of onl' t%o!i"e!F #eig#t an" ba!e.
2/23/2008Programming in C++
7/24/2019 Programming in CPP
58/64
$#i! oul" be repre!ente" in t#e %orl" ofla!!e! %it# a la!! C4olygonfrom %#i#%e %oul" "eri&e t#e t%o ot#er one!FCectangle an" C3riangle.
2/23/2008Programming in C++ 8
7/24/2019 Programming in CPP
59/64
class C4olygon {protected' int idt*. *eig*t;pu$lic' ,oid setB,alues (int a. int $){idt*1a; *eig*t1$;
;
class Cectangle' pu$lic C4olygon {pu$lic' int area() { return (idt* *eig*t);
;
class C3riangle' pu$lic C4olygon {pu$lic' int area(){ return (idt* *eig*t / );
;
2/23/2008Programming in C++ *
7/24/2019 Programming in CPP
60/64
int main () {Cectangle rect;
C3riangle trgl;
rect7setB,alues (8.2);trgl7setB,alues (8.2);
cout
7/24/2019 Programming in CPP
61/64
Access public protected private
members o thesame class
yes yes yes
members o deri'edclasses
yes yes no
not members yes no no
2/23/2008Programming in C++ A1
7/24/2019 Programming in CPP
62/64
n priniple5 a "eri&e" la!! in#erit! e&er'member of a ba!e la!! eeptF it! on!trutor an" it! "e!trutor
it! operatorK4 member!
it! frien"!
2/23/2008Programming in C++ A2
7/24/2019 Programming in CPP
63/64
7/24/2019 Programming in CPP
64/64
#ttpF//%%%.plu!plu!.om C++ Programming 9un"amental! b'
C#arle! Ri&er C++ Primer5 =t#E" b' -""i!on)Je!le' C++ Programmer! Referene b' @Gra%
http://www.cplusplus.com/http://www.cplusplus.com/