PREPROCESSOR
/ /C o m m e n tt oe n do fl i n e / *M u l t i l i n ec o m m e n t* / / /I n s e r ts t a n d a r dh e a d e rf i l e / /I n s e r tf i l ei nc u r r e n td i r e c t o r y / /R e p l a c eXw i t hs o m et e x t / /R e p l a c eF ( 1 , 2 )w i t h1 + 2 / /L i n ec o n t i n u a t i o n / /R e m o v ed e f i n i t i o n / /C o n d i o n a lc o m p i l a t i o n( # i f d e fX ) / /O p t i o n a l( # i f n d e fXo r# i f! d e f i n e d ( X ) ) / /R e q u i r e da f t e r# i f ,# i f d e f
LITERALS
2 5 5 ,0 3 7 7 ,0 x f f 2 1 4 7 4 8 3 6 4 7 L ,0 x 7 f f f f f f f l 1 2 3 . 0 ,1 . 2 3 e 2 ' a ' ,' \ 1 4 1 ' ,' \ x 6 1 ' ' \ n ' ,' \ \ ' ,' \ ' ' ,' \ " ' " s t r i n g \ n " " h e l l o "" w o r l d " t r u e ,f a l s e
/ /I n t e g e r s( d e c i m a l ,o c t a l ,h e x ) / /L o n g( 3 2 b i t )i n t e g e r s / /d o u b l e( r e a l )n u m b e r s / /C h a r a c t e r( l i t e r a l ,o c t a l ,h e x ) / /N e w l i n e ,b a c k s l a s h ,s i n g l eq u o t e ,d o u b l eq u o t e / /A r r a yo fc h a r a c t e r se n d i n gw i t hn e w l i n ea n d\ 0 / /C o n c a t e n a t e ds t r i n g s / /b o o lc o n s t a n t s1a n d0
DECLARATIONS
i n tx ; / /D e c l a r ext ob ea ni n t e g e r( v a l u eu n d e f i n e d ) i n tx = 2 5 5 ; / /D e c l a r ea n di n i t i a l i z ext o2 5 5 s h o r ts ;l o n gl ; / /U s u a l l y1 6o r3 2b i ti n t e g e r( i n tm a yb ee i t h e r ) c h a rc = ' a ' ; / /U s u a l l y8b i tc h a r a c t e r u n s i g n e dc h a ru = 2 5 5 ;s i g n e dc h a rs = 1 ; / /c h a rm i g h tb ee i t h e r u n s i g n e dl o n gx = 0 x f f f f f f f f L ; / /s h o r t ,i n t ,l o n ga r es i g n e d f l o a tf ;d o u b l ed ; / /S i n g l eo rd o u b l ep r e c i s i o nr e a l( n e v e ru n s i g n e d ) b o o lb = t r u e ; / /t r u eo rf a l s e ,m a ya l s ou s ei n t( 1o r0 ) i n ta ,b ,c ; / /M u l t i p l ed e c l a r a t i o n s i n ta [ 1 0 ] ; / /A r r a yo f1 0i n t s( a [ 0 ]t h r o u g ha [ 9 ] ) i n ta [ ] = { 0 , 1 , 2 } ; / /I n i t i a l i z e da r r a y( o ra [ 3 ] = { 0 , 1 , 2 } ;) i n ta [ 2 ] [ 3 ] = { { 1 , 2 , 3 } , { 4 , 5 , 6 } } ; / /A r r a yo fa r r a yo fi n t s c h a rs [ ] = " h e l l o " ; / /S t r i n g( 6e l e m e n t si n c l u d i n g' \ 0 ' ) i n t *p ; / /pi sap o i n t e rt o( a d d r e s so f )i n t c h a r *s = " h e l l o " ; / /sp o i n t st ou n n a m e da r r a yc o n t a i n i n g" h e l l o " v o i d *p = N U L L ; / /A d d r e s so fu n t y p e dm e m o r y( N U L Li s0 ) i n t &r = x ; / /ri sar e f e r e n c et o( a l i a so f )i n tx e n u mw e e k e n d{ S A T , S U N } ; / /w e e k e n di sat y p ew i t hv a l u e sS A Ta n dS U N e n u mw e e k e n dd a y ; / /d a yi sav a r i a b l eo ft y p ew e e k e n d e n u mw e e k e n d{ S A T = 0 , S U N = 1 } ; / /E x p l i c i tr e p r e s e n t a t i o na si n t e n u m{ S A T , S U N }d a y ; / /A n o n y m o u se n u m t y p e d e fS t r i n gc h a r * ; / /S t r i n gs ;m e a n sc h a r *s ;
http://www.sourcepole.com/sources/programming/cpp/cppqref.html 1/8
4/25/2014
c o n s ti n tc = 3 ; c o n s ti n t *p = a ; i n t *c o n s tp = a ; c o n s ti n t *c o n s tp = a ; c o n s ti n t &c r = x ;
STORAGE CLASSES
i n tx ; s t a t i ci n tx ; e x t e r ni n tx ; / /A u t o( m e m o r ye x i s t so n l yw h i l ei ns c o p e ) / /G l o b a ll i f e t i m ee v e ni fl o c a ls c o p e / /I n f o r m a t i o no n l y ,d e c l a r e de l s e w h e r e
STATEMENTS
x = y ; i n tx ; ; { i n tx ; a ; } i f( x )a ; e l s ei f( y )b ; e l s ec ; w h i l e( x )a ; f o r( x ;y ;z )a ; d oa ;w h i l e( x ) ; s w i t c h( x ){ c a s eX 1 :a ; c a s eX 2 :b ; d e f a u l t :c ; } b r e a k ; c o n t i n u e ; r e t u r nx ; t r y{a ;} c a t c h( Tt ){b ;} c a t c h( . . . ){c ;} / /I fxi st r u e( n o t0 ) ,e v a l u a t ea / /I fn o txa n dy( o p t i o n a l ,m a yb er e p e a t e d ) / /I fn o txa n dn o ty( o p t i o n a l ) / /R e p e a t0o rm o r et i m e sw h i l exi st r u e / /E q u i v a l e n tt o :x ;w h i l e ( y ){ a ;z ; } / /E q u i v a l e n tt o :a ;w h i l e ( x )a ; / /xm u s tb ei n t / /I fx= =X 1( m u s tb eac o n s t ) ,j u m ph e r e / /E l s ei fx= =X 2 ,j u m ph e r e / /E l s ej u m ph e r e( o p t i o n a l ) / /E v e r ye x p r e s s i o ni sas t a t e m e n t / /D e c l a r a t i o n sa r es t a t e m e n t s / /E m p t ys t a t e m e n t / /Ab l o c ki sas i n g l es t a t e m e n t / /S c o p eo fxi sf r o md e c l a r a t i o nt oe n do fb l o c k / /I nC ,d e c l a r a t i o n sm u s tp r e c e d es t a t e m e n t s
/ /J u m po u to fw h i l e ,d o ,o rf o rl o o p ,o rs w i t c h / /J u m pt ob o t t o mo fw h i l e ,d o ,o rf o rl o o p / /R e t u r nxf r o mf u n c t i o nt oc a l l e r
FUNCTIONS
i n tf ( i n tx ,i n t ) ; v o i df ( ) ; v o i df ( i n ta = 0 ) ; f ( ) ; i n l i n ef ( ) ; f ( ){s t a t e m e n t s ;} To p e r a t o r + ( Tx ,Ty ) ; To p e r a t o r ( Tx ) ; To p e r a t o r + + ( i n t ) ; / /fi saf u n c t i o nt a k i n g2i n t sa n dr e t u r n i n gi n t / /fi sap r o c e d u r et a k i n gn oa r g u m e n t s / /f ( )i se q u i v a l e n tt of ( 0 ) / /D e f a u l tr e t u r nt y p ei si n t / /O p t i m i z ef o rs p e e d / /F u n c t i o nd e f i n i t i o n( m u s tb eg l o b a l ) / /a + b( i ft y p eT )c a l l so p e r a t o r + ( a ,b ) / /ac a l l sf u n c t i o no p e r a t o r ( a ) / /p o s t f i x+ +o r-( p a r a m e t e ri g n o r e d )
2/8
http://www.sourcepole.com/sources/programming/cpp/cppqref.html
4/25/2014
e x t e r n" C "{ v o i df ( ) ; }
/ /f ( )w a sc o m p i l e di nC
Function parameters and return values may be of any type. A function must either be declared or defined before it is used. It may be declared first and defined later. Every program consists of a set of a set of global variable declarations and a set of function definitions (possibly in separate files), one of which must be:
i n tm a i n ( ) {s t a t e m e n t s . . .} o r i n tm a i n ( i n ta r g c ,c h a r *a r g v [ ] ){s t a t e m e n t s . . .}
argv is an array of argc strings from the command line. By convention, main returns status 0 if successful, 1 or higher for errors. Functions with different parameters may have the same name (overloading). Operators except :: . .* ?: may be overloaded. Precedence order is not affected. New operators may not be created.
EXPRESSIONS
Operators are grouped by precedence, highest first. Unary operators and assignment evaluate right to left. All others are left to right. Precedence does not affect order of evaluation, which is undefined. There are no run time checks for arrays out of bounds, invalid pointers, etc.
T : : X N : : X : : X t . x p > x a [ i ] f ( x , y ) T ( x , y ) x + + x t y p e i d ( x ) t y p e i d ( T ) d y n a m i c _ c a s t < T > ( x ) s t a t i c _ c a s t < T > ( x ) r e i n t e r p r e t _ c a s t < T > ( x ) c o n s t _ c a s t < T > ( x ) s i z e o fx s i z e o f ( T ) + + x x ~ x ! x x + x & x * p n e wT n e wT ( x ,y ) n e wT [ x ] d e l e t ep d e l e t e [ ]p ( T )x / /N a m eXd e f i n e di nc l a s sT / /N a m eXd e f i n e di nn a m e s p a c eN / /G l o b a ln a m eX / /M e m b e rxo fs t r u c to rc l a s st / /M e m b e rxo fs t r u c to rc l a s sp o i n t e dt ob yp / /i ' t he l e m e n to fa r r a ya / /C a l lt of u n c t i o nfw i t ha r g u m e n t sxa n dy / /O b j e c to fc l a s sTi n i t i a l i z e dw i t hxa n dy / /A d d1t ox ,e v a l u a t e st oo r i g i n a lx( p o s t f i x ) / /S u b t r a c t1f r o mx ,e v a l u a t e st oo r i g i n a lx / /T y p eo fx / /E q u a l st y p e i d ( x )i fxi saT / /C o n v e r t sxt oaT ,c h e c k e da tr u nt i m e / /C o n v e r t sxt oaT ,n o tc h e c k e d / /I n t e r p r e tb i t so fxa saT / /C o n v e r t sxt os a m et y p eTb u tn o tc o n s t / /N u m b e ro fb y t e su s e dt or e p r e s e n to b j e c tx / /N u m b e ro fb y t e st or e p r e s e n tt y p eT / /A d d1t ox ,e v a l u a t e st on e wv a l u e( p r e f i x ) / /S u b t r a c t1f r o mx ,e v a l u a t e st on e wv a l u e / /B i t w i s ec o m p l e m e n to fx / /t r u ei fxi s0 ,e l s ef a l s e( 1o r0i nC ) / /U n a r ym i n u s / /U n a r yp l u s( d e f a u l t ) / /A d d r e s so fx / /C o n t e n t so fa d d r e s sp( * & xe q u a l sx ) / /A d d r e s so fn e w l ya l l o c a t e dTo b j e c t / /A d d r e s so faTi n i t i a l i z e dw i t hx ,y / /A d d r e s so fa l l o c a t e dn e l e m e n ta r r a yo fT / /D e s t r o ya n df r e eo b j e c ta ta d d r e s sp / /D e s t r o ya n df r e ea r r a yo fo b j e c t sa tp / /C o n v e r txt oT( o b s o l e t e ,u s e. . _ c a s t < T > ( x ) )
http://www.sourcepole.com/sources/programming/cpp/cppqref.html
3/8
4/25/2014
x*y x/y x%y x+y x-y x< <y x> >y x<y x< =y x>y x> =y x= =y x! =y x&y x^y x|y x& &y x| |y x=y x+ =y x?y:z t h r o wx x,y
/ /M u l t i p l y / /D i v i d e( i n t e g e r sr o u n dt o w a r d0 ) / /M o d u l o( r e s u l th a ss i g no fx ) / /A d d ,o r& x [ y ] / /S u b t r a c t ,o rn u m b e ro fe l e m e n t sf r o m* xt o* y / /xs h i f t e dyb i t st ol e f t( x*p o w ( 2 ,y ) ) / /xs h i f t e dyb i t st or i g h t( x/p o w ( 2 ,y ) ) / /L e s st h a n / /L e s st h a no re q u a lt o / /G r e a t e rt h a n / /G r e a t e rt h a no re q u a lt o / /E q u a l s / /N o te q u a l s / /B i t w i s ea n d( 3&6i s2 ) / /B i t w i s ee x c l u s i v eo r( 3^6i s5 ) / /B i t w i s eo r( 3|6i s7 ) / /xa n dt h e ny( e v a l u a t e syo n l yi fx( n o t0 ) ) / /xo re l s ey( e v a l u a t e syo n l yi fxi sf a l s e( 0 ) ) / /A s s i g nyt ox ,r e t u r n sn e wv a l u eo fx / /x=x+y ,a l s o=* =/ =< < => > =& =| =^ = / /yi fxi st r u e( n o n z e r o ) ,e l s ez / /T h r o we x c e p t i o n ,a b o r t si fn o tc a u g h t / /e v a l u a t e sxa n dy ,r e t u r n sy( s e l d o mu s e d )
CLASSES
c l a s sT{ / /An e wt y p e p r i v a t e : / /S e c t i o na c c e s s i b l eo n l yt oT ' sm e m b e rf u n c t i o n s p r o t e c t e d : / /A l s oa c c e s s a b l et oc l a s s e sd e r i v e df r o mT p u b l i c : / /A c c e s s a b l et oa l l i n tx ; / /M e m b e rd a t a v o i df ( ) ; / /M e m b e rf u n c t i o n v o i dg ( ){ r e t u r n ; } / /I n l i n em e m b e rf u n c t i o n v o i dh ( )c o n s t ; / /D o e sn o tm o d i f ya n yd a t am e m b e r s i n to p e r a t o r + ( i n ty ) ; / /t + ym e a n st . o p e r a t o r + ( y ) i n to p e r a t o r ( ) ; / /tm e a n st . o p e r a t o r ( ) T ( ) :x ( 1 ){ } / /C o n s t r u c t o rw i t hi n i t i a l i z a t i o nl i s t T ( c o n s tT &t ) :x ( t . x ){ } / /C o p yc o n s t r u c t o r T &o p e r a t o r = ( c o n s tT &t ){ x = t . x ;r e t u r n* t h i s ;} / /A s s i g n m e n to p e r a t o r ~ T ( ) ; / /D e s t r u c t o r( a u t o m a t i cc l e a n u pr o u t i n e ) e x p l i c i tT ( i n ta ) ; / /A l l o wt = T ( 3 )b u tn o tt = 3 o p e r a t o ri n t ( )c o n s t{ r e t u r nx ; } / /A l l o w si n t ( t ) f r i e n dv o i di ( ) ; / /G l o b a lf u n c t i o ni ( )h a sp r i v a t ea c c e s s f r i e n dc l a s sU ; / /M e m b e r so fc l a s sUh a v ep r i v a t ea c c e s s s t a t i ci n ty ; / /D a t as h a r e db ya l lTo b j e c t s s t a t i cv o i dl ( ) ; / /S h a r e dc o d e . M a ya c c e s syb u tn o tx c l a s sZ{ } ; / /N e s t e dc l a s sT : : Z
http://www.sourcepole.com/sources/programming/cpp/cppqref.html 4/8
4/25/2014
t y p e d e fi n tV ; / /T : : Vm e a n si n t } ; v o i dT : : f ( ){ / /C o d ef o rm e m b e rf u n c t i o nfo fc l a s sT t h i s > x=x ; } / /t h i si sa d d r e s so fs e l f( m e a n sx = x ; ) i n tT : : y=2 ; / /I n i t i a l i z a t i o no fs t a t i cm e m b e r( r e q u i r e d ) T : : l ( ) ; / /C a l lt os t a t i cm e m b e r s t r u c tT{ / /E q u i v a l e n tt o :c l a s sT{p u b l i c : v i r t u a lv o i df ( ) ; / /M a yb eo v e r r i d d e na tr u nt i m eb yd e r i v e dc l a s s v i r t u a lv o i dg ( ) = 0 ;} ; / /M u s tb eo v e r r i d d e n( p u r ev i r t u a l ) c l a s sU :p u b l i cT{ } ; / /D e r i v e dc l a s sUi n h e r i t sa l lm e m b e r so fb a s eT c l a s sV :p r i v a t eT{ } ; / /I n h e r i t e dm e m b e r so fTb e c o m ep r i v a t e c l a s sW :p u b l i cT ,p u b l i cU{ } ; / /M u l t i p l ei n h e r i t a n c e c l a s sX :p u b l i cv i r t u a lT{ } ;/ /C l a s s e sd e r i v e df r o mXh a v eb a s eTd i r e c t l y
All classes have a default copy constructor, assignment operator, and destructor, which perform the corresponding operations on each data member and each base class as shown above. There is also a default no-argument constructor (required to create arrays) if the class has no constructors. Constructors, assignment, and destructors do not inherit.
TEMPLATES
t e m p l a t e< c l a s sT >Tf ( Tt ) ; / /O v e r l o a dff o ra l lt y p e s t e m p l a t e< c l a s sT >c l a s sX{ / /C l a s sw i t ht y p ep a r a m e t e rT X ( Tt ) ;} ; / /Ac o n s t r u c t o r t e m p l a t e< c l a s sT >X < T > : : X ( Tt ){ } / /D e f i n i t i o no fc o n s t r u c t o r X < i n t >x ( 3 ) ; / /A no b j e c to ft y p e" Xo fi n t " t e m p l a t e< c l a s sT ,c l a s sU = T ,i n tn = 0 > / /T e m p l a t ew i t hd e f a u l tp a r a m e t e r s
NAMESPACES
4/25/2014
/ /P r i n tt os t a n d a r de r r o r( n o tr e d i r e c t e d ) / /R e a do n ec h a r( a sa ni n t )o rE O Ff r o mf / /P u tb a c ko n ect of / /g e t c ( s t d i n ) ; / /f p r i n t f ( f ," % c " ,c ) ; / /p u t c ( c ,s t d o u t ) ; / /R e a dl i n ei n t oc h a rs [ n ]f r o mf . N U L Li fE O F / /f g e t s ( s ,I N T _ M A X ,f ) ;n ob o u n d sc h e c k / /R e a dnb y t e sf r o mft os ,r e t u r nn u m b e rr e a d / /W r i t enb y t e so fst of ,r e t u r nn u m b e rw r i t t e n / /F o r c eb u f f e r e dw r i t e st of / /P o s i t i o nb i n a r yf i l efa tn / /P o s i t i o ni nf ,1 Li fe r r o r / /f s e e k ( f ,0 L ,S E E K _ S E T ) ;c l e a r e r r ( f ) ; / /I sfa te n do ff i l e ? / /E r r o ri nf ? / /P r i n tc h a r *sa n de r r o rm e s s a g e / /C l e a re r r o rc o d ef o rf / /D e l e t ef i l e ,r e t u r n0i fO K / /R e n a m ef i l e ,r e t u r n0i fO K / /C r e a t et e m p o r a r yf i l ei nm o d e" w b + " / /P u tau n i q u ef i l en a m ei nc h a rs [ L _ t m p n a m ]
STRING.H, CSTRING (Character array handling functions) Strings are type char[] with a '\0' in the last element used.
s t r c p y ( d s t ,s r c ) ; / /C o p ys t r i n g .N o tb o u n d sc h e c k e d s t r c a t ( d s t ,s r c ) ; / /C o n c a t e n a t et od s t .N o tb o u n d sc h e c k e d s t r c m p ( s 1 ,s 2 ) ; / /C o m p a r e ,< 0i fs 1 < s 2 ,0i fs 1 = = s 2 ,> 0i fs 1 > s 2 s t r n c p y ( d s t ,s r c ,n ) ; / /C o p yu pt onc h a r s ,a l s os t r n c a t ( ) ,s t r n c m p ( ) s t r l e n ( s ) ; / /L e n g t ho fsn o tc o u n t i n g\ 0 s t r c h r ( s , c ) ;s t r r c h r ( s , c ) ; / /A d d r e s so ff i r s t / l a s tc h a rci nso r0 s t r s t r ( s ,s u b ) ; / /A d d r e s so ff i r s ts u b s t r i n gi nso r0 / /m e m . . .f u n c t i o n sa r ef o ra n yp o i n t e rt y p e s( v o i d * ) ,l e n g t hnb y t e s m e m m o v e ( d s t ,s r c ,n ) ; / /C o p ynb y t e sf r o ms r ct od s t m e m c m p ( s 1 ,s 2 ,n ) ; / /C o m p a r enb y t e sa si ns t r c m p m e m c h r ( s ,c ,n ) ; / /F i n df i r s tb y t eci ns ,r e t u r na d d r e s so r0 m e m s e t ( s ,c ,n ) ; / /S e tnb y t e so fst oc
4/25/2014
http://www.sourcepole.com/sources/programming/cpp/cppqref.html
4/25/2014
s 1+ =s 2+''+" w o r l d " ;/ /C o n c a t e n a t i o n s 1= =" h e l l ow o r l d " / /C o m p a r i s o n ,a l s o< ,> ,! = ,e t c . s 1 [ 0 ] ; / /' h ' s 1 . s u b s t r ( m ,n ) ; / /S u b s t r i n go fs i z ens t a r t i n ga ts 1 [ m ] s 1 . c _ s t r ( ) ; / /C o n v e r tt oc o n s tc h a r * g e t l i n e ( c i n ,s ) ; / /R e a dl i n ee n d i n gi n' \ n '
UTILITY (Pair)
p a i r < s t r i n g ,i n t >a ( " h e l l o " ,3 ) ; / /A2 e l e m e n ts t r u c t a . f i r s t ; / /" h e l l o " a . s e c o n d ; / /3
http://www.sourcepole.com/sources/programming/cpp/cppqref.html
8/8