C For C Programmers
C For C Programmers
-."/
0 Object oriented syntax, with C compatibility 0 Language of choice of various projects 0 Various flavors to choose from
4evie5ing C pa33erns
#include <stdlib.h> typedef struct _Person { char *name; int age; } Person; int main() { p = (Person *) malloc(sizeof(Person)); p >age = !"; return #; }
4
1%2ec3s
0 ,magine for a second 5e are no3 3a+6ing a%ou3 Persons, %u3 Cars 0 -e 5an3 3.e car go fas3er car->fue+f+o5++7 car->.... /
,n3erfaces
Crea3ing an in3erface...
/* person.h */ typedef struct _Person { char *name; int age; } Person; Person * person_create(); $oid person_destro%( Person *p ); $oid person_set_name( Person *p& const char *name ); $oid person_set_age( Person *p& int age ); #endi'
!
"
$%
me3.ods
person_set_age(p, 30);
seems 3o %e ou3 of p+ace... 5e a+read" .ave 3.e o%2ec3 5.ere 5e 5an3 3o perform person_set_age on. 8ac3ua++", prefi9ing i3 5i3. person_ is a .ac6 3o avoid co++ision 5i3. o3.er s3ruc3ure:s me3.ods and 3o ma6e i3 reada%+e ; -." no3/<
p->set_age(30);
$$
Des3ro"ing o%2ec3s
int main() { Person *p;
$2
Des3ro"ing o%2ec3s
int main() { Person p;
$&
=ierarc.ies
>++ emp+o"ees are persons. =o5ever no3 ever" person is an emp+o"ee.
typedef struct _Person { char *name; int age; } Person; typedef struct _(mplo%ee { Person *person; int salar%; } (mplo%ee;
Employee !!!
e;
C+asses
class )ight { pu"lic* $oid on(); $oid o''(); $oid brighten(); $oid dim(); pri#ate* int _po+er; }; )ight light; light.on(); light._po+er = ,#;
# $ovem%er &', ())* $ove++ ,nc.
$'
$!
,n.eri3ance 8 > is ? ;
class .hape { pu"lic* $oid dra+(); $oid erase(); $oid mo$e(); -olor get-olor(); $oid set-olor( -olor color ); }; class -ircle * pu"lic .hape { pu"lic* // has all Shape interface int radio(); };
.hape *a = ne$ .hape()* .hape *b = ne$ .hape(); -ircle *c = ne$ -ircle(); a c c b >dra+(); >dra+(); >radio(); >radio();
b = a; // 01 b = c; // 01 c = a; // +rong2
$"
1verriding func3ions
class -ircle * pu"lic .hape { // overrides Shape's #irtual dra+(); }; .hape *a; .hape *b; -ircle *c; ...
a b b c
>dra+() // Shape *, calls Shape's draw = c; >dra+() // Shape *, calls Circle's draw!! >dra+() // Circle *, callas Circle's draw
$#
4un3ime po+"morp.ism
2%
22
3.is A
$oid -ircle**do3ice4hings(); { // the same _radio = !" this#$_radio = !" // doSomethin%( Shape * ) or // doSomethin%( Circle * ) doSomethin%(this)" }
2&
Cons3ruc3ors
typedef int -olor; class .hape { pu"lic* .hape(); .hape( -olor c ); pri#ate* -olor _color; }; .hape s; .hape s(,#); s.dra+(); .hape**.hape() * _color(#) { // do more st&ff here } .hape**.hape( -olor c ) * _color(c) { // more st&ff here } .hape *s = ne$ .hape(); .hape *s = ne$ .hape(,#); s >dra+();
24
B
// more st&ff here
Des3ruc3ors
class .hape { pu"lic* .hape(); 5.hape(); }; .hape**5.hape() { // clean&p } .hape *s = ne$ .hape(); delete s; // destr&ctor called // plain bloc' { .hape s; // destr&ctor called }
2'
Defau+3 argumen3s
class .hape { pu"lic* int color() const* $oid set-olor( int c = # /* blac6 */ ); pri#ate* int _color; }; $oid .hape**set-olor( int c ) { ... } s >set-olor(7); s >set-olor();
2"
Temp+a3es
template <class 4> class m%pair { 4 $alues 879; pu"lic* m%pair (4 'irst& 4 second) { $alues8#9='irst; $alues8,9=second; } }; m%pair<int> m%ob:ect (,,;& !");
&%
TL
!ttp#//www$sgi$co /tec!/stl/table%o"%contents$!t l !ttp#//www$boost$org/doc/libs
s art ptrs, t!reads "unctors, testing, algorit! s, etc strings, containers, i/o strea s
language
&2
E3
!ttp#//doc$trolltec!$co / !ttp#//api$kde$org/
ulti edia,
Strings, containers, i/o strea s, )*+, network, testing, scripting, signals, ipc, etc
language
&&
&4
=o5 3o compi+e
0 Fse g++ -o m"prog m"prog.cpp 0 1r 5ri3e a Ma6efi+e 0 1r 5ri3e a Cma6eLis3s.393 P41GHCT8foo; >DDIHJHCFT>?LH8foo foo.cpp;
&
=ome5or6 K&
Loo6 a3 E3 3oo+6i3 c+ass .ierarc." .33p<@@doc.3ro++3ec..com@e93ras@L3'&-c+ass-c.ar3.pdf Dind 3.e para++e+ %e35een in.eri3ance concep3s e9p+ained and 3.e fac3 3.a3 ever" 5idge3 3a6es a E-idge3 A as a paren3. 4ead .33p<@@doc.3ro++3ec..com@LL@LL&M-apis..3m+ =o5 3o design good >P,s
# $ovem%er &', ())* $ove++ ,nc.
&'
=ome5or6 K(
4ead .33p<@@[email protected]+er@cpp-pi3fa++s..3m+ >nd +earn a%ou3 3.e difference %e35een cons3 me3.od, cons3 parame3ers, cons3 re3urn va+ues.
&!
=ome5or6 KM
0 Dind ou3 a C +i%rar" 5ri33en in a more or +ess o%2ec3 orien3ed 5a". 0 -rap i3 in a C++ c+ass .ierarc." 8no need 3o cover 3.e fu++ >P, ;
0 0 0
Crea3ing C s3ruc3s in cons3ruc3ors Des3ro"ing 3.e da3a in 3.e c+ass des3ruc3ors -rapping 3.e me3.ods as c+ass me3.ods
&"
T.an6s a +o3O Eues3ions/ Dan6esc.PnO Dragen/ Muc.as graciasO Pregun3as/ Goin usO "[email protected] .33p<@@opensuse.org@Ya T
(eneral )isclaimer
T.is documen3 is no3 3o %e cons3rued as a promise %" an" par3icipa3ing compan" 3o deve+op, de+iver, or mar6e3 a produc3. $ove++, ,nc., ma6es no represen3a3ions or 5arran3ies 5i3. respec3 3o 3.e con3en3s of 3.is documen3, and specifica++" disc+aims an" e9press or imp+ied 5arran3ies of merc.an3a%i+i3" or fi3ness for an" par3icu+ar purpose. Dur3.er, $ove++, ,nc., reserves 3.e rig.3 3o revise 3.is documen3 and 3o ma6e c.anges 3o i3s con3en3, a3 an" 3ime, 5i3.ou3 o%+iga3ion 3o no3if" an" person or en3i3" of suc. revisions or c.anges. >++ $ove++ mar6s referenced in 3.is presen3a3ion are 3rademar6s or regis3ered 3rademar6s of $ove++, ,nc. in 3.e Fni3ed 3a3es and o3.er coun3ries. >++ 3.ird-par3" 3rademar6s are 3.e proper3" of 3.eir respec3ive o5ners. T.is 5or6 is +icensed under 3.e Crea3ive Commons >33ri%u3ion-$oncommercia+- .are >+i6e (.Q License. To vie5 a cop" of 3.is +icense, visi3 .33p<@@crea3ivecommons.org@+icenses@%"-ncsa@(.Q@. Dor o3.er +icenses con3ac3 au3.or.