You are on page 1of 8

Getting Started with C++

4.1 Introduction
History of C++ langauge:
Developed at AT&T Bell a!s in t"e early 1#$%&s !y B'arne (troustrup
C++ ) C + (i*ula +,
T"e na*e C++ -as given !y .ic/ 0ascitti -"ere ++ is t"e incre*ent operator
4.2 C++ Character Set
It is a set of valid c"aracters t"at a langauge can recogni1e 2 a letter3 digit or any ot"er
sign
etters: a21 4A25
Digits: %2#
(pecial (y*!ols: space3 +32 3637 etc.
8"ite spaces: !lan/ space3 "ori1ontal ta! etc..
9t"er c"aracters: :;+ A(CII c"aracters
4.< T9=>?(
To/en definition: T"e s*allest indivdual unit in a progra* is called as a to/en
C++ To/ens are:
1@ =ey-ords
:@ Identifiers
<@ iterals
4@ Aunctuators
;@ 9perators
Definition of keyword:
Conveys a special *eaning to t"e language co*plier.
.eserved for special purpose
Cannot !e used as nor*al identifier na*es
Identifiers containing a dou!le underscore BC C @ are reserved for use !y C++
i*ple*entations and standard li!raries and s"ould !e avoided !y users
>Da*ples: auto3 !rea/3 as*3 auto 3 !rea/3continue etc.
Definition of Identifiers
An identifier is an ar!itarily long seEuence of letters and digits.
T"e first c"aracter *ust !e a letter4 t"e underscore C counts as a letter.
Fpper and lo-er case letters are different.All c"aracters are i*portant.
C++ is case2sensitive 2 Bie@ upper case letters and lo-er case letters are treated
differently.
>g: 0GHI> and *yfile are different identifiers
Definition of Literals
etters Boften referred as constants@ are data ite*s t"at never c"ange t"eir value during
a progra* run
Types of iterals in C++
integer2constants
It *ust "ave at least one digit and *ust not "ave any deci*al point.It *ay
contain eit"er a + or a negative sign. A nu*!er -it" no sign is assu*ed to !e
positive.T"ere are < types of integer constants
a@ Deci*al Integer Constant BBase 1%@ : An integer constant consisting of a seEuence
of digits is ta/en to !e a deci*al integer constant unless it starts -it" a %. >g:
1:<43413+#,321, .Any co*!ination of digits !et-een %2#
!@ 9ctal Integer ConstantBBase $@
A seEuence of digits starting -it" a % is ta/en to !e an 9ctal integer. >g: to convert
fro* deci*al to 9ctal t"e follo-ing procedure needs to !e follo-ed:
>D: Convert :1 to t"e !ase 1% 22222222I :1 to !ase $
:1 divided !y $
$6: )1+ J ) : 4 . ) ;
8e "ave to reverse t"e values and give t"e 9ctal representation
:1 to t"e !ase 1% ) ;: to t"e !ase $
.eference in/: "ttp:77---.-i/i"o-.co*7Convert2fro*2Deci*al2to29ctal
Algorit"*: >ac" ti*e -e need to /eep noting do-* t"e re*ainder and /eep
dividing t"e Euotient !y $ unless -e get a re*aninder t"at is less t"an $.
c@ HeDadeci*al BBase 1+@
A seEuence of digits preceded !y %D or %K is ta/en to a "eDadeci*al integer
HeDa2deci*al digits are represented fro* %2# and t"e re*aining are represented as
follo-s:
1% 2 A
11 2 B
1:2 C
1<2 D
142 >
1; 2 H
>D: 1: ) %KC
.eference: "ttp:77---.-i/i"o-.co*7Convert2fro*2Deci*al2to2HeDadeci*al
T"e suffiD 2 l or Brepresentation of anu*!er as long@
T"e suffiD 2 u or F BBrepresentation of a nu*!er as unsigned@
c"aracter2constants
A c"aracter constant in C++ *ust !e contain one c"aracter and *ust !e
enclosed !y single Euotation *ar/s. T"e representation of a c"aracter constant in t"e
underlying *ac"ine c"aracter set is as a nu*eric value.
A25 : +;2#1 4 a21: #,21:+
?on grap"ic c"aracters are also allo-ed Bie 3 t"e c"aracter st"at cannot !e typed directly
fro* t"e /ey2!oard@ >g: !ac/space3 ta! etc.T"is is acco*plis"ed !y t"e use of escape
seEuences B!ac/2slas"@. He- eDa*ples are as follo-s:
La : audi!le !ell
L! : !ac/space
Lf: for*feed
LM : Dou!le Euote
T"ese c"aracters carry a special *eaning -"en used -it" !ac/2slas".An escape
seEuence represents a single c"aracter.
Hloating2constants
T"ese are also called as real constants.T"ey can !e represented in : for*s:
1@ Hractional for*
A real constant in fractional for* *ust "ave at least one digit !efore t"e
deci*al sign and one digit after t"e deci*al sign. It can "ave eit"er a positive or a
negative sign !efore it.A nu*!er -it" no sign is assu*ed to !e positive. >g: :.%3 1,.;32
1<.%32%.%%+:;
:@ >Dponent for*
A real constant in >Dponent for* consists of : parts: 0antissa and >Dponent.
>g: ;.$ can !e represented as %.;$ 6 1% po-er 1 or %.;$>%1
-"ere %.;$ is called as t"e *antissa and %1 is called t"e eDponent
(tring2constants : A string literal is a seEuence of c"aracters surrounded !y
dou!le Euotes. M0ultiple c"aracterM constants are referred to as string2literals.
Difference !et-een 0ulti2c"aracter constants and string2literals
0ulti2c"aracter constants: used for -riting octal nu*!ers or "eDadeci*al
nu*!ers or A(CII nu*!ers
(tring iteral: T"is is treated as an array of c"aracters
-"ere eac" c"aracter can !e referenced separately
(tring iterals are ter*inated !y &L%&. >D:
(i1e of string lietral Ma!cM ) 4 Binternally represented as Ma!cL%M in co*puter&s
*e*ory
Punctuators
a@ Brac/ets: t"is indicates array su!2script >g: int arrayN1%O 2 T"is indicates an integer
array of si1e 1% -"ere eac" int occupies : !ytes in *e*ory
Dou!le di*ensional array eg: c"ar strN1%ON<%O
1st di*ension ) 1% indicates t"e nu*!er of strings
:nd di*ension ) <% indicates t"e si1e of eac" string
!@ Aarent"eses: T"is is used to represent function calls and function para*eters. Also3
used to group eDpressions and isolate conditional eDpressions.
>D: function addBint a3 int !@
P return Ba+!@4
Q
void *ainB@
P int a3!4
cinIIaII!4
int addCvar) addBa3!@4
coutRR addCvar4
Q
77 eDpression evaluation
eD: Ba 6! 7 BD+y@ 2t @ : -"atever is enclosed -it"in parant"eses is evaluated first.
T"e C++ co*piler -ill first evaluate BD+y@ . et D+y ) su*
et a6! ) prod Bsince *ultiplication operator "as a "ig"er precedence co*pared to
addtion7su!traction@
T"e a!ove eDpression -ill !e replaced !y : prod 7 su* 2t
(ince division operator "as got a "ig"er precedence co*pared to su!traction operator3
follo-ing occurs:
let prod7su* ) divCvalue
t"e final eDpression value evaluates to divCvalue 2 t
c@ Braces: 9pening and closing !races indicate t"e !eginning and end of a co*pound
state*ent . >D: class Aerson P
data *e*!er declaration
*e*!er funtion declaration
Q
d@ Co**a: t"is is used as a seperator in a function argue*ent list.
>D: function addBint a3 int !3 int c@
e@ (e*icolon: used as a state*ent ter*inator in c++.
>D: int a3! 4
f@ Colon: T"is is used to indicate a la!eled stat*ent
>D: goto D 4 -"ere D is a la!el
int *ainB@
P int a3!4
cinIIaII!4
goto D4
float c3d4
c"ar e3f4
D: coutRRaRR!4
return %4
Q
77 since -e "ave used t"e goto state*ent 3 all t"e state*ents after t"e goto are s/ipped
Bin t"is case3 t"e co*piler does not eDecute t"e float and c"ar state*ents . It directly
goes to t"e la!el D @
g@ Asteris/ : T"is is used for pointer declaration to a particular data type.
>D: int 6 ptr4 declares a pointer to an int varia!le t"at "olds t"e address of an integer
varia!le in *e*ory
Fsage: int *ainB@
P int 6 ptr4
int a) 1%4
ptr ) &a4
coutRRptr RRMis t"e address of aM4
coutRR6ptr RRMis t"e value of varia!le aM
return %4
Q
?ote: T"is operator can also !e used for perfor*ing *ultiplication
"@ >llipsis: T"is is used in t"e for*al argue*ent list of function prototypes to indicate a
varia!le nu*!er of argue*ents Bie@ any nu*!er of argue*ents can !e present and can
!e of any data type.
i@ >Eual to sign: used for varia!le initialisation >D: int a );4
used as an assign*ent operator in eDpressions >D: int 1 ) Ba+! 6Bc 7D Sy@@
'@ Aound sign: used in pre2processor directive >g: TincludeRiostrea*."I
BTT@ : used during t"e pre2processor scanning p"ase !y t"e co*piler
!erators
9perators are to/ens t"at trigger so*e co*putation -"en applied to varia!les and
ot"er o!'ects in an eDpression.T"ere are different /inds of unary operators.T"ese
include a single operand to operate upon.
Fnary operators includes t"e follo-ing:
1@ & Address operator 2 >D: int 6 ptr ) &varia!le . Here3 & operates on a single operand
-"ic" is t"e varia!le.
:@ Indirection operator: B6@ >D: Declaration of a pointer Bie@ int 6 ptr4
6 operates on t"e single operand ptr
<@ Fnary Alus 2 >D: +;
4@ Fnary 0inus 2 >D: 2,
;@ Bit-ise co*ple*ent : >D: U ; : !inary representation of ; ) 1%1 . Bit-ise co*ple*ent
c"anges t"e %&s to ones and 1&s to %&s
Hence3 1%1 "as t"e !it-ise co*ple*ent ) %1% ) :
Conversion fro* deci*al to !inary reference:
"ttp:77---.-i/i"o-.co*7Convert2fro*2Deci*al2to2Binary
+@ Incre*ent operator: ++ >D: ++; ) + Bpre2incre*ent !ecause t"e ++ sign is !efore t"e
value ;@.
,@ Decre*ent operator: 22; ) 4 Bpre2decre*ent operaor !eacuse t"e 22 sign is !efore t"e
value ;@
?ote: 8e -ill also loo/ at t"e concepts of post2incre*ent and post2decre*ent
$@ V 2 ogical ?egation . >D: Vtrue ) false 4 Vfalse )true

You might also like