Download as pdf or txt
Download as pdf or txt
You are on page 1of 41

C++ for C programmers

Duncan Mac-Vicar P. <dmacvicar@suse.de> Team Lead, Ya T, !erman"

-."/
0 Object oriented syntax, with C compatibility 0 Language of choice of various projects 0 Various flavors to choose from

# $ovem%er &', ())* $ove++ ,nc.

1%2ec3 orien3ed C pa33erns

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

# $ovem%er &', ())* $ove++ ,nc.

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->.... /

# $ovem%er &', ())* $ove++ ,nc.

,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'
!

# $ovem%er &', ())* $ove++ ,nc.

-.ic. can %e no5 used...


int main() { Person *p; p = person_create(); person_set_age(p& !#); person_destro%(p); return #; }

"

# $ovem%er &', ())* $ove++ ,nc.

-.ic. can %e no5 used...


int main() { Person *p; p = person_create(); person_set_age(p& !#); return #; }

# $ovem%er &', ())* $ove++ ,nc.

-.ic. can %e no5 used...


int main() { Person *p;

p = person_create(); person_set_age(p, 30);


return #; }

No knowledge about internals

$%

# $ovem%er &', ())* $ove++ ,nc.

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);

$$

# $ovem%er &', ())* $ove++ ,nc.

Des3ro"ing o%2ec3s
int main() { Person *p;

p = person_create(); person_set_age(p, 30);


return #; }

Destroy not called (expected)

$2

# $ovem%er &', ())* $ove++ ,nc.

Des3ro"ing o%2ec3s
int main() { Person p;

person_init(&p); person_set_age(&p, 30);


return #; }

Destroy not called (expected?)

$&

# $ovem%er &', ())* $ove++ ,nc.

=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;

person_set_age(e, 30); person_set_age(e->person, 30);


$4

# $ovem%er &', ())* $ove++ ,nc.

1%2ec3 1rien3ed concep3s and C++

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.

$'

Composi3ion 8> .as ?;


class (ngine; class -ar { pu"lic* (ngine * engine(); pri#ate* (ngine *_engine; }; (ngine * -ar**engine() { return _engine; }

$!

# $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

$"

# $ovem%er &', ())* $ove++ ,nc.

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

$#

# $ovem%er &', ())* $ove++ ,nc.

4un3ime po+"morp.ism

2%

# $ovem%er &', ())* $ove++ ,nc.

More s"n3a9 af3er 3.e concep3s

Me3.ods @ in+ine me3.ods


class .hape { $oid dra+(); }; $oid .hape**dra+(); { // blah } class .hape { $oid dra+() { // blah } };

22

# $ovem%er &', ())* $ove++ ,nc.

3.is A
$oid -ircle**do3ice4hings(); { // the same _radio = !" this#$_radio = !" // doSomethin%( Shape * ) or // doSomethin%( Circle * ) doSomethin%(this)" }

2&

# $ovem%er &', ())* $ove++ ,nc.

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

# $ovem%er &', ())* $ove++ ,nc.

Ca++ing superc+ass cons3ruc3ors


-ircle**-ircle( -olor c& int radio ) * .hape(c)& _radio(radio)

B
// more st&ff here

# $ovem%er &', ())* $ove++ ,nc.

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'

# $ovem%er &', ())* $ove++ ,nc.

Compi+e 3ime res3ric3ions< cons3 me3.od


class .hape { pu"lic* int color() const { return _color; } $oid set-olor( int c ) { _color = c; } pri#ate* int _color; };
2!

# $ovem%er &', ())* $ove++ ,nc.

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"

# $ovem%er &', ())* $ove++ ,nc.

Temp+a3es re3.in6ing 3.e +in6ed +is3...

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 (,,;& !");

&%

# $ovem%er &', ())* $ove++ ,nc.

Languages, 3ac6s and Drame5or6s

TL
!ttp#//www$sgi$co /tec!/stl/table%o"%contents$!t l !ttp#//www$boost$org/doc/libs

boost STL stl c++

s art ptrs, t!reads "unctors, testing, algorit! s, etc strings, containers, i/o strea s

language

&2

# $ovem%er &', ())* $ove++ ,nc.

E3
!ttp#//doc$trolltec!$co / !ttp#//api$kde$org/

&D' (t stl c++

Desktop, !ardware, ,pplication,

ulti edia,

Strings, containers, i/o strea s, )*+, network, testing, scripting, signals, ipc, etc

language

&&

# $ovem%er &', ())* $ove++ ,nc.

Dorge3 cons3 c.ar A


string a(<abcd e'g<); string b(<=%> i:6<); string c; cout << a << < < << b << endl; i-' cout << <.tring empt%* < << c.empt%() << endl; // .s strin% empt+/ 0es it is empt+. (1234) c = a ? b; cout << c << endl; cout << <.tring length* < << c.length() << endl; cout << <.tring si>e* < << c.si>e() << endl; cout << <.tring capacit%* < << c.capacit%() << endl; cout << <.tring empt%* < << c.empt%() << endl; // .s strin% empt+/ 6o it is 6(1 empt+. (789S4) string d = c; cout << d << endl; // (&tp&t) abcd ef% *+, // Strin% empt+) // // // // // // concatenation abcd ef%*+, i-' Strin% len%th) 5 Strin% si,e) 5 Strin% capacit+) 5 Strin% empt+) !

// abcd ef%*+, i-'

&4

# $ovem%er &', ())* $ove++ ,nc.

=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;

&

# $ovem%er &', ())* $ove++ ,nc.

=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<@@deve+oper.6de.org@N5.ee+er@cpp-pi3fa++s..3m+ >nd +earn a%ou3 3.e difference %e35een cons3 me3.od, cons3 parame3ers, cons3 re3urn va+ues.

&!

# $ovem%er &', ())* $ove++ ,nc.

=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

0 ugges3ions< cur+ eas" >P,, DL, d%us

&"

# $ovem%er &', ())* $ove++ ,nc.

T.an6s a +o3O Eues3ions/ Dan6esc.PnO Dragen/ Muc.as graciasO Pregun3as/ Goin usO "as3-deve+@opensuse.org .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.

You might also like