Lecture Notes CS102 Part1
-
Upload
manish-tiwari -
Category
Documents
-
view
80 -
download
0
Transcript of Lecture Notes CS102 Part1
OB
JEC
T O
RIE
NTE
D
PRO
GR
AM
MIN
G W
ITH
C
++ (C
ompl
ete)
Pre
pare
d by
Dee
pak
Gou
rFa
culty
-Dep
artm
ent o
f IT
Sir
Pad
ampa
t Sin
ghan
ia S
choo
l of E
ngin
eerin
g
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
2
C &
C++
Diff
eren
ces
C: p
roce
dura
l pro
gram
min
g la
ngua
ge
Prog
ram
min
g is
act
ion-
orie
nted
Func
tion
is b
asic
uni
t of p
rogr
amm
ing
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
3
C &
C++
Diff
eren
ces
(con
td..)
C++
: obj
ect-o
rient
ed p
rogr
amm
ing
lang
uage
Pro
gram
min
g is
obj
ect-o
rient
edC
lass
(a u
ser-
defin
ed d
ata
type
) is
basi
c un
it of
pr
ogra
mm
ing
Attr
ibut
es (d
ata
mem
bers
)B
ehav
iors
or o
pera
tions
(mem
ber f
unct
ions
)O
bjec
ts a
re c
reat
ed (i
nsta
ntia
ted)
from
the
clas
sV
aria
bles
are
inst
ance
s of
bui
lt-in
dat
a ty
pes
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
4
OO
Pro
gram
min
g P
arad
igm
The
mai
n fe
atur
es o
f OO
pro
gram
min
g ar
eEm
phas
is is
on
data
rath
er th
an p
roce
dure
s.Pr
ogra
ms
are
divi
ded
into
OB
JEC
TS.
Dat
a is
hid
den
and
cann
ot b
e ac
cess
ed b
y ex
tern
al fu
nctio
ns.
Obj
ects
m
ay
com
mun
icat
e w
ith
each
ot
her
thro
ugh
func
tions
.N
ew d
ata
and
func
tions
can
be
easi
ly a
dded
w
hene
ver n
eces
sary
.Fo
llow
B
OTT
OM
U
P ap
proa
ch
in
prog
ram
de
sign
.
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
5
Fund
amen
tal C
once
pts
in O
OP
The
mai
n co
ncep
ts a
re a
s fo
llow
s:O
bjec
tsC
lass
esD
ata
Abst
ract
ion
and
Enca
psul
atio
nIn
herit
ance
Poly
mor
phis
mD
ynam
ic b
indi
ngM
essa
ge p
assi
ng
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
6
Wha
t is
an O
bjec
t:D
efin
ition
An
obje
ct is
an
entit
y th
at h
as
Stat
e (w
hat t
he o
bjec
t loo
ks li
ke) C
lass
M
embe
rs -
Attr
ibut
esB
ehav
ior (
how
the
obje
ct a
cts)
Cla
ss
Mem
ber F
unct
ions O
bjec
tsO
bjec
tsB
ehav
iors
Beh
avio
rsO
bjec
tsO
bjec
tsSt
ate
Stat
eU
ser
Use
r
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
7
Exam
ple
of O
bjec
t
FUN
CTI
ON
STo
tal
Ave
rage
------
----
DA
TAN
ame
Dat
e_of
_birt
hM
arks
------
----
Obj
ect:
STU
DEN
T
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
8
CLA
SSO
bjec
t co
ntai
n da
ta,
and
code
to
m
anip
ulat
e th
at d
ata.
The
ent
ire s
et o
f da
ta a
nd c
ode
of a
n ob
ject
can
be
mad
e a
user
def
ined
dat
a ty
pe w
ith t
he h
elp
of
a C
LASS
. In
fact
, obj
ects
are
var
iabl
e of
th
e ty
pe C
LASS
.
Thus
a C
LASS
is
a co
llect
ion
of o
bjec
ts
of s
imila
r typ
es.
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
9
Cla
ss D
efin
ition
(con
td..)
In C
++, a
cla
ss is
dec
lare
d us
ing
the
clas
ske
ywor
d.
The
synt
ax o
f a c
lass
dec
lara
tion
is s
imila
r to
that
of
a st
ruct
ure.
Its
gene
ral f
orm
is,
clas
scl
ass-
nam
e {
priv
ate:
// pr
ivat
e fu
nctio
ns a
nd v
aria
bles
pu
blic
://
publ
ic fu
nctio
ns a
nd v
aria
bles
} o
bjec
t-lis
t;In
a c
lass
dec
lara
tion
the
obje
ct-li
st is
opt
iona
l.
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
10
Exam
ple
1#i
nclu
de <
iost
ream
.h>
usin
g na
mes
pace
std
;cl
ass
pers
on{
priv
ate:
char
nam
e[30
];in
tage
;pu
blic
:vo
id g
etda
ta(v
oid)
;vo
id p
utda
ta(v
oid)
;};
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
11
Exam
ple
1 (c
ontd
..)vo
id p
erso
n ::
getd
ata(
void
){
cout
<< “E
nter
nam
e: ”
;ci
n>>
nam
e;co
ut<<
“Ent
er a
ge:
”;ci
n>>
age
;}
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
12
Exam
ple
1 (c
ontd
..)
void
per
son
:: pu
tdat
a(vo
id)
{co
ut<<
“\n
Nam
e: ”
<<
nam
e;co
ut<<
“\n
Age
: ” <
< ag
e;}
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
13
Exam
ple
1 (c
ontd
..)m
ain
(){
pers
on p
;p.
getd
ata(
);p.
putd
ata(
);}
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
14
Exam
ple
2#i
nclu
de <
iost
ream
.h>
usin
g na
mes
pace
std
;cl
ass
item
{pr
ivat
e:in
tnum
ber;
float
cos
t;pu
blic
:vo
id g
etda
ta(in
ta, f
loat
b);
void
put
data
(voi
d);
};
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
15
Exam
ple
2 (c
ontd
..)vo
id it
em ::
get
data
(voi
d){
num
ber =
a;
cost
= b
;}
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
16
Exam
ple
2 (c
ontd
..)
void
item
:: p
utda
ta(v
oid)
{co
ut<<
“\n
Num
ber:
” <<
num
ber <
< “\n
”;co
ut<<
“\n
Cos
t: ”
<< c
ost <
< “\n
”;}
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
17
Exam
ple
2 (c
ontd
..)m
ain
(){
item
x;
cout
<< “
\n O
bjec
t x ”
<<
“\n”
;x.
getd
ata(
100,
175.
99);
x.pu
tdat
a();
item
y;
cout
<< “
\n O
bjec
t y ”
<<
“\n”
;y.
getd
ata(
200,
285.
99);
y.pu
tdat
a();
}
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
18
Cla
ss D
efin
ition
(con
td..)
//Fraction abstract data type (ADT) definition
class Fraction{
public:
//accessible anywhere in program
Fraction();
//member function prototype
void set(int, int); //member function prototype
void print();
//member function prototype
private:
//accessible only to member functions
intnum;
//data member
intden;
//data member
};
//terminate class definition with a semicolon
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
19
Mem
ber A
cces
s S
peci
fiers
Publ
icA
ny d
ata
mem
ber
or d
ata
func
tion
decl
ared
afte
r th
is is
acc
essi
ble
anyw
here
in th
e pr
ogra
mPr
ivat
eA
ny d
ata
mem
ber
or d
ata
func
tion
decl
ared
afte
r th
is is
onl
y ac
cess
ible
to m
embe
r fun
ctio
ns o
f the
cl
ass
If no
spe
cifie
rs a
re d
ecla
red,
the
n th
e de
faul
t is
pr
ivat
eC
an li
st s
peci
fiers
in a
ny o
rder
Prot
ecte
dU
sed
for i
nher
itanc
e
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
20
Mem
ber F
unct
ion
Def
initi
ons
//Constructor initializes each data member
Fraction::Fraction(){
num = 0;
den = 1;
} //Set a new Fraction value & check data
void Fraction::set(intn , intd ){
if(d== 0) d = 1;
if(d< 0) {d = -d; n = -n;}
num = n; den = d;
} //Print a Fraction
void Fraction::print(){
cout<< num << " / " << den;
}
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
21
Driv
er P
rogr
am// Driver program to test class Fraction
void main(){
//instantiate object t of class Fraction
Fraction t;
t.print();
// 0 / 1
//set the data
t.set( 13, -27);
t.print();
// -13 / 27
//attempt invalid data
t.set( 99, 0);
t.print();
// 99 / 1
}
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
22
Acc
essi
ng C
lass
Mem
bers
class Fraction {
public:
Fraction();
void print();
int
num, den; //Not good programming practice
};
Fraction::Fraction() {num = 0; den = 1; }
void Fraction::print(){cout<<num<<"/"<<den<<endl;}
void main(){
Fraction t;
//instantiate a fraction
Fraction *ptr
= &t;
//pointer to t
Fraction &ref = t;
//reference to t
t.num
= 1; t.den
= 2; t.print();
// 1/2
ptr->num = 2; ptr->den = 3; ptr->print();// 2/3
ref.num
= 3; ref.den
= 4; ref.print(); // 3/4
}
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
23
Acc
essi
ng C
lass
Mem
bers
class Fraction {
Private:
int
num, den;
public:
Fraction();
void print();
void setNum(int);
void setDen(int);
int
getNum();
int
getDen();
};
void Fraction::setNum(intn){num
= n;}
void Fraction::setDen(intd){if(d==0)d=1; den = d;}
int
Fraction::getNum(){return
num;}
int
Fraction::getDen(){return
den;}
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
24
Enca
psul
atio
n
Abs
trac
tion
Dat
a H
idin
g
Obj
ects
Obj
ects
Beh
avio
rsB
ehav
iors
Obj
ects
Obj
ects
Stat
eSt
ate
Use
rU
ser
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
25
Enca
psul
atio
nD
ata
abst
ract
ion
allo
w p
rogr
amm
ers
to h
ide
data
re
pres
enta
tion
deta
ils b
ehin
d a
(com
para
tivel
y)
sim
ple
set o
f ope
ratio
ns (a
n in
terfa
ce)
Wha
t the
ben
efits
of d
ata
abst
ract
ion?
Red
uces
con
cept
ual l
oad
Pro
gram
mer
s ne
ed to
kno
ws
less
abo
ut th
e re
st o
f the
pr
ogra
mPr
ovid
es fa
ult c
onta
inm
ent
Bug
s ar
e lo
cate
d in
inde
pend
ent c
ompo
nent
sPr
ovid
es a
sig
nific
ant d
egre
e of
inde
pend
ence
of
prog
ram
com
pone
nts
Sep
arat
e th
e ro
les
of d
iffer
ent p
rogr
amm
er
Softw
are
Softw
are
Engi
neer
ing
Engi
neer
ing
Goa
lsG
oals
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
26
Enca
psul
atio
n (c
ontd
..)Th
e w
rapp
ing
up o
f da
ta a
nd f
unct
ions
in
to a
sin
gle
unit
(cal
led
clas
s) i
s kn
own
as
ENC
APSU
LATI
ON
. Th
e da
ta
is
not
acce
ssib
le t
o th
e ou
tsid
e w
orld
, and
onl
y th
ose
func
tions
whi
ch a
re w
rapp
ed in
the
cl
ass
can
acce
ss
it.
Thes
e fu
nctio
ns
prov
ide
the
inte
rfac
e be
twee
n th
e ob
ject
’s
data
and
the
prog
ram
. Th
is i
nsul
atio
n of
the
dat
a fr
om d
irect
ac
cess
by
the
prog
ram
is
calle
d D
ATA
H
IDIN
G o
r IN
FOR
MA
TON
HID
ING
.
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
27
AB
STR
AC
TIO
NA
BST
RAC
TIO
Nre
fers
to
th
e ac
t of
re
pres
entin
g es
sent
ial
feat
ures
w
ithou
t in
clud
ing
the
back
grou
nd
deta
ils
or
expl
anat
ions
. C
LASS
ESus
e th
e co
ncep
t of
abs
trac
tion
and
are
defin
ed a
s a
list
of
abst
ract
at
trib
utes
and
func
tions
to
oper
ate
on th
ese
attr
ibut
es.
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
28
Enca
psul
atio
nC
lass
es, O
bjec
ts a
nd M
etho
dsTh
e un
it of
enc
apsu
latio
n in
an
O-O
PL
is a
cla
ssAn
abs
tract
dat
a ty
peTh
e se
t of v
alue
s is
the
set o
f obj
ects
(or i
nsta
nces
)O
bjec
ts c
an h
ave
aSe
t of i
nsta
nce
attri
bute
s (h
as-a
rela
tions
hip)
Set o
f ins
tanc
em
etho
dsC
lass
es c
an h
ave
aSe
t of c
lass
attr
ibut
esSe
t of c
lass
met
hods
Met
hod
calls
are
M
etho
d ca
lls a
re
know
n as
kn
own
as m
essa
ges
mes
sage
s
The
entir
e se
t of m
etho
ds o
f an
obje
ct is
kno
wn
as th
e m
essa
ge p
roto
colo
r the
mes
sage
inte
rface
of t
he
obje
ct
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
29
Inhe
ritan
ce
Obj
ects
Obj
ects
Beh
avio
rsB
ehav
iors
Obj
ects
Obj
ects
Stat
eSt
ate
Use
rU
ser
Obj
ects
Obj
ects
Beh
avio
rsB
ehav
iors
Obj
ects
Obj
ects
Stat
eSt
ate
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
30
Inhe
ritan
ce (c
ontd
..)In
herit
ance
is
the
proc
ess
by w
hich
obj
ects
of
one
clas
s ac
quire
the
pro
pert
ies
of o
bjec
ts o
f an
othe
r cl
ass.
It
supp
orts
th
e co
ncep
t of
hi
erar
chic
al
clas
sific
atio
n.In
OO
P, th
e co
ncep
t of i
nher
itanc
e pr
ovid
es th
e id
ea o
f re
usab
ility
. Thi
s m
eans
tha
t w
e ca
n ad
d ad
ditio
nal
feat
ures
to
an e
xist
ing
clas
s w
ithou
t m
odify
ing
it.
This
is
poss
ible
by
deriv
ing
a ne
w c
lass
fro
m t
he
exis
ting
one.
The
new
cla
ss w
ill h
ave
the
com
bine
d fe
atur
es o
f bot
h th
e cl
asse
s.
Not
e th
at e
ach
sub-
clas
s de
fines
onl
y th
ose
feat
ures
th
at
are
uniq
ue
to
it.
With
out
the
use
of
clas
sific
atio
n,
each
cla
ss w
ould
hav
e to
exp
licitl
y in
clud
e al
l of i
ts fe
atur
es.
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
31
POLY
MO
RPH
ISM
Poly
mor
phis
m,
a G
reek
ter
m,m
eans
the
abi
lity
to ta
ke m
ore
than
one
form
. An
oper
atio
n m
ay
exhi
bit
diffe
rent
be
havi
ors
in
diffe
rent
in
stan
ces.
Th
e be
havi
or
depe
nds
upon
th
e ty
pe o
f dat
a us
ed in
the
oper
atio
n.Fo
r ex
ampl
e, c
onsi
der
the
oper
atio
n of
add
ition
. Fo
r tw
o nu
mbe
rs, t
he o
pera
tion
will
gen
erat
e a
sum
. If
the
oper
ands
are
str
ings
, th
en t
he
oper
atio
n w
ould
pr
oduc
e a
third
st
ring
by
conc
aten
atio
n.
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
32
POLY
MO
RPH
ISM
(con
td..)
The
proc
ess
of
mak
ing
an
oper
ator
to
ex
hibi
t di
ffere
nt
beha
vior
s in
di
ffere
nt
inst
ance
s is
kn
own
as o
pera
tor o
verlo
adin
g.S
ingl
e fu
nctio
n na
me
can
be
used
to
ha
ndle
di
ffere
nt
num
ber
and
diffe
rent
ty
pes
of
argu
men
ts.
This
is
so
met
hing
si
mila
r to
pa
rticu
lar
wor
d ha
ving
se
vera
l di
ffere
nt
mea
ning
s de
pend
ing
on th
e co
ntex
t. U
sing
a s
ingl
e fu
nctio
n na
me
to p
erfo
rm d
iffer
ent
type
s of
ta
sks
is
know
n as
fu
nctio
n ov
erlo
adin
g.
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
33
POLY
MO
RPH
ISM
(con
td..)
Pol
ymor
phis
m p
lays
an
impo
rtant
rol
e in
al
low
ing
obje
cts
havi
ng d
iffer
ent i
nter
nal
stru
ctur
es t
o sh
are
the
sam
e ex
tern
al
inte
rface
. Th
is m
eans
tha
t a
gene
ral
clas
s of
ope
ratio
ns m
ay b
e ac
cess
ed in
th
e sa
me
man
ner
even
tho
ugh
spec
ific
actio
ns a
ssoc
iate
d w
ith e
ach
oper
atio
n m
ay
diffe
r. Po
lym
orph
ism
is
ex
tens
ivel
y us
ed
in
impl
emen
ting
inhe
ritan
ce.
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
34
DYN
AM
IC B
IND
ING
Bin
ding
ref
ers
to t
he li
nkin
g of
a p
roce
dure
ca
ll to
th
e co
de
to
be
exec
uted
in
re
spon
se
to
the
call.
D
ynam
ic
bind
ing
(als
o kn
own
as l
ate
bind
ing)
mea
ns t
hat
the
code
as
soci
ated
w
ith
a gi
ven
proc
edur
e ca
ll is
not
kno
wn
until
the
time
of a
cal
l at
run-
time.
It is
ass
ocia
ted
with
po
lym
orph
ism
and
inhe
ritan
ce. A
func
tion
call
asso
ciat
ed
with
a
poly
mor
phic
re
fere
nce
depe
nds
on th
e dy
nam
ic ty
pe o
f th
at re
fere
nce.
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
35
MES
SAG
E PA
SSIN
GA
n O
O p
rogr
am c
onsi
sts
of a
set
of
obje
cts
that
co
mm
unic
ate
with
ea
ch
othe
r. Th
e pr
oces
s of
pr
ogra
mm
ing
in
an
OO
L,
ther
efor
e,
invo
lves
th
e fo
llow
ing
basi
c st
eps:
1.C
reat
ing
clas
ses
that
def
ines
obj
ects
and
th
eir b
ehav
ior,
2.C
reat
ing
obje
cts
from
cla
ss d
efin
ition
, and
3.Es
tabl
ishi
ng
com
mun
icat
ion
amon
g ob
ject
s.
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
36
Obj
ects
com
mun
icat
e w
ith o
ne a
noth
er b
y se
ndin
g an
d re
ceiv
ing
info
rmat
ion
muc
h th
e sa
me
way
as
peop
le p
ass
mes
sage
s to
one
an
othe
r. Th
e co
ncep
t of
mes
sage
pas
sing
m
akes
it e
asie
r to
talk
abo
ut b
uild
ing
syst
ems
that
dire
ctly
mod
el o
r sim
ulat
e th
eir r
eal-w
orld
co
unte
rpar
ts.
MES
SAG
E PA
SSIN
G (c
ontd
..)
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
37
MES
SAG
E PA
SSIN
G (c
ontd
..)A
mes
sage
for
an
obje
ct i
s a
requ
est
for
exec
utio
n of
a p
roce
dure
, an
d th
eref
ore
will
inv
oke
a fu
nctio
n in
th
e re
ceiv
ing
obje
ct t
hat
gene
rate
s th
e de
sire
d re
sult.
M
essa
ge p
assi
ngin
volv
es s
peci
fyin
g th
e na
me
of
the
obje
ct,
the
nam
e of
th
e fu
nctio
n (m
essa
ge) a
nd th
e in
form
atio
n to
be
sen
t.
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
38
App
licat
ions
of C
++C
++ i
s a
vers
atile
lan
guag
e fo
r ha
ndlin
g ve
ry
larg
e pr
ogra
ms.
It
is
suita
ble
for
virt
ually
any
pro
gram
min
g ta
sk i
nclu
ding
de
velo
pmen
t of
ed
itors
, co
mpl
iers
, da
taba
ses,
com
mun
icat
ion
syst
ems
and
any
com
plex
real
-life
app
licat
ion
syst
ems.
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
39
App
licat
ions
of C
++ (c
ontd
..)A
pplic
atio
ns:
Sin
ce C
++ a
llow
s us
to c
reat
e hi
erar
chy
rela
ted
obje
cts,
w
e ca
n bu
ild s
peci
al O
O l
ibra
ries
whi
ch c
ane
be u
sed
late
r by
man
y pr
ogra
mm
ers.
Whi
le
C++
is
ab
le
to
map
th
e re
al-w
orld
pr
oble
m
prop
erly
, the
C p
art o
f C++
giv
es th
e la
ngua
ge th
e ab
ility
to
get
clo
se to
the
mac
hine
-leve
l det
ails
.C
++ p
rogr
ams
are
easi
ly m
aint
aina
ble
and
expa
ndab
le.
Whe
n a
new
feat
ure
need
s to
be
impl
emen
ted,
it is
ver
y ea
sy to
add
to th
e ex
istin
g st
ruct
ure
of a
n ob
ject
.It
is e
xpec
ted
that
C++
will
rep
lace
C a
s a
gene
ral-
purp
ose
lang
uage
in th
e ne
ar fu
ture
.
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
40
OU
TPU
T &
INPU
T O
PER
ATO
RO
utpu
t ope
rato
rC
out<
< “C
++ is
bet
ter t
han
C”
This
sta
tem
ent
intr
oduc
es t
wo
new
C++
fea
ture
s,
cout
and
<<.
The
oper
ator
<<
is c
alle
d th
e in
sert
ion
or p
ut to
oper
ator
.In
put o
pera
tor
Cin
>> n
umbe
r;Th
is s
tate
men
t in
trod
uces
tw
o ne
w C
++ f
eatu
res,
ci
nan
d >>
.Th
e op
erat
or
>>
is
know
n as
ex
trac
tion
or g
et to
oper
ator
.
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
41
Stru
ctur
e of
C++
pro
gram
Mai
n fu
nctio
n pr
ogra
mM
embe
r fun
ctio
ns d
efin
ition
sC
lass
dec
lara
tion
Incl
ude
files
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
42
Con
stan
ts, V
aria
bles
, and
Dat
a Ty
pes
A
prog
ram
min
g la
ngua
ge i
s de
sign
ed t
o he
lp
proc
ess
cert
ain
kind
s of
da
ta
cons
istin
g of
nu
mbe
rs,
char
acte
rs,
and
strin
gs a
nd t
o pr
ovid
e us
eful
out
put
know
n as
inf
orm
atio
n. T
he t
ask
of
proc
essi
ng o
f da
ta is
acc
ompl
ishe
d by
exe
cutin
g a
sequ
ence
of
pr
ecis
e in
stru
ctio
ns
calle
d a
prog
ram
. Th
ese
inst
ruct
ions
are
for
med
usi
ng
cert
ain
sym
bols
and
wor
ds a
ccor
ding
to
som
e rig
id r
ules
kno
wn
as s
ynta
x ru
les
(or
gram
mar
). Ev
ery
prog
ram
inst
ruct
ion
mus
t con
firm
pre
cise
ly
to th
e sy
ntax
rule
s of
the
lang
uage
. Lik
e an
y ot
her
lang
uage
, C
++
has
its
own
voca
bula
ry
and
gram
mar
.
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
43
CH
AR
AC
TER
SET
The
char
acte
rs th
at c
an b
e us
ed to
form
w
ords
, nu
mbe
rs,
and
expr
essi
ons
depe
nd u
pon
the
com
pute
r on
whi
ch
the
prog
ram
is
run.
The
cha
ract
ers
in
C++
ar
e gr
oupe
d in
to
the
follo
win
g ca
tego
ries:
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
44
CH
AR
AC
TER
SET
(con
td..)
Lette
rsD
igits
Spec
ial c
hara
cter
sW
hite
spa
ces
The
com
pile
r ig
nore
s w
hite
spa
ces
unle
ss
they
are
par
t of a
str
ing
cons
tant
. Whi
te
spac
es m
ay b
e us
ed to
sep
arat
e w
ords
, bu
t ar
e pr
ohib
ited
betw
een
the
char
acte
rs o
f key
wor
ds a
nd id
entif
iers
.
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
45
C T
OK
ENS
In a
pas
sage
of
text
, in
divi
dual
wor
ds a
nd
punc
tuat
ion
mar
ks
are
calle
d to
kens
. Si
mila
rly,
in
a C
++
prog
ram
th
e sm
alle
st
indi
vidu
al u
nits
are
kno
wn
as C
++ t
oken
s.
C++
has
six
type
s of
toke
ns a
s:K
eyw
ords
Iden
tifie
rsC
onst
ants
Strin
gsO
pera
tors
Spec
ial s
ymbo
ls
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
46
KEY
WO
RD
S &
IDEN
TIFI
ERS
Ever
y C
++ w
ord
is c
lass
ified
as
eith
er a
ke
ywor
d or
id
entif
iers
. Al
l ke
ywor
ds
have
fix
ed m
eani
ng a
nd t
hese
mea
ning
ca
nnot
be
chan
ged.
Key
wor
d se
rves
as
basi
c bu
ildin
g bl
ocks
fo
r pr
ogra
m
stat
emen
ts.
All
keyw
ords
m
ust
be
writ
ten
in l
ower
case
. So
me
com
pile
rs
may
use
add
ition
al k
eyw
ords
tha
t m
ust
be id
entif
ied
from
the
C++
man
ual.
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
47
KEY
WO
RD
S &
IDEN
TIFI
ERS
(con
td..)
Iden
tifie
rs r
efer
to
the
nam
es o
f va
riabl
es,
func
tions
and
arr
ays.
The
se a
re u
ser
defin
ed
nam
es a
nd c
onsi
st o
f a
sequ
ence
of
lette
rs
and
digi
ts,
with
a l
ette
r as
a f
irst
char
acte
r. B
oth
uppe
rcas
e an
d lo
wer
case
le
tters
ar
e pe
rmitt
ed,
alth
ough
le
tters
ar
e co
mm
only
us
ed.
The
unde
rsco
re
char
acte
r is
al
so
perm
itted
in id
entif
iers
. It i
s us
ually
use
d as
a
link
betw
een
two
wor
ds in
long
iden
tifie
rs.
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
48
KEY
WO
RD
S &
IDEN
TIFI
ERS
(con
td..)
Rul
es fo
r ide
ntifi
ers:
Firs
t ch
arac
ter
mus
t be
an
alph
abet
(or
un
ders
core
).M
ust
cons
ist
of o
nly
lette
rs,
digi
ts,
or
unde
rsco
re.
Onl
y fir
st 3
1 ch
arac
ters
are
sig
nific
ant.
Can
not u
se a
key
wor
d.M
ust n
ot c
onta
in w
hite
spa
ce.
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
49
CO
NST
AN
TSC
onst
ants
in
C++
ref
er t
o fix
ed v
alue
s th
at d
o no
t cha
nge
durin
g th
e ex
ecut
ion
of
a pr
ogra
m.
C++
su
ppor
ts
seve
ral
type
s of
con
stan
ts a
s:N
umer
ic C
onst
ants
i) In
tege
r C
onst
ants
ii) R
eal C
onst
ants
Cha
ract
ers
Con
stan
ts
i) Si
ngle
C
hara
cter
C
onst
ants
ii)
St
ring
Con
stan
ts
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
50
INTE
GER
CO
NST
AN
TA
n in
tege
r con
stan
t re
fers
to
a
sequ
ence
of
di
gits
. Th
ere
are
thre
e ty
pes
of
inte
gers
, na
mel
y,
deci
mal
in
tege
r, oc
tal
inte
ger,
and
hexa
deci
mal
in
tege
r.D
ecim
al i
nteg
ers
cons
ists
of
a se
t of
di
gits
, 0
thro
ugh
9,
prec
eded
by
an
op
tiona
l –or
+ s
ign.
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
51
INTE
GER
CO
NST
AN
T (c
ontd
..)A
n oc
tal i
nteg
er c
onst
ant c
onsi
st o
f any
co
mbi
natio
n of
dig
its f
rom
the
set
of
0 th
roug
h 7,
with
a le
adin
g 0.
A s
eque
nce
of d
igits
pre
cede
d by
0x
or
0X
is
cons
ider
ed
as
hexa
deci
mal
in
tege
rs.
They
m
ay
also
in
clud
e al
phab
ets
A th
roug
h F
or a
thr
ough
f.
The
lette
rs A
thr
ough
F r
epre
sent
the
nu
mbe
rs 1
0 th
roug
h 15
.
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
52
REA
L C
ON
STA
NT
Inte
ger
num
bers
ar
e in
adeq
uate
to
re
pres
ent
quan
titie
s th
at
vary
co
ntin
uous
ly,
such
as
di
stan
ces,
he
ight
s,
etc.
Th
ese
quan
titie
s ar
e re
pres
ente
d by
nu
mbe
rs
cont
aini
ng
frac
tiona
l pa
rts
like
17.5
48.
Such
nu
mbe
rs a
re c
alle
d re
al o
r flo
atin
g po
int
cons
tant
s.
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
53
REA
L C
ON
STA
NT
(con
td..)
A re
al n
umbe
r may
als
o be
exp
ress
ed in
ex
pone
ntia
l not
atio
ns. F
or e
xam
ple,
the
valu
e 21
5.65
may
be
writ
ten
as 2
.156
5e2
in
expo
nent
ial
nota
tion.
E2
m
eans
m
ultip
ly b
y 10
2. T
he g
ener
al fo
rm is
:
Man
tissa
e e
xpon
ent
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
54
REA
L C
ON
STA
NT
(con
td..)
The
man
tissa
is
ei
ther
a
real
nu
mbe
r ex
pres
sed
in
deci
mal
no
tatio
n or
an
in
tege
r. Th
e ex
pone
nt is
an
inte
ger n
umbe
r w
ith a
n op
tiona
l pl
us o
r m
inus
sig
n. T
he
lette
r e
sepa
ratin
g th
e m
antis
sa a
nd t
he
expo
nent
ca
n be
w
ritte
n in
ei
ther
lo
wer
case
or u
pper
case
.
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
55
SIN
GLE
CH
AR
AC
TER
CO
NST
AN
TA
sin
gle
char
acte
r co
nsta
nt c
onta
ins
a si
ngle
cha
ract
er e
nclo
sed
with
in a
pai
r of
sin
gle
quot
e m
arks
. Ex
ampl
e is
‘5’
, an
d ‘x
’, ‘ ’
Not
e th
at t
he c
hara
cter
con
stan
t ‘5
’ is
no
t ha
ving
the
sam
e va
lue
as i
nteg
er
cons
tant
5.
Cha
ract
er c
onst
ants
hav
e in
tege
r val
ue k
now
n as
ASC
II va
lues
.
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
56
STR
ING
CO
NST
AN
TA
st
ring
cons
tant
is
a
sequ
ence
of
ch
arac
ters
enc
lose
d in
dou
ble
quot
e.
The
char
acte
rs
may
be
le
tters
, nu
mbe
rs,
spec
ial
char
acte
rs a
nd b
lank
sp
ace.
Exa
mpl
es a
re:
“hel
lo”,
“19
87”
etc.
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
57
STR
ING
CO
NST
AN
T (c
ontd
..)R
emem
ber
that
a
sing
le
char
acte
r co
nsta
nt is
not
equ
ival
ent
to t
he s
trin
g ch
arac
ter
cons
tant
. Fu
rthe
r a
sing
le
char
acte
r st
ring
cons
tant
do
es
not
havi
ng
any
ASC
II va
lue
as
sing
le
char
acte
r con
stan
t.
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
58
VAR
IAB
LEA
var
iabl
e is
a d
ata
nam
e th
at m
ay b
e us
ed
to
stor
e a
data
va
lue.
U
nlik
e co
nsta
nt,
a va
riabl
e m
ay t
ake
diffe
rent
va
lues
at
di
ffere
nt
times
du
ring
exec
utio
n.
Varia
ble
nam
e m
ay c
onsi
st o
f le
tters
, di
gits
, an
d th
e un
ders
core
cha
ract
er,
subj
ect t
o th
e fo
llow
ing
cond
ition
s:
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
59
VAR
IAB
LE (c
ontd
..)Th
ey m
ust b
egin
with
a le
tter.
Som
e sy
stem
s al
so
perm
it un
ders
core
as
a fir
st c
hara
cter
.A
NSI
st
anda
rd
reco
gniz
es
a le
ngth
of
31
ch
arac
ters
.U
pper
case
and
low
erca
se a
re s
igni
fican
t. Th
at is
, th
e va
riabl
e to
tal a
nd T
OTA
L is
not
the
sam
e.It
shou
ld n
ot b
e a
keyw
ord.
Whi
te s
pace
is n
ot a
llow
ed.
Not
e: o
nly
first
eig
ht c
hara
cter
s ar
e re
cogn
ized
by
a co
mpi
ler.
That
m
eans
, av
erag
e_he
ight
and
aver
age_
wei
ght
mea
n th
e sa
me
thin
g to
th
e co
mpu
ter.
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
60
DA
TA T
YPES
AN
SI C
sup
port
s th
ree
clas
ses
of d
ata
type
s as
;Pr
imar
y (o
r fu
ndam
enta
l) da
ta
type
s (in
t, ch
ar, f
loat
, dou
ble,
voi
d)D
eriv
ed d
ata
type
s (
arra
ys,
func
tions
. po
inte
rs, s
truc
ture
s)U
ser-
defin
ed d
ata
type
s
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
61
DA
TA T
YPES
(con
td..)
All
C++
co
mpi
lers
su
ppor
t fiv
e fu
ndam
enta
l dat
a ty
pes,
nam
ely
inte
ger
(int),
ch
arac
ter
(cha
r),
float
ing
poin
t (fl
oat),
dou
ble-
prec
isio
n flo
atin
g po
int
(dou
ble)
and
voi
d. M
any
of t
hem
als
o of
fer
exte
nded
dat
a ty
pes
such
as
long
in
tand
long
dou
ble.
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
62
INTE
GER
TYP
ESTh
ere
are
two
broa
der c
ateg
orie
s as
:
Sign
edU
nsig
ned
Int
unsi
gned
int
Shor
t int
unsi
gned
Sho
rt in
tLo
ng in
tun
sign
ed L
ong
int
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
63
CH
AR
AC
TER
TYP
ES
Ther
e ar
e th
ree
cate
gorie
s as
:
Cha
r Si
gned
cha
ract
ers
Uns
igne
d ch
arac
ters
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
64
FLO
ATI
NG
PO
INT
TYPE
Ther
e ar
e th
ree
cate
gorie
s as
:
Floa
tD
oubl
eLo
ng d
oubl
e
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
65
Tabl
e: S
ize
and
Ran
ge o
f Dat
a Ty
pes
on a
16-
bit M
achi
ne
3.4E
-493
2 to
1.1
E+49
3280
Long
dou
ble
1.7E
-308
to 1
.7E+
308
64D
oubl
e3.
4E –
38 to
3.4
E +
3832
Floa
t0
to 4
,294
,967
,295
32U
nsig
ned
long
int
-2,1
47,4
83,6
48 to
2,1
47,4
83,6
4732
Long
into
r sig
ned
long
int
0 to
255
8U
nsig
ned
shor
t int
-128
to 1
278
Shor
t int
or u
sins
igne
dsh
ort i
nt0
to 6
5535
16U
nsig
ned
int
-32,
768
to 3
2,76
716
Into
r sig
ned
int
0 to
255
8U
nsig
ned
char
-128
to 1
278
Cha
r or s
igne
d ch
arR
ange
Size
(bits
)Ty
pe
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
66
DEC
LAR
ATI
ON
OF
VAR
IAB
LEA
fter
desi
gnin
g su
itabl
e va
riabl
e na
mes
, we
mus
t de
clar
e th
em
to
com
pile
r. D
ecla
ratio
n do
es tw
o th
ings
:It
tells
the
com
pile
r w
hat
the
varia
ble
nam
e is
.It
spec
ifies
w
hat
type
of
da
ta
the
varia
ble
will
hol
d.Th
e de
clar
atio
n of
the
var
iabl
e m
ust
done
be
fore
they
are
use
d in
the
prog
ram
.
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
67
PRIM
AR
Y TY
PE D
ECLA
RA
TIO
NA
var
iabl
e ca
n be
use
d to
sto
re a
val
ue
of
any
data
ty
pe.
The
synt
ax
for
decl
arin
g a
varia
ble
is a
s fo
llow
s:D
ata-
type
v1,
v2,
…. v
n;v1
, v2
, …
.. vn
are
the
nam
es o
f th
e va
riabl
es.
Varia
bles
are
sep
arat
ed b
y co
mm
as.
A de
clar
atio
n st
atem
ent
mus
t en
d w
ith a
sem
icol
on.
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
68
Tabl
e: D
ata
Type
and
thei
r K
eyw
ords Lo
ng d
oubl
eEx
tend
ed d
oubl
e pr
ecis
ion
Floa
ting
poin
tD
oubl
eD
oubl
e pr
ecis
ion
Floa
ting
poin
tFl
oat
Floa
ting
poin
tU
nsig
ned
long
int
Uns
igne
d lo
ng in
tege
rU
nsig
ned
shor
t int
Uns
igne
d sh
ort i
nteg
erU
nsig
ned
int
Uns
igne
d in
tege
rSi
gned
long
int
Sign
ed lo
ng in
tege
rSi
gned
sho
rt in
tSi
gned
sho
rt in
tege
rSi
gned
int
Sign
ed in
tege
rSi
gned
cha
rSi
gned
cha
ract
erU
nsig
ned
char
Uns
igne
d ch
arac
ter
Cha
rC
hara
cter
Key
wor
d eq
uiva
lent
Dat
a Ty
pe
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
69
Use
r-de
fined
Typ
e D
ecla
ratio
n C
++ s
uppo
rts
a fe
atur
e kn
own
as “
type
de
finiti
on”
that
allo
ws
user
s to
def
ine
an i
dent
ifier
tha
t w
ould
rep
rese
nt a
nd
exis
ting
data
ty
pe.
The
user
de
fined
da
ta t
ype
iden
tifie
r ca
n la
ter
be u
sed
to
decl
are
varia
bles
. It
take
s th
e ge
nera
l fo
rm: Ty
pede
ftyp
e id
entif
ier;
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
70
Use
r-de
fined
Typ
e D
ecla
ratio
n (c
ontd
..)W
here
type
refe
rs to
an
exis
ting
data
type
and
id
entif
ier
refe
rs t
o th
e ne
w n
ame
give
n to
the
da
ta t
ype.
The
exi
stin
g da
ta t
ype
may
bel
ong
to
any
clas
s of
ty
pe,
incl
udin
g th
e us
er-
defin
ed o
nes.
Rem
embe
r th
at t
he n
ew t
ype
is
new
onl
y in
nam
e, b
ut n
ot t
he d
ata
type
. ty
pede
fcan
not c
reat
e a
new
type
. Ex
ampl
es:
Type
defi
ntun
its;
Type
deff
loat
mar
ks;
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
71
Use
r-de
fined
Typ
e D
ecla
ratio
n (c
ontd
..)H
ere,
un
its s
ymbo
lize
int
and
mar
ks
sym
boliz
es fl
oat.
They
can
be
late
r use
d to
dec
lare
as
follo
ws:
Uni
ts b
atch
1;M
arks
nam
e1[5
0];
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
72
Use
r-de
fined
Typ
e D
ecla
ratio
n (c
ontd
..)A
noth
er u
ser-
defin
ed d
ata
type
is e
num
erat
ed
data
typ
e pr
ovid
ed b
y A
NSI
sta
ndar
d. I
t is
de
fined
as
follo
ws:
Enum
iden
tifie
r {va
lue1
, val
ue2,
……
, val
uen}
;Th
e id
entif
ier
is
a us
er
defin
ed
data
en
umer
ated
dat
a ty
pe w
hich
can
be
used
to
decl
are
varia
bles
tha
t ca
n ha
ve o
ne o
f th
e va
lues
enc
lose
d w
ithin
the
bra
ces.
Afte
r th
is
defin
ition
, we
can
decl
are
varia
bles
to
be th
is
new
type
as
belo
w:
Enum
iden
tifie
r V1,
v2,
…..
vn;
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
73
Use
r-de
fined
Typ
e D
ecla
ratio
n (c
ontd
..)Th
e en
umer
ated
var
iabl
es v
1, v
2, …
, vn
can
only
hav
e on
e of
the
valu
es v
alue
1,
valu
e2, …
… v
alue
n. T
he a
ssig
nmen
t of
th
e fo
llow
ing
type
s ar
e va
lid:
V1 =
val
ue3;
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
74
DEC
LAR
ATI
ON
OF
STO
RA
GE
CLA
SSES
Varia
bles
in C
++ c
an h
ave
not o
nly
data
ty
pe
but
also
st
orag
e cl
ass
that
pr
ovid
es
info
rmat
ion
abou
t th
eir
loca
tion
and
visi
bilit
y.
The
stor
age
clas
s de
cide
s th
e po
rtio
n of
th
e pr
ogra
m w
ithin
whi
ch t
he v
aria
bles
are
re
cogn
ized
.
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
75
DEC
LAR
ATI
ON
OF
STO
RA
GE
CLA
SSES
(con
td..)
The
stor
age
varia
ble
is a
noth
er q
ualif
ier
that
ca
n be
add
ed t
o a
varia
ble
decl
arat
ion
as
show
n be
low
:A
uto
intc
ount
;R
egis
ter c
har c
h;St
atic
intx
;Ex
tern
long
tota
l;St
atic
an
d ex
tern
al
(ext
ern)
va
riabl
es
are
auto
mat
ical
ly
initi
aliz
ed
to
zero
. A
utom
atic
(a
uto)
va
riabl
es
cont
ain
unde
fined
va
lues
(k
now
n as
gar
bage
) unl
ess
they
are
initi
aliz
ed
expl
icitl
y.
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
76
DEC
LAR
ATI
ON
OF
STO
RA
GE
CLA
SSES
(con
td..)
Loca
l var
iabl
e w
hich
is s
tore
d in
the
regi
ster
.R
egis
ter
Glo
bal
varia
ble
know
n to
all
func
tions
in
the
file.
Ext
ern
Loca
l va
riabl
e w
hich
exi
sts
and
reta
ins
its
valu
e ev
en a
fter t
he c
ontro
l is
trans
ferre
d to
th
e ca
lling
func
tion.
Sta
tic
Loca
l va
riabl
e kn
own
only
to
the
func
tion
in
whi
ch it
is d
ecla
red.
Def
ault
is a
uto.
Aut
o
Mea
ning
Stor
age
clas
s
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
77
ASS
IGN
ING
TH
E VA
LUE
TO
VAR
IAB
LEVa
lues
can
be
assi
gned
to
varia
bles
us
ing
the
assi
gnm
ent
oper
ator
=
as
follo
ws:
Varia
ble_
nam
e=
cons
tant
;C
per
mits
mul
tiple
ass
ignm
ents
in
one
line.
For
exa
mpl
eIn
itial
_val
ue=
0;fin
al_v
alue
= 10
0;ar
e va
lid s
tate
men
t.
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
78
ASS
IGN
ING
TH
E VA
LUE
TO
VAR
IAB
LE (c
ontd
..)A
n as
sign
men
t st
atem
ent
impl
ies
that
th
e va
lue
of t
he v
aria
ble
on t
he l
eft
of
the
‘equ
al s
ign’
is s
et e
qual
to th
e va
lue
of t
he q
uant
ity (
or t
he e
xpre
ssio
n) o
n th
e rig
ht.
Dur
ing
assi
gnm
ent
oper
atio
n,
C++
co
nver
ts t
he t
ype
of v
alue
on
the
right
ha
nd s
ide
to t
he t
ype
on t
he l
eft.
This
m
ay in
volv
e tr
unca
tion
whe
n re
al v
alue
is
con
vert
ed in
to in
tege
r.
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
79
ASS
IGN
ING
TH
E VA
LUE
TO
VAR
IAB
LE (c
ontd
..)It
is a
lso
poss
ible
to a
ssig
n a
valu
e to
a
varia
ble
at
the
time
the
varia
ble
is
decl
ared
. Thi
s ta
kes
the
follo
win
g fo
rm:
Dat
a_ty
peva
riabl
e_na
me
= co
nsta
nt;
Som
e ex
ampl
es a
re:
Intf
inal
_val
ue=
100;
Cha
r yes
= ‘x
’;D
oubl
e ba
lanc
e =
100.
90;
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
80
ASS
IGN
ING
TH
E VA
LUE
TO
VAR
IAB
LE (c
ontd
..)Th
e pr
oces
s of
giv
ing
initi
al v
alue
s to
va
riabl
e is
ca
lled
initi
aliz
atio
n.
C++
pe
rmits
the
ini
tializ
atio
n of
mor
e th
an
one
varia
ble
in
one
stat
emen
t us
ing
mul
tiple
as
sign
men
t op
erat
ors.
So
me
exam
ples
are
as
follo
ws:
P =
q =
r = 0
;X
= y
= z=
MAX
;
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
81
DEF
ININ
G S
YMB
OLI
C
CO
NST
AN
TSEx
ampl
e:
#def
ine
STR
ENG
TH 1
00#d
efin
e PA
SS M
AR
K 5
0#d
efin
e M
AX 2
00#d
efin
e PI
3.4
159
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
82
DEF
ININ
G S
YMB
OLI
C
CO
NST
AN
TS (c
ontd
..)Th
e fo
llow
ing
rule
s ap
ply
to #
defin
e st
atem
ent
whi
ch d
efin
es a
sym
bolic
con
stan
t:Sy
mbo
lic
nam
es
have
th
e sa
me
form
as
va
riabl
e na
mes
. N
o bl
ank
spac
e be
twee
n th
e po
und
sign
# a
nd
the
wor
d de
fine
is p
erm
itted
.#
mus
t be
the
first
cha
ract
er in
the
line.
A b
lank
spa
ce i
s re
quire
d be
twee
n #d
efin
e an
d sy
mbo
lic n
ame
and
betw
een
the
sym
bolic
na
me
and
the
cons
tant
.#d
efin
e st
atem
ent
mus
t no
t en
d w
ith
a se
mic
olon
.
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
83
DEF
ININ
G S
YMB
OLI
C
CO
NST
AN
TS (c
ontd
..)A
fter
defin
ition
, th
e sy
mbo
lic n
ame
shou
ld
not b
e as
sign
ed a
ny v
alue
with
in th
e pr
ogra
m
by u
sing
an
assi
gnm
ent o
pera
tor.
Sym
bolic
nam
es a
re n
ot d
ecla
red
for
data
ty
pes.
Its
data
type
dep
ends
on
the
type
of t
he
cons
tant
.#d
efin
e st
atem
ent
may
app
ear
anyw
here
in
the
prog
ram
but
bef
ore
it is
ref
eren
ced
in t
he
prog
ram
.
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
84
DEC
LAR
ING
A V
AR
IAB
LE A
S C
ON
STA
NT
We
may
lik
e th
e va
lue
of
cert
ain
varia
bles
to r
emai
n co
nsta
nt d
urin
g th
e ex
ecut
ion
of
the
prog
ram
. W
e ca
n ac
hiev
e th
is b
y de
clar
ing
the
varia
ble
with
the
qua
lifie
r co
nst
at t
he t
ime
of
initi
aliz
atio
n.
Exam
ple:
Con
st in
tcla
ss_s
ize
= 40
;
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
85
DEC
LAR
ING
A V
AR
IAB
LE A
S C
ON
STA
NT
(con
td..)
Con
st
is
a ne
w
data
ty
pe
qual
ifier
de
fined
in A
NSI
. Thi
s te
lls t
he c
ompi
ler
that
the
val
ue o
f in
tva
riabl
e cl
ass_
size
mus
t no
t be
mod
ified
by
the
prog
ram
. H
owev
er i
t ca
n be
use
d on
the
rig
ht
hand
sid
e of
an
assi
gnm
ent
stat
emen
t lik
e an
y ot
her v
aria
ble.
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
86
DEC
LAR
ING
A V
AR
IAB
LE A
S VO
LATI
LEA
NSI
sta
ndar
d de
fines
ano
ther
qua
lifie
r vo
latil
e th
at
coul
d be
us
ed
to
tell
expl
icitl
y th
e co
mpi
ler
that
a v
aria
ble’
s va
lue
may
be
chan
ged
at a
ny t
ime
by
som
e ex
tern
al s
ourc
e (fr
om o
utsi
de t
he
prog
ram
). Fo
r exa
mpl
e:Vo
latil
e in
tdat
e;
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
87
DEC
LAR
ING
A V
AR
IAB
LE A
S VO
LATI
LE (c
ontd
..)Th
e va
lue
of d
ate
may
be
alte
red
by
som
e ex
tern
al fa
ctor
s ev
en if
it d
oes
not
appe
ar
on
the
left
hand
si
de
of
an
assi
gnm
ent
stat
emen
t. W
hen
we
decl
are
an
varia
ble
as
vola
tile,
th
e co
mpi
ler
will
exa
min
e th
e va
lue
of t
he
varia
ble
each
tim
e it
is e
ncou
nter
ed t
o se
e w
heth
er a
ny e
xter
nal a
ltera
tion
has
chan
ged
the
valu
e.
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
88
OVE
RFL
OW
AN
D U
ND
ERFO
W
OF
DA
TAPr
oble
m o
f da
ta o
verf
low
occ
urs
whe
n th
e va
lue
of a
var
iabl
e is
eith
er t
oo b
ig
or t
oo s
mal
l fo
r th
e da
ta t
ype
to h
old.
Th
e la
rges
t va
lue
that
a v
aria
ble
can
hold
al
so
depe
nds
on
the
mac
hine
. Si
nce
float
ing
poin
t va
lues
are
rou
nded
of
f to
the
num
ber
of s
igni
fican
t di
gits
al
low
ed, a
n ov
erflo
w n
orm
ally
res
ults
in
the
larg
est p
ossi
ble
real
val
ue, w
here
as
an u
nder
flow
resu
lts in
zer
o.
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
89
OPE
RA
TOR
S &
EXP
RES
SIO
NS
An
oper
ator
is
a sy
mbo
l th
at t
ells
the
co
mpu
ter
to
perf
orm
ce
rtai
n m
athe
mat
ical
or
logi
cal
man
ipul
atio
ns.
Ope
rato
rs
are
used
in
pr
ogra
ms
to
man
ipul
ate
the
data
and
var
iabl
es. T
hey
usua
lly f
orm
a p
art
of t
he m
athe
mat
ical
or
logi
cal e
xpre
ssio
ns.
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
90
OPE
RA
TOR
S &
EXP
RES
SIO
NS
(con
td..)
C o
pera
tors
can
be
clas
sifie
d in
to a
num
ber o
f ca
tego
ries.
The
y in
clud
e:i.
Arit
hmet
ic o
pera
tors
ii.R
elat
iona
l ope
rato
rsiii
.Lo
gica
l ope
rato
rsiv
.A
ssig
nmen
t ope
rato
rsv.
Incr
emen
t and
dec
rem
ent o
pera
tors
vi.
Con
ditio
nal o
pera
tors
vii.
Bitw
ise
oper
ator
svi
ii.Sp
ecia
l ope
rato
rs
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
91
TAB
LE: A
RIT
HM
ETIC
O
PER
ATO
RS
Mod
ulo
divi
sion
s%
Div
isio
n/
Mul
tiplic
atio
n*
Sub
tract
ion
or
unar
y m
inus
-
Add
ition
or u
nary
plu
s+
Mea
ning
Ope
rato
rs
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
92
TAB
LE: R
ELA
TIO
NA
L O
PER
ATO
RS
Is n
ot e
qual
to!=
Is e
qual
to==
Is g
reat
er t
han
or e
qual
to
>=
Is g
reat
er th
an>
Is le
ss th
an o
r equ
al to
<=
Is le
ss th
an<
Mea
ning
Ope
rato
rs
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
93
TAB
LE: L
OG
ICA
L O
PER
ATO
R
Logi
cal N
OT
!
Logi
cal O
R||
Logi
cal A
ND
&&
Mea
ning
Ope
rato
rs
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
94
TAB
LE: S
HO
RTH
AN
D
ASS
IGN
MEN
T O
PER
ATO
R
A%
=bA
=a%
b
a/=n
+1A
=a/(n
+1)
A*=n
+1A
=a*(
n+1)
a-=1
A=a
-1
A+=1
A=a+
1
Mea
ning
Ope
rato
rs
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
95
INC
REM
ENT
& D
ECR
EMEN
T O
PER
ATO
RS
Rul
es fo
r Inc
rem
ent a
nd d
ecre
men
t ope
rato
rsIn
crem
ent
and
decr
emen
t op
erat
ors
are
unar
y op
erat
ors
and
they
re
quire
va
riabl
es
as
thei
r op
eran
ds.
Whe
n po
stfix
++
(or-
-) is
use
d w
ith a
var
iabl
e in
an
expr
essi
on, t
he e
xpre
ssio
ns is
eva
luat
ed fi
rst u
sing
th
e or
igin
al
valu
e of
th
e va
riabl
e an
d th
en
the
varia
ble
is in
crem
ente
d (o
r dec
rem
ente
d) b
y on
e.W
hen
pref
ix +
+ (o
r--)
is u
sed
in a
n ex
pres
sion
, th
e va
riabl
e is
inc
rem
ente
d (o
r de
crem
ente
d) f
irst
and
then
the
exp
ress
ion
is e
valu
ated
usi
ng t
he n
ew
valu
e of
the
varia
ble.
The
prec
eden
ce
and
asso
ciat
ivel
y of
++
an
d --
oper
ator
s ar
e th
e sa
me
as t
hose
of
unar
y +
and
unar
y -.
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
96
CO
ND
ITIO
NA
L O
PER
ATO
RS
A t
erna
ry o
pera
tor
pair
“? :
” is
ava
ilabl
e in
C
to c
onst
ruct
con
ditio
nal
expr
essi
ons
of t
he
form Ex
p1 ?
exp
2 :
exp3
whe
re e
xp1,
exp
2, a
nd
exp3
are
exp
ress
ions
.Th
e op
erat
or ?
: w
orks
as
follo
ws:
Exp1
is
eval
uate
d fir
st. I
f it
is n
onze
ro (
true
), th
en t
he e
xpre
ssio
n ex
p2 i
s ev
alua
ted
and
beco
mes
the
val
ue o
f th
e ex
pres
sion
. If
exp1
is
fal
se,
then
exp
3 is
eva
luat
ed b
ecom
es t
he
valu
e of
the
expr
essi
on.
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
97
TAB
LE: B
ITW
ISE
OPE
RA
TOR
S
Shi
ft rig
ht>>
Shi
ft le
ft<<
Bitw
ise
excl
usiv
e O
R^
Bitw
ise
OR
|
Bitw
ise
AN
D&
Mea
ning
Ope
rato
rs
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
98
Dyn
amic
Initi
aliz
atio
n of
Va
riabl
esIn
C,
a va
riabl
e m
ust
be i
nitia
lized
usi
ng a
co
nsta
nt e
xpre
ssio
n, a
nd t
he c
com
pile
r w
ould
fix
the
ini
tializ
atio
n co
de a
t th
e tim
e of
co
mpi
latio
n.
C++
, ho
wev
er,
perm
its i
nitia
lizat
ion
of t
he v
aria
ble
at
run
time.
Thi
s is
ref
erre
d to
as
dyna
mic
in
itial
izat
ion.
In
C++
, a
varia
ble
can
be
initi
aliz
ed a
t ru
n tim
e us
ing
expr
essi
on
at th
e pl
ace
of d
ecla
ratio
n.
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
99
Dyn
amic
Initi
aliz
atio
n of
Va
riabl
es (c
ontd
..)Fo
r ex
ampl
e,
the
follo
win
g ar
e va
lid
initi
aliz
atio
n st
atem
ents
:
Intn
= s
trle
n(s
trin
g);
Floa
t are
a =
3.14
* r *
r;
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
100
Ref
eren
ce V
aria
bles
C++
in
trodu
ces
a ne
w
kind
of
va
riabl
e kn
own
as t
he r
efer
ence
var
iabl
e. A
re
fere
nce
varia
ble
prov
ides
an
al
ias
(alte
rnat
ive
nam
e)
for
the
prev
ious
ly
defin
ed v
aria
ble.
Fo
r exa
mpl
e, if
we
mak
e th
e va
riabl
e su
ma
refe
renc
e to
th
e va
riabl
e to
tal,
then
su
man
d to
tal
can
be
used
in
terc
hang
eabl
y to
re
pres
ent
that
va
riabl
e
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
101
Ref
eren
ce V
aria
bles
(con
td..)
A re
fere
nce
varia
ble
is c
reat
ed a
s fo
llow
s:
data
-type
& re
fere
nce-
nam
e =
varia
ble-
nam
e;
Exam
ple:
float
tota
l = 1
00;
float
& s
um =
tota
l;
Tota
l is
a f
loat
typ
e va
riabl
e th
at h
as a
lread
y be
en
decl
ared
; su
m
is
the
alte
rnat
ive
decl
ared
to r
epre
sent
the
var
iabl
e to
tal.
Bot
h th
e va
riabl
e re
fer
to t
he s
ame
data
obj
ect
in
the
mem
ory.
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
102
Ref
eren
ce V
aria
bles
(con
td..)
Now
, the
sta
tem
ents
cout
<< to
tal;
and
cout
<< s
um; b
oth
prin
t the
val
ue 1
00.
The
stat
emen
t to
tal =
tota
l+ 1
0;w
ill c
hang
e th
e va
lue
of b
oth
tota
l and
sum
to
110.
like
wis
e, th
e as
sign
men
t su
m =
0;
will
cha
nge
the
valu
e of
bot
h th
e va
riabl
es t
o ze
ro.
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
103
OPE
RA
TPR
S IN
C++
C++
has
a ri
ch s
et o
f ope
rato
rs. A
ll C
ope
rato
rs a
re v
alid
in
C++
. In
add
ition
, in
trodu
ces
som
e ne
w o
pera
tors
. Th
e ne
w o
pera
tors
are
:<<
the
inse
rtion
ope
rato
r>>
the
extra
ctio
n op
erat
or::
scop
e re
solu
tion
oper
ator
::*po
inte
r to
mem
ber d
ecla
ratio
n->
*po
inte
r to
mem
ber o
pera
tor
.*po
inte
r to
mem
ber o
pera
tor
dele
tem
emor
y re
leas
e op
erat
oren
dllin
e fe
ed o
pera
tor
new
mem
ory
allo
catio
n op
erat
orse
twfie
ld w
idth
ope
rato
r
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
104
Scop
e R
esol
utio
n O
pera
tor
The
sam
e va
riabl
e na
me
can
be u
sed
to
have
di
ffere
nt
mea
ning
in
di
ffere
nt b
lock
s. T
he s
cope
of
the
varia
ble
exte
nds
from
the
poi
nt o
f its
dec
lara
tion
till
the
end
of t
he
bloc
k co
ntai
ning
the
dec
lara
tion.
A
varia
ble
decl
ared
ins
ide
a bl
ock
is
said
to b
e lo
cal t
o th
at b
lock
.
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
105
Scop
e R
esol
utio
n O
pera
tor (
cont
d..)
The
scop
e re
solu
tion
oper
ator
(::)
can
be
used
to u
ncov
er th
e hi
dden
var
iabl
e. T
he
synt
ax is
as
follo
ws:
:: va
riabl
e_na
me;
This
ope
rato
r al
low
s ac
cess
to
the
glob
al
vers
ion
of v
aria
ble.
N
ote:
It
is t
o be
not
ed t
hat
::m w
ill a
lway
s re
fer t
o th
e gl
obal
var
iabl
e
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
106
#inc
lude
<io
stra
em>
Intm
=10;
Void
mai
n()
{In
tm=2
0;{
intk
=m;
Intm
=30;
Cou
t<<
“we
are
in in
ner b
lock
\n”;
Cou
t<<
“k=
” <
< k
<< “
\n”;
Cou
t<<
“m=
” <
< m
<<
“\n”
;C
out<
< “:
:m=
” <
< ::m
<<
“\n”
;} C
out<
< “w
e ar
e in
out
er b
lock
\n”;
Cou
t<<
“m=
” <
< m
<<
“\n”
;C
out<
< “:
:m=
” <
< ::m
<<
“\n”
;}Sc
ope
Res
olut
ion
Ope
rato
r (co
ntd.
.)
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
107
New
and
Del
ete
Ope
rato
r
An
obje
ct c
an b
e cr
eate
d by
usi
ng n
ew,
and
dest
roye
d by
usi
ng d
elet
e,as
and
w
hen
requ
ired.
A
dat
a ob
ject
cre
ated
in
side
a b
lock
with
new
, w
ill r
emai
n in
ex
iste
nce
until
it
is e
xplic
itly
dest
roye
d by
usi
ng d
elet
e. T
hus
the
lifet
ime
of a
n ob
ject
is
dire
ctly
und
er o
ur c
ontr
ol a
nd
is u
nrel
ated
to th
e bl
ock
stru
ctur
e of
the
prog
ram
.
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
108
New
and
Del
ete
Ope
rato
r (co
ntd.
.)Th
e ne
w o
pera
tor
can
be u
sed
to c
reat
e ob
ject
s of
any
type
. The
syn
tax
is:
poin
ter_
varia
ble
= ne
w d
ata_
type
;
Her
e po
inte
r_va
riabl
eis
a p
oint
er o
f ty
pe
data
_typ
e.
the
new
op
erat
or
allo
cate
s su
ffici
ent m
emor
y to
hol
d a
data
obj
ect o
f ty
pe d
ata_
type
and
retu
rns
the
addr
ess
of
the
obje
ct.
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
109
The
poin
ter
varia
ble
hold
s th
e ad
dres
s of
th
e m
emor
y sp
ace
allo
cate
d. F
or e
.g.
p=ne
w in
t; q=
new
floa
t;w
here
p is
a p
oint
er o
f typ
e in
tege
r an
d q
is a
poi
nter
of
type
flo
at.
Her
e p
and
q m
ust
have
al
read
y be
en
decl
ared
as
po
inte
r of a
ppro
pria
te ty
pes.
For
e.g
.in
t*p
= ne
w in
t;in
t*q
= ne
w fl
oat;
New
and
Del
ete
Ope
rato
r (co
ntd.
.)
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
110
We
can
also
ini
tializ
e th
e m
emor
y us
ing
the
new
op
erat
or. T
he s
ynta
x is
:po
inte
r_va
riabl
e=
new
dat
a_ty
pe(v
alue
);in
t*p
= ne
w in
t(25)
; flo
at *
q =
new
(10.
5);
Her
e va
lue
spec
ifies
the
initi
al v
alue
.N
ew c
an b
e us
ed t
o cr
eate
a m
emor
y sp
ace
of a
ny
data
typ
e in
clud
ing
user
def
ined
dat
a ty
pe a
s ar
ray.
The
syn
tax
is:
poin
ter_
varia
ble
= ne
w d
ata_
type
[siz
e];
Her
e si
ze s
peci
fies
the
num
ber
of e
lem
ents
in
the
arra
y.
New
and
Del
ete
Ope
rato
r (co
ntd.
.)
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
111
New
and
Del
ete
Ope
rato
r (co
ntd.
.)W
hen
a da
ta
obje
ct
is
no
long
er
need
ed,
it is
de
stro
yed
usin
g de
lete
. The
syn
tax
is:
dele
te p
oint
er_v
aria
ble;
dele
te p
; del
ete
q;
If w
e w
ant t
o fr
ee a
dyn
amic
ally
allo
cate
d ar
ray
then
th
e sy
ntax
is:
dele
te [s
ize]
poi
nter
_var
iabl
e;ol
der v
ersi
onde
lete
[ ] p
oint
er_v
aria
ble;
new
er v
ersi
on
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
112
Adva
ntag
e of
new
ove
r mal
loc(
)1.
It au
tom
atic
ally
com
pute
s th
e si
ze o
f th
e da
ta
obje
ct.
We
need
no
t us
e op
erat
or
size
of.
2.It
auto
mat
ical
ly r
etur
n th
e co
rrect
poi
nter
ty
pe, s
o th
ere
is n
o ne
ed to
use
a ty
pe c
ast.
3.It
is p
ossi
ble
to i
nitia
lize
the
obje
ct w
hile
cr
eatin
g th
e m
emor
y sp
ace.
4.N
ew
and
dele
te
oper
ator
ca
n be
ov
erlo
aded
.
New
and
Del
ete
Ope
rato
r (co
ntd.
.)
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
113
MA
NIP
ULA
TOR
S
Man
ipul
ator
s ar
e op
erat
ors
that
ar
e us
ed t
o fo
rmat
the
dat
a di
spla
y. T
he
mos
t co
mm
only
use
d ar
e en
dlan
d se
tw.
The
endl
man
ipul
ator
, whe
n us
ed in
an
outp
ut,
caus
es
a lin
efee
d to
be
in
sert
ed.
It ha
s th
e sa
me
effe
ct a
s us
ing
the
new
line
char
acte
rs \n
.
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
114
MA
NIP
ULA
TOR
S (c
ontd
..)
The
setw
man
ipul
ator
rig
ht
just
ified
th
e nu
mbe
r. Th
e sy
ntax
is:
cout
<<se
tw(w
idth
)<<v
aria
ble_
nam
e<<
endl
;
The
man
ipul
ator
se
twsp
ecifi
es
the
field
w
idth
for p
rintin
g th
e va
lue
of th
e va
riabl
e.
The
valu
e is
als
o rig
ht ju
stifi
ed w
ithin
the
fie
ld.
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
115
#inc
lude
<io
stre
am>
#inc
lude
<io
man
ip>
Void
mai
n()
{in
tbas
ic=9
50, a
llow
ance
=95,
tota
l = 1
045;
cout
<<se
tw(1
0)<<
“bas
ic”<
<set
w(1
0)<<
basi
c<<e
ndl
<<se
tw(1
0)<<
“allo
wan
ce”<
<set
w(1
0)<<
allo
wan
ce<<
endl
<<se
tw(1
0)<<
“tot
al”<
<set
w(1
0)<<
to
tal<
<end
l;} N
ote:
ch
arac
ter
strin
gs
are
also
pr
inte
d rig
ht
just
ified
.
MAN
IPU
LATO
RS
(con
td..)
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
116
Spe
cial
Ass
ignm
ent E
xpre
ssio
nC
hain
ed A
ssig
nmen
t:
x =
(y=1
0); o
r x =
y =
10;
A
chai
ned
stat
emen
t ca
nnot
be
us
ed
to
initi
aliz
e a
varia
ble
at
the
time
of
decl
arat
ion.
For
e.g
.
float
a =
b =
12.
24; /
/wro
ngflo
at a
= 1
2.24
, b=1
2.24
; //ri
ght
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
117
Embe
dded
Ass
ignm
ent:
x =
(y =
50)
+ 1
0; is
iden
tical
toy
= 50
; x =
y +
10;
Com
poun
d A
ssig
nmen
t:
x+=1
0; is
iden
tical
to x
= x
+ 1
0;
Spe
cial
Ass
ignm
ent E
xpre
ssio
n
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
118
Impl
icit
Con
vers
ion
We
can
mix
dat
a ty
pe in
exp
ress
ion.
For
e.g
.m
= 5
+ 5
.5;i
s a
valid
sta
tem
ent.
Whe
re
ever
dat
a ty
pes
are
mix
ed,
C++
per
form
s th
e co
nver
sion
au
tom
atic
ally
. Th
is
proc
ess
is k
now
n as
impl
icit
or a
utom
atic
ty
pe c
onve
rsio
n.Fo
r e.
g. if
one
of
the
oper
and
is in
tan
d th
e ot
her
is f
loat
, th
en i
ntis
con
vert
ed i
nto
float
bec
ause
floa
t is
wid
er th
an in
t.
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
119
FUN
CTI
ON
S IN
C++
Func
tion
Prot
otyp
ing:
It is
a d
ecla
ratio
n st
atem
ent
in t
he c
allin
g pr
ogra
m a
nd is
of t
he fo
llow
ing
synt
ax:
type
func
tion_
nam
e(a
rgum
ent_
list);
e.g
.flo
at v
olum
e (in
tx, f
loat
y, f
loat
z);
Not
e th
at e
ach
argu
men
t m
ust
be d
ecla
red
inde
pend
ently
. For
e.g
.flo
at v
olum
e (in
tx, f
loat
y, z
) is
illeg
al.
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
120
In a
fun
ctio
n de
clar
atio
n, t
he n
ames
of
the
argu
men
ts
are
dum
my
varia
ble
and
ther
efor
e th
ey a
re o
ptio
nal.
For e
.g.
float
vol
ume
(int,
float
, flo
at) i
s le
gal.
Exam
ple
prog
ram
:flo
at v
olum
e (in
ta, f
loat
b, f
loat
c)
{flo
at q
= a
*b*c
;}FU
NC
TIO
NS
IN C
++ (c
ontd
..)
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
121
The
func
tion
volu
me(
) can
be
invo
ked
as
float
cub
e1 =
vol
ume(
b1, w
1, h
1);
The
varia
ble
b1, w
1, a
nd h
1 ar
e kn
own
as t
he
actu
al
para
met
er
whi
ch
spec
ify
the
dim
ensi
on
of
cube
1.
Thei
r ty
pe
shou
ld
mat
ch w
ith th
e ty
pe d
ecla
red
in p
roto
type
.
FUN
CTI
ON
S IN
C++
(con
td..)
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
122
Cal
l be
refe
renc
e:Pr
ovis
ion
of t
he r
efer
ence
var
iabl
e in
C
++ p
erm
its u
s to
pas
s pa
ram
eter
s to
the
fun
ctio
ns b
y re
fere
nce.
Whe
n w
e pa
ss a
rgum
ents
by
refe
renc
e, th
e “f
orm
al”
argu
men
ts
in
the
calle
d fu
nctio
n be
com
e al
iase
s to
th
e “a
ctua
l”
argu
men
ts
in
the
calli
ng
func
tion.
FUN
CTI
ON
S IN
C++
(con
td..)
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
123
This
mea
ns th
at w
hen
the
func
tion
is w
orki
ng w
ith
its o
wn
argu
men
ts, i
t is
act
ually
wor
king
on
the
orig
inal
dat
a. F
or e
.g.
void
sw
ap (i
nt&
a, in
t&b)
{in
tt=a
; //
dyna
mic
initi
aliz
atio
n a=
b; b
=t;
}N
ow if
m a
nd n
are
tw
o in
tege
r va
riabl
e, t
hen
the
func
tion
call
swap
(m, n
) will
exc
hang
e th
e va
lue
of
m
and
n us
ing
thei
r al
iase
s (r
efer
ence
va
riabl
e) a
and
b.
FUN
CTI
ON
S IN
C++
(con
td..)
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
124
In t
radi
tiona
l C, t
his
is a
ccom
plis
hed
usin
g po
inte
rs. F
or e
.g.
void
sw
ap (i
nt*a
, int
*b)
{in
tt; t
=*a;
*a=*
b; *b
=t;
}Th
e fu
nctio
n ca
n be
cal
led
as s
wap
(&x,
&y)
. Th
is a
ppro
ach
is a
lso
acce
ptab
le in
C++
.
FUN
CTI
ON
S IN
C++
(con
td..)
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
125
Ret
urn
by re
fere
nce:
A fu
nctio
n ca
n al
so re
turn
a re
fere
nce.
For e
.g.
int&
max
(int
&x,
int&
y){
if (x
>y) r
etur
n x;
els
e re
turn
y;
}FUN
CTI
ON
S IN
C++
(con
td..)
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
126
Sinc
e th
e re
turn
typ
e of
max
() is
int
&,
the
func
tion
retu
rn r
efer
ence
to
x an
d y
(and
no
t the
val
ue).
Then
the
func
tion
call
such
as
max
(a,
b)
will
yie
ld a
ref
eren
ce t
o ei
ther
a o
r b
depe
ndin
g on
the
ir va
lues
. Th
is m
eans
tha
t th
is f
unct
ion
appe
ar o
n th
e le
ft ha
nd
side
of
an
as
sign
men
t st
atem
ent.
max
(a, b
) = -1
is le
gal a
nd a
ssig
n -1
to a
if
it is
larg
er o
ther
wis
e -1
to b
.
FUN
CTI
ON
S IN
C++
(con
td..)
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
127
Inlin
e fu
nctio
n:O
ne o
f the
maj
or o
bjec
tive
of u
sing
func
tion
in a
pro
gram
is
to s
ave
som
e m
emor
y sp
ace,
whi
ch b
ecom
es a
ppre
ciab
le w
hen
a fu
nctio
n is
lik
ely
to b
e ca
lled
man
y tim
es.
How
ever
, ev
ery
time
a fu
nctio
n is
ca
lled,
it
take
s a
lot
of
extr
a tim
e in
ex
ecut
ing
a se
ries
of
inst
ruct
ions
fo
r ta
sks
such
as
FUN
CTI
ON
S IN
C++
(con
td..)
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
128
1.Ju
mpi
ng to
the
func
tion
2.Sa
ving
regi
ster
s3.
Push
ing
argu
men
ts to
the
stac
k4.
Ret
urni
ng to
the
calli
ng fu
nctio
ns e
tc
Whe
n a
func
tion
is
smal
l, a
subs
tant
ial
perc
enta
ge o
f exe
cutio
n tim
e m
ay b
e sp
ent
in s
uch
over
head
.
FUN
CTI
ON
S IN
C++
(con
td..)
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
129
To
elim
inat
e th
e co
st
of
calls
to
sm
all
func
tion,
C
++
prop
oses
a
new
fe
atur
e ca
lled
Inlin
e fu
nctio
n.A
n in
line
func
tion
is
a fu
nctio
n th
at
is
expa
nded
in li
ne w
hen
it is
invo
ked.
Tha
t is
, the
com
pile
r re
plac
es t
he f
unct
ion
call
with
th
e co
rres
pond
ing
func
tion
call.
Sy
ntax
is: in
line
func
tion_
head
er{
func
tion_
body
}
FUN
CTI
ON
S IN
C++
(con
td..)
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
130
For e
.g.
inlin
e do
uble
cub
e (d
oubl
e a)
{re
turn
(a*a
*a);
}fu
nctio
n ca
lling
:c
= cu
be (3
.0);
d =
cube
(1.5
_ 2
.5);
Usu
ally
the
fun
ctio
n ar
e m
ade
inlin
e w
hen
they
are
sm
all
enou
gh t
o be
def
ined
in
one
or tw
o lin
es.
FUN
CTI
ON
S IN
C++
(con
td..)
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
131
Not
e:in
line
expr
essi
ons
mak
e a
prog
ram
ru
ns
fast
er
beca
use
the
over
head
of
a
func
tion
call
and
retu
rn
is
elim
inat
ed.
How
ever
, it
mak
es th
e pr
ogra
m to
take
up
mor
e m
emor
y be
caus
e th
e st
atem
ent t
hat
defin
e th
e in
line
func
tion
are
repr
oduc
ed a
t ea
ch p
oint
whe
re t
he
func
tion
is c
alle
d.
FUN
CTI
ON
S IN
C++
(con
td..)
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
132
Som
e of
th
e si
tuat
ions
w
here
in
line
expa
nsio
ns m
ay n
ot w
ork
are:
1.Fo
r fu
nctio
ns r
etur
ning
val
ues,
if a
loop
, a
switc
h or
a g
oto
exis
ts.
2.Fo
r fu
nctio
n no
t re
turn
ing
valu
es,
if a
retu
rn s
tate
men
t exi
sts.
3.If
func
tions
con
tain
sta
tic v
aria
ble.
4.If
inlin
e fu
nctio
ns a
re re
curs
ive.
FUN
CTI
ON
S IN
C++
(con
td..)
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
133
Exam
ple:
inlin
e flo
at m
ul(fl
oat x
, flo
at y
){ r
etur
n (x
*y);
}in
line
doub
le d
iv (d
oubl
e p,
dou
ble
q){r
etur
n (p
/ q)
;}
void
mai
n ()
{flo
at a
= 1
2.34
5; fl
oat b
= 9
.82;
cout
<<m
ul(a
,b);
cout
<< d
iv(a
,b)}
FUN
CTI
ON
S IN
C++
(con
td..)
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
134
Def
ault
argu
men
ts:
C++
al
low
s us
to
ca
ll a
func
tion
with
out
spec
ifyin
g al
l its
arg
umen
ts.
In s
uch
case
s,
the
func
tion
assi
gn a
def
ault
valu
e to
the
pa
ram
eter
whi
ch d
oes
not
have
a m
atch
ing
argu
men
ts in
the
func
tion
call.
Def
ault
valu
es
are
spec
ified
whe
n th
e fu
nctio
n is
dec
lare
d.
The
com
pile
r lo
oks
at t
he p
roto
type
to
see
how
man
y ar
gum
ents
a f
unct
ion
uses
and
al
ert t
he p
rogr
am fo
r pos
sibl
e de
faul
t val
ues.
FUN
CTI
ON
S IN
C++
(con
td..)
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
135
For e
.g.
float
am
ount
(flo
at p
, int
t, flo
at r
=0.1
5);
callin
g va
lue
= am
ount
(500
0, 7
);va
lue
= am
ount
(500
0, 7
, 0.1
2)N
ote:
on
ly
the
trailin
g ar
gum
ents
ca
n ha
ve
defa
ult
valu
es a
nd t
here
fore
we
mus
t ad
d de
faul
ts fr
om ri
ght t
o le
ft.
FUN
CTI
ON
S IN
C++
(con
td..)
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
136
For e
.g.
intm
ul(in
ti, i
ntj=
5, in
tk=1
0) //
lega
lin
tmul
(inti
=5, i
ntj)
//ille
gal
intm
ul(in
ti=1
0, in
tj, i
ntk=
5) //
illeg
alin
tmul
(inti
=2, i
ntj=
5, in
tk=1
0) //
lega
l
FUN
CTI
ON
S IN
C++
(con
td..)
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
137
#inc
lude
<io
stre
am.h
>vo
id m
ain(
){
float
am
ount
;flo
at v
alue
(flo
at p
, int
n, fl
oat r
=0.1
5);
void
prin
tline
(cha
r ch=
‘*’, i
ntle
n=40
);pr
intli
ne();
amou
nt =
val
ue (5
000.
00, 5
);co
ut<<
“\n
Fin
al V
alue
=”<
<am
ount
;pr
intli
ne(‘=
’);}FU
NC
TIO
NS
IN C
++ (c
ontd
..)
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
138
float
val
ue (f
loat
p, i
ntn,
floa
t r)
{in
tyea
r=1;
float
sum
=p;
whi
le (y
ear <
=n)
{su
m=s
um*(
1+r)
;ye
ar=y
ear+
1;} re
turn
(sum
);}FU
NC
TIO
NS
IN C
++ (c
ontd
..)
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
139
void
prin
tline
(cha
r ch,
intl
en)
{fo
r (in
ti=1
; i<=
len;
i++)
cout
<<“\n
”;}FU
NC
TIO
NS
IN C
++ (c
ontd
..)
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
140
Func
tion
Ove
rload
ing
Ove
rload
ing
refe
rs t
o th
e us
e of
the
sam
e th
ing
for
diffe
rent
pur
pose
s. C
++ p
erm
its
over
load
ing
of t
he f
unct
ion.
Thi
s m
eans
th
at w
e ca
n us
e th
e sa
me
func
tion
nam
e to
cre
ate
func
tions
that
per
form
s a
varie
ty
of
diffe
rent
ta
sks.
Th
is
is
know
n as
fu
nctio
n po
lym
orph
ism
in O
OP.
FUN
CTI
ON
S IN
C++
(con
td..)
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
141
Usi
ng t
he c
once
pt o
f fu
nctio
n ov
erlo
adin
g,
we
can
desi
gn a
fam
ily o
f fu
nctio
ns w
ith
one
func
tion
nam
e bu
t w
ith
diffe
rent
ar
gum
ent
lists
. Th
e fu
nctio
n w
ould
pe
rfor
m
diffe
rent
op
erat
ions
de
pend
ing
on t
he a
rgum
ent
list
in t
he f
unct
ion
call.
Th
e co
rrec
t fu
nctio
n to
be
in
voke
d is
de
term
ined
by
chec
king
the
num
ber
and
type
of t
he a
rgum
ents
but
not
on
the
type
.
FUN
CTI
ON
S IN
C++
(con
td..)
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
142
For e
.g. (
Func
tion
Dec
lara
tion)
inta
dd (i
nta,
intb
);in
tadd
(int
a, in
tb, i
ntc)
;do
uble
add
(dou
ble
x, d
oubl
e y)
;do
uble
add
(int
p, d
oubl
e q)
;do
uble
(dou
ble
p, in
tq);
FUN
CTI
ON
S IN
C++
(con
td..)
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
143
For e
.g. (
Func
tion
call)
cout
<< a
dd (5
,10)
;//u
sing
pro
toty
pe 1
cout
<< a
dd(1
5,10
.0);
//usi
ng p
roto
type
4co
ut<<
add
(12.
5,7.
5); /
/usi
ng p
roto
type
3co
ut<<
add
(5,1
0,15
); //u
sing
pro
toty
pe 2
cout
<< a
dd (0
.75,
5);
//usi
ng p
roto
type
5
FUN
CTI
ON
S IN
C++
(con
td..)
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
144
A f
unct
ion
call
first
mat
ches
the
pro
toty
pe
havi
ng
the
sam
e nu
mbe
r an
d ty
pe
of
argu
men
ts a
nd t
hen
calls
the
app
ropr
iate
fu
nctio
n fo
r ex
ecut
ion.
A b
est m
atch
mus
t be
uni
que.
The
func
tion
sele
ctio
n in
volv
es
the
follo
win
g st
eps:
1. T
he c
ompi
ler
first
trie
s to
fin
d an
exa
ct
mat
ch
in
whi
ch
the
type
s of
ac
tual
ar
gum
ents
are
the
sam
e.
FUN
CTI
ON
S IN
C++
(con
td..)
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
145
2.
If an
ex
act
mat
ch
is
not
foun
d,
the
com
pile
r us
es t
he i
nteg
ral
prom
otio
ns
to t
he a
ctua
l ar
gum
ents
, su
ch a
s ch
ar
to i
ntan
d flo
at t
o do
uble
to f
ind
a m
atch
.3.
Whe
n ei
ther
of
them
fai
ls,
then
com
pile
r us
es
the
impl
icit
type
co
nver
sion
s m
echa
nism
.
FUN
CTI
ON
S IN
C++
(con
td..)
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
146
Supp
ose
we
use
the
follo
win
g tw
o fu
nctio
ns:
long
squ
are
(long
);do
uble
squ
are
(dou
ble)
;Th
en a
func
tion
call
squa
re (1
0);
will
cau
se a
n er
ror d
ue to
am
bigu
ity.
4. I
f al
l st
eps
fails
, th
en t
he c
ompi
ler
will
try
th
e us
er d
efin
ed c
onve
rsio
ns.
FUN
CTI
ON
S IN
C++
(con
td..)
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
147
Exam
ple
prog
ram
of f
unct
ion
over
load
ing
#inc
lude
<io
stre
am>
intv
olum
e (in
t);do
uble
vol
ume
(dou
ble,
int)
long
vlo
ume
(long
, int
, int
);vo
id m
ain(
){
cout
<< v
olum
e (1
0) <
< en
dl;
cout
<< v
olum
e (2
.5,8
) <<
endl
;co
ut<<
vol
ume
(100
L,75
,15)
<<
endl
;}
FUN
CTI
ON
S IN
C++
(con
td..)
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
148
intv
olum
e (in
ts)/
/cub
e{
retu
rn (s
*s*s
); }
doub
le v
olum
e (d
oubl
e r,
inth
) //c
ylin
der
{re
turn
(3.1
4*r*
r*h)
}lo
ng v
olum
e (lo
ng l,
intb
, int
h)
//rec
tang
ular
box
{re
turn
(l*b
*h)
}
FUN
CTI
ON
S IN
C++
(con
td..)
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
149
STA
TIC
MEM
BER
FU
NC
TIO
NA
mem
ber
func
tion
that
is d
ecla
red
stat
ic h
as
the
follo
win
g pr
oper
ties:
1.A
sta
tic f
unct
ion
can
have
acc
ess
to o
nly
othe
r st
atic
m
embe
rs
(func
tions
or
va
riabl
es) d
ecla
red
in th
e sa
me
clas
s.
2.A
sta
tic m
embe
r fu
nctio
n ca
n be
cal
led
usin
g th
e cl
ass
nam
e (in
stea
d of
its
ob
ject
s) a
s fo
llow
s:cl
ass_
nam
e::
func
tion_
nam
e;
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
150
Exam
ple
prog
. of s
tatic
mem
ber f
unct
ion:
#inc
lude
<io
stre
am>
clas
s te
st{
intc
ode;
sta
tic in
tcou
nt;
publ
ic:
void
set
code
(voi
d){
code
= +
+cou
nt;
}vo
id s
how
code
(voi
d){
cout
<<“o
bjec
t no.
: ”<
<cod
e<<e
ndl;
}st
atic
voi
d sh
owco
unt(
void
){
cout
<<“c
ount
: ”
<<co
unt<
<end
l;
}};ST
ATI
C M
EMB
ER F
UN
CTI
ON
(con
td..)
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
151
intt
est :
: cou
nt;
void
mai
n()
{te
st t1
,t2;
t1.s
etco
de()
;t2
.set
code
();
test
:: s
how
coun
t();
test
t3;
t3.s
etco
unt()
;te
st ::
sho
wco
unt()
;t1
.sho
wco
de()
;t2
.sho
wco
de()
;t3
.sho
wco
de()
;}ST
ATI
C M
EMB
ER F
UN
CTI
ON
(con
td..)
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
152
FRIE
ND
FU
NC
TIO
N
C++
al
low
s th
e co
mm
on
func
tion
to
be
mad
e fr
iend
ly w
ith m
ore
than
one
cla
sses
, th
ereb
y al
low
ing
the
func
tion
to
have
ac
cess
to
th
e pr
ivat
e da
ta
of
thes
e cl
asse
s. S
uch
a fu
nctio
n ne
ed n
ot b
e a
mem
ber o
f any
of t
hese
cla
sses
.To
mak
e an
out
side
fun
ctio
n “f
riend
ly”
to a
cl
ass,
w
e ha
ve
to
sim
ply
decl
are
this
fu
nctio
n as
a fr
iend
of t
he c
lass
.
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
153
clas
s ex
ampl
e{
data
mem
bers
…..
mem
ber f
unct
ions
dec
lara
tions
……
.fri
end
void
exa
mpl
efun
c(vo
id);
}FRIE
ND
FU
NC
TIO
N (c
ontd
..)
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
154
The
func
tion
decl
arat
ion
shou
ld
be
prec
eded
by
th
e ke
ywor
d fr
iend
. Th
e fu
nctio
n is
de
fined
el
sew
here
in
th
e pr
ogra
m l
ike
a no
rmal
C++
fun
ctio
n. T
he
func
tion
defin
ition
doe
s no
t use
eith
er th
e ke
ywor
d fr
iend
or
sc
ope
reso
lutio
n op
erat
or. A
fun
ctio
n ca
n be
dec
lare
d as
a
frie
nd in
any
num
ber
of c
lass
es.
A fr
iend
fu
nctio
n ha
s fu
ll ac
cess
rig
hts
to
the
priv
ate
mem
bers
of t
he c
lass
.
FRIE
ND
FU
NC
TIO
N (c
ontd
..)
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
155
A
frie
nd
func
tion
poss
esse
s ce
rtai
n sp
ecia
l ch
arac
teris
tics:
1.It
is n
ot i
n th
e sc
ope
of t
he c
lass
to
whi
ch i
t ha
s be
en d
ecla
red
as fr
iend
.2.
Sinc
e it
is n
ot i
n th
e sc
ope
of t
he c
lass
, it
cann
ot b
e ca
lled
usin
g th
e ob
ject
of t
hat c
lass
.3.
It ca
n be
invo
ked
like
a no
rmal
func
tion
with
out
the
help
of a
ny o
bjec
t.4.
Unl
ike
mem
ber
func
tions
, it
cann
ot a
cces
s th
e m
embe
r na
mes
dire
ctly
and
has
to
use
an
obje
ct n
ame
and
dot m
embe
rshi
p op
erat
or w
ith
each
mem
ber n
ame.
FRIE
ND
FU
NC
TIO
N (c
ontd
..)
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
156
FRIE
ND
FU
NC
TIO
N (c
ontd
..)
5. It
can
be
decl
ared
eith
er in
the
publ
ic o
r th
e pr
ivat
e pa
rt of
a c
lass
with
out
affe
ctin
g its
m
eani
ng.
6. U
sual
ly, i
t has
the
obje
cts
as a
rgum
ents
.
The
frien
d fu
nctio
n ar
e of
ten
used
in o
pera
tor
over
load
ing.
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
157
FRIE
ND
FU
NC
TIO
N (c
ontd
..)Ex
ampl
e of
frie
nd fu
nctio
n:cl
ass
sam
ple
{pr
ivat
e: in
ta; i
ntb;
publ
ic: v
oid
setv
alue
() { a
=25;
b=4
0; }
frie
nd fl
oat m
ean
(sam
ple
s);
}; float
mea
n (s
ampl
e s)
{ ret
urn
float
(s.a
+ s.
b)/2
.0; }
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
158
FRIE
ND
FU
NC
TIO
N (c
ontd
..)
void
mai
n()
{sa
mpl
e x;
x.se
tval
ue();
cout
<< “m
ean
valu
e =”
<<
mea
n(x)
;}
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
159
FRIE
ND
FU
NC
TIO
N (c
ontd
..)
Mem
ber
func
tion
of o
ne c
lass
can
be
frie
nd
func
tion
of a
noth
er c
lass
. In
suc
h ca
ses,
th
ey
are
defin
ed
usin
g th
e sc
ope
reso
lutio
n op
erat
or a
s sh
own
belo
w:
clas
s x
{ int
fun(
);};
clas
s y
{ frie
nd in
tx ::
fun(
); };
The
func
tion
fun(
) is
a m
embe
r of
cla
ss x
an
d a
frie
nd o
f cla
ss y
;
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
160
FRIE
ND
FU
NC
TIO
N (c
ontd
..)W
e ca
n al
so d
ecla
re a
ll th
e m
embe
r fun
ctio
n of
one
cla
ss a
s th
e fr
iend
func
tions
of
anot
her c
lass
. In
such
cas
es, t
he c
lass
is
cal
led
frie
nd c
lass
. Thi
s ca
n be
sp
ecifi
ed a
s fo
llow
s:
clas
s z
{ frie
nd c
lass
x; }
;
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
161
FRIE
ND
FU
NC
TIO
N (c
ontd
..)E
xam
ple
of fr
iend
cla
ss:
clas
s AB
C;
//for
war
d de
clar
atio
ncl
ass
XYZ
{ pr
ivat
e: in
tx;
publ
ic: v
oid
setv
alue
(inti
) {x=
i; }
frien
d vo
id m
ax (X
YZ, A
BC
);};
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
162
FRIE
ND
FU
NC
TIO
N (c
ontd
..)cl
ass
AB
C{
priv
ate
inta
;pu
blic
: voi
d se
tval
ue(in
ti) {
a=
i; }
frie
nd v
oid
max
(XYZ
, AB
C);
}; void
max
(XYZ
m, A
BC
, n)
{if
(m.x
> n.
a)co
ut<<
m.x
;el
se
cout
<< n
.a;
}
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
163
FRIE
ND
FU
NC
TIO
N (c
ontd
..)vo
id m
ain(
){
ABC
abc
;ab
c.se
tval
ue(1
0);
XYZ
xyz
;xy
z.se
tval
ue(2
0);
max
(xyz
, abc
);}
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
164
CO
NS
T M
EMB
ER F
UN
CTI
ON
If a
mem
ber f
unct
ion
does
not
alte
r any
dat
a in
the
cla
ss,
then
we
may
dec
lare
it
as a
co
nst m
embe
r fun
ctio
n as
follo
ws:
void
mul
(int,
int)
cons
t;do
uble
get
_bal
ance
() co
nst;
The
qual
ifier
co
nst
is
appe
nded
to
th
e fu
nctio
n pr
otot
ype
(in
both
de
clar
atio
n an
d de
finiti
on).
The
com
pile
r w
ill g
ener
ate
an e
rror
mes
sage
if s
uch
func
tions
try
to
alte
r the
dat
a va
lues
;
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
165
LOC
AL C
LASS
Cla
sses
can
be
defin
ed a
nd u
sed
insi
de a
fu
nctio
n or
a b
lock
. Su
ch c
lass
es a
re
calle
d as
loca
l cla
sses
. Exa
mpl
e:
void
test
(int
a){ -
---cl
ass
stud
ent {
----
};---
--st
uden
t s1(
a);
}
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
166
LOC
AL
CLA
SS
(con
td..)
Loca
l cl
asse
s ca
n us
e gl
obal
var
iabl
es (
decl
ared
ab
ove
the
func
tion)
an
d st
atic
va
riabl
es
decl
ared
ins
ide
the
func
tion
but
cann
ot u
se
auto
mat
ic
loca
l va
riabl
e.
The
glob
al
varia
bles
sho
uld
be u
sed
with
the
sco
pe
reso
lutio
n op
erat
or.
Ther
e ar
e so
me
rest
rictio
ns in
con
stru
ctin
g lo
cal
clas
ses.
Th
ey
cann
ot
have
st
atic
da
ta
mem
bers
and
mem
ber
func
tions
mus
t be
de
fined
in
side
th
e lo
cal
clas
s.
Encl
osin
g fu
nctio
n ca
nnot
acc
ess
the
priv
ate
mem
bers
of
a l
ocal
cla
ss.
How
ever
, w
e ca
n ac
hiev
e th
is b
y de
clar
ing
the
encl
osin
g fu
nctio
n as
fr
iend
.
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
167
INH
ERIT
AN
CE
Inhe
ritan
ce b
etw
een
clas
ses
An
impo
rtant
feat
ure
of c
lass
es is
the
inhe
ritan
ce.
This
one
allo
ws
us to
cre
ate
an o
bjec
t der
ived
fro
m a
noth
er o
ne, s
o th
at it
may
incl
ude
som
e of
the
oth
er's
mem
bers
plu
s its
ow
n on
es.
For
exam
ple,
we
are
goin
g to
sup
pose
tha
t w
e w
ant
to
decl
are
a se
ries
of
clas
ses
that
de
scrib
e po
lygo
ns li
ke o
ur C
Rec
tang
le, o
r lik
e C
Tria
ngle
. B
oth
have
ce
rtain
co
mm
on
feat
ures
, lik
e fo
r ex
ampl
e, t
he o
ne t
hat
both
ca
n be
des
crib
ed b
y m
eans
of
only
tw
o si
des:
he
ight
and
bas
e.
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
168
This
cou
ld b
e re
pres
ente
d in
the
wor
ld o
f cl
asse
s w
ith a
cl
ass
CPo
lygo
nfro
m t
hat
we
wou
ld d
eriv
e th
e tw
o pr
evio
us o
nes
CR
ecta
ngle
and
CTr
iang
le.
The
clas
s C
Poly
gon
wou
ld c
onta
in m
embe
rs t
hat
are
com
mon
for
all
poly
gons
. In
our
cas
e: w
idth
and
heig
ht.
And
CR
ecta
ngle
and
CTr
iang
lew
ould
be
its d
eriv
ed c
lass
es.
Cla
sses
der
ived
from
oth
ers
inhe
rit a
ll th
e vi
sibl
e m
embe
rs
of t
he b
ase
clas
s. T
hat
mea
ns t
hat
if a
base
cla
ss
incl
udes
a m
embe
r A
and
we
deriv
e it
to a
noth
er
clas
s w
ith a
noth
er m
embe
r ca
lled
B,
the
deriv
ed
clas
s w
ill c
onta
in b
oth
Aan
d B
.
INH
ERIT
AN
CE
(con
td..)
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
169
In o
rder
to
deriv
e a
clas
s fro
m a
noth
er o
ne,
we
shal
l us
e th
e op
erat
or :
(col
on)
in t
he d
ecla
ratio
n of
the
de
rived
cla
ss in
the
follo
win
g w
ay:
clas
s de
rived
_cla
ss_n
ame:
pub
lic
base
_cla
ss_n
ame;
w
here
der
ived
_cla
ss_n
ame
is th
e na
me
of th
e de
rived
clas
s an
d ba
se_c
lass
_nam
eis
the
nam
e of
the
cl
ass
on w
hich
is b
ased
. pub
licm
ay b
e re
plac
ed b
y an
yone
of
the
othe
r ac
cess
spe
cifie
rs p
rote
cted
or
priv
ate,
and
desc
ribes
the
acc
ess
for
the
inhe
rited
m
embe
rs, a
s w
e w
ill se
e rig
ht a
fter t
his
exam
ple:
INH
ERIT
AN
CE
(con
td..)
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
170
#inc
lude
<io
stre
am.h
>
clas
s C
Poly
gon
{pr
otec
ted:
intw
idth
, hei
ght;
publ
ic:
void
set
_val
ues
(inta
, int
b){ w
idth
=a; h
eigh
t=b;
}};
INH
ERIT
AN
CE
(con
td..)
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
171
clas
s C
Rec
tang
le: p
ublic
CP
olyg
on{
publ
ic:
inta
rea
(voi
d){ r
etur
n (w
idth
* h
eigh
t); }
}; clas
s C
Tria
ngle
: pub
lic C
Pol
ygon
{pu
blic
:in
tare
a (v
oid)
{ ret
urn
(wid
th *
hei
ght /
2);
}};
INH
ERIT
AN
CE
(con
td..)
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
172
intm
ain
() {
CR
ecta
ngle
rect
;C
Tria
ngle
trgl
;re
ct.s
et_v
alue
s(4
,5);
trgl
.set
_val
ues
(4,5
);co
ut<<
rect
.are
a() <
< en
dl;
cout
<< tr
gl.a
rea(
) <<
endl
;re
turn
0;
} 20 10
INH
ERIT
AN
CE
(con
td..)
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
173
As
you
may
se
e,
obje
cts
of
clas
ses
CR
ecta
ngle
and
CTr
iang
leco
ntai
n al
l of
them
mem
bers
of
CPo
lygo
n,th
at a
re: w
idth
, hei
ghta
nd s
et_v
alue
s().
The
prot
ecte
dsp
ecifi
eris
si
mila
r to
pr
ivat
e,its
on
ly
diffe
renc
e in
deed
occ
urs
whe
n de
rivin
g cl
asse
s. W
hen
we
deriv
e a
clas
s, p
rote
cted
mem
bers
of
the
base
cl
ass
can
be u
sed
by o
ther
mem
bers
of
the
deriv
ed
clas
s,
neve
rthel
ess
priv
ate
mem
ber
cann
ot.
As
we
wan
ted
that
wid
than
d he
ight
have
the
abi
lity
to b
e m
anip
ulat
ed
by
mem
bers
of
its
de
rived
cl
asse
s C
Rec
tang
lean
d C
Tria
ngle
and
not o
nly
by m
embe
rs o
f C
Poly
gon
we
have
use
d th
e pr
otec
ted
acce
ss in
stea
d of
priv
ate.
INH
ERIT
AN
CE
(con
td..)
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
174
INH
ERIT
AN
CE
(con
td..)
No
No
Yes
not-m
embe
rs
No
Yes
Yes
mem
bers
of d
eriv
ed c
lass
es
Yes
Yes
Yes
mem
bers
of t
he s
ame
clas
s
Priv
ate
Pro
tec
ted
Pub
licA
cces
s
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
175
In
our
exam
ple,
th
e m
embe
rs
inhe
rited
by
C
Rec
tang
lean
d C
Tria
ngle
follo
w w
ith t
he
sam
e ac
cess
per
mis
sion
tha
t in
the
bas
e cl
ass
CPo
lygo
n:C
Poly
gon:
:wid
th//
prot
ecte
d ac
cess
CR
ecta
ngle
::wid
th//
prot
ecte
d ac
cess
CPo
lygo
n::s
et_v
alue
s()
// p
ublic
acc
ess
CR
ecta
ngle
::set
_val
ues(
) //
publ
ic a
cces
s
INH
ERIT
AN
CE
(con
td..)
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
176
This
is
beca
use
we
have
der
ived
a c
lass
fro
m t
he
othe
r as
publ
ic,r
emem
ber:
clas
s C
Rec
tang
le: p
ublic
CPo
lygo
n;th
is p
ublic
keyw
ord
repr
esen
ts t
he m
inim
umle
vel
of
prot
ectio
n th
at t
he i
nher
ited
mem
bers
of
the
base
cl
ass
(CPo
lygo
n)m
ust
acqu
ire i
n th
e ne
w c
lass
(C
Rec
tang
le).
This
min
imum
acc
ess
leve
l fo
r th
e in
herit
ed m
embe
rs c
an b
e ch
ange
d if
inst
ead
of
publ
icw
e pu
t pr
otec
ted
or p
rivat
e,fo
r ex
ampl
e,
supp
ose
that
da
ught
eris
a
clas
s de
rived
fro
m
mot
hert
hat w
e de
fined
thus
: cl
ass
daug
hter
: pro
tect
ed m
othe
r;
INH
ERIT
AN
CE
(con
td..)
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
177
this
wou
ld e
stab
lish
prot
ecte
das
the
min
imum
acc
ess
leve
l for
the
mem
bers
of
daug
hter
that
it in
herit
ed
from
mot
her.
That
is, a
ll m
embe
rs th
at w
ere
publ
icin
mot
her
wou
ld b
ecom
e pr
otec
ted
in d
augh
ter,
that
wou
ld b
e th
e m
inim
um le
vel w
hich
they
can
be
inhe
rited
. O
f co
urse
, th
is
wou
ld
not
rest
rict
that
da
ught
erco
uld
have
its
own
publ
icm
embe
rs. T
he
min
imum
lev
el w
ould
onl
y be
est
ablis
hed
for
the
inhe
rited
mem
bers
of m
othe
r.
INH
ERIT
AN
CE
(con
td..)
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
178
TYPE
S O
F IN
HER
ITAN
CE
1.Si
ngle
Inhe
ritan
ce2.
Mul
tiple
Inhe
ritan
ce3.
Hie
rarc
hica
l Inh
erita
nce
4.M
ultil
evel
Inhe
ritan
ce5.
Hyb
rid In
herit
ance
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
179
SIN
GLE
INH
ERIT
ANC
E
A B
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
180
MU
LTIP
LE IN
HER
ITAN
CE
AB
C
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
181
HIE
RAR
CH
ICA
L IN
HER
ITAN
CE
A
BC
D
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
182
MU
LTIL
EVEL
INH
ER
ITAN
CE
A CB
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
183
HYB
RID
INH
ER
ITAN
CE
A D
BC
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
184
SIN
GLE
INH
ERIT
ANC
E
Exam
ple
#inc
lude
<io
stre
am.h
>cl
ass
B{
inta
;pu
blic
:in
tb; v
oid
get_
ab();
intg
et_a
(voi
d);
void
sho
w_a
(voi
d);
};
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
185
clas
s D
: pr
ivat
e B
{in
tc;
publ
ic:
void
mul
(voi
d);
void
dis
play
(voi
d);
};
SIN
GLE
INH
ERIT
AN
CE
(con
td..)
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
186
void
B ::
get
_ab
(voi
d){
cout
<< “e
nter
the
valu
e of
a a
nd b
:”;ci
n>>
a >
> b;
} intB
:: g
et_a
(){
retu
rn a
;}
void
B ::
sho
w_a
(){
cout
<< “a
= ”
<< a
<<
endl
;}
void
D ::
mul
(voi
d){
get_
ab()
;c
= b
* ge
t_a(
);}
SIN
GLE
INH
ERIT
AN
CE
(con
td..)
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
187
void
D ::
dis
play
(){
show
_a()
;co
ut<<
“b =
” <<
b <
< en
dl<<
“c =
” <<
c; }
intm
ain(
){
D d
;d.
get_
ab()
; d.s
how
_a()
; d.d
ispl
ay()
;d.
b=20
; d.m
ul();
d.d
ispl
ay()
; ret
urn
0;}
SIN
GLE
INH
ERIT
AN
CE
(con
td..)
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
188
A c
lass
can
inhe
rit th
e at
tribu
tes
of tw
o or
m
ore
clas
ses.
M
ultip
le
inhe
ritan
ce
allo
ws
us t
o co
mbi
ne t
he f
eatu
res
of
seve
ral
exis
ting
clas
ses
as a
sta
rting
po
int f
or d
efin
ing
new
cla
sses
.
MU
LTIP
LE IN
HER
ITA
NC
E
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
189
#inc
lude
<io
stre
am.h
>cl
ass
M{
prot
ecte
d: in
tm;
publ
ic :
void
get
_m(in
t);}; cl
ass
N{
prot
ecte
d : i
ntn;
publ
ic :
void
get
_n(in
t);}; cl
ass
P :
publ
ic M
, pub
lic N
{pu
blic
: vo
id d
ispl
ay (v
oid)
;};
void
M ::
get
_m(in
tx)
{m
= x
;}
void
N ::
get
_n(in
ty)
{n
= y;
}
MU
LTIP
LE IN
HER
ITA
NC
E (c
ontd
..)
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
190
void
P ::
dis
play
(voi
d){
cout
<< “m
= ”
<< m
<<
endl
;co
ut<<
“n =
” <<
n <
< en
dl;
cout
<< “m
*n =
” <<
m*n
<<
endl
;} in
tmai
n()
{P
p;p.
get_
m(1
0);
p.ge
t_n(
20);
p.di
spla
y();
retu
rn 0
;}
MU
LTIP
LE IN
HER
ITA
NC
E (c
ontd
..)
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
191
We
may
fa
ce
a pr
oble
m
in
usin
g th
e m
ultip
le
inhe
ritan
ce, w
hen
a fu
nctio
n w
ith th
e sa
me
nam
e ap
pear
s in
mor
e th
an o
ne b
ase
clas
s. C
onsi
der
the
follo
win
g tw
o cl
asse
s:cl
ass
M
{pu
blic
: vo
id d
ispl
ay(v
oid)
{cou
t<<“
clas
s M
”;}
} clas
s N
{
publ
ic :
void
dis
play
(voi
d){c
out<
<“cl
ass
N”;
}}
MU
LTIP
LE IN
HER
ITA
NC
E (c
ontd
..)
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
192
Whi
ch d
ispl
ay()
func
tion
is u
sed
by t
he d
eriv
ed
clas
s w
hen
we
inhe
rit th
ese
two
clas
ses?
We
can
solv
e th
is p
robl
em b
y de
finin
g a
nam
ed
inst
ance
with
in t
he d
eriv
ed c
lass
, us
ing
the
clas
s re
solu
tion
oper
ator
with
the
fun
ctio
n as
sh
own
belo
w:
clas
s P
: pub
lic M
, pub
lic N
{pu
blic
: vo
id d
ispl
ay(v
oid)
{M::d
ispl
ay()
;}};
MU
LTIP
LE IN
HER
ITA
NC
E (c
ontd
..)
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
193
We
can
now
use
the
deriv
ed c
lass
as
follo
ws:
intm
ain(
){
P p
;p.
disp
lay(
);}
Am
bigu
ity
may
al
so
aris
e in
si
ngle
in
herit
ance
ap
plic
atio
ns.
For
inst
ance
, co
nsid
er
the
follo
win
g si
tuat
ions
:cl
ass
A{
publ
ic :
void
dis
play
() {
cout
<< “A
”;}};
clas
s B
: pu
blic
A{
publ
ic :
void
dis
play
() {
cout
<< “B
”;}};
MU
LTIP
LE IN
HER
ITA
NC
E (c
ontd
..)
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
194
Ref
er t
he d
iagr
am o
f M
ultil
evel
inhe
ritan
ce.
The
clas
s A
serv
es a
s a
base
cla
ss fo
r th
e de
rived
cla
ss B
, whi
ch in
turn
ser
ves
as a
ba
se c
lass
for
the
der
ived
cla
ss C
. Th
e cl
ass
B
is
know
n as
IN
TER
MED
IATE
B
ASE
clas
s si
nce
it pr
ovid
es a
lin
k fo
r th
e in
herit
ance
bet
wee
n A
and
C.
The
chai
n AB
Cis
kn
own
as
INH
ERIT
AN
CE
PATH
.Th
is p
roce
ss c
an b
e ex
tend
ed t
o an
y nu
mbe
r of l
evel
s.
MU
LTIL
EVEL
INH
ERIT
AN
CE
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
195
#inc
lude
<io
stre
am.h
>cl
ass
stud
ent
{pr
otec
ted
: int
roll_
no;
publ
ic :
void
get
_no(
int);
void
put
_no(
void
);}; vo
id s
tude
nt ::
get
_no(
inta
){
roll_
no=
a;}
void
stu
dent
:: p
ut_n
o()
{co
ut<<
“R
oll N
umbe
r: ”
<<
roll_
no;
}
MU
LTIL
EVEL
INH
ERIT
AN
CE
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
196
clas
s te
st :
publ
ic s
tude
nt{
prot
ecte
d : f
loat
sub
1,su
b2;
publ
ic :
void
get
_mar
ks(fl
oat,f
loat
);vo
id p
ut_m
arks
(voi
d);
}; void
test
:: g
et_m
arks
(floa
tx,fl
oaty
){
sub1
=x; s
ub2=
y;}
void
test
:: p
ut_m
arks
(){
cout
<<“M
arks
in S
ubje
ct 1
= ”
<< s
ub1;
cout
<< “
Mar
ks in
Sub
ject
2=
” <<
sub
2;}M
ULT
ILEV
EL IN
HER
ITA
NC
E
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
197
clas
s re
sult
: pub
lic te
st{
float
tota
l;pu
blic
: vo
id d
ispl
ay(v
oid)
;}; vo
id re
sult
:: di
spla
y(vo
id)
{to
tal=
sub1
+sub
2;pu
t_no
(); p
ut_m
arks
();co
ut<<
“To
tal =
” <
< to
tal;
}
MU
LTIL
EVEL
INH
ERIT
AN
CE
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
198
void
mai
n()
{re
sult
stud
ent1
;st
uden
t1.g
et_n
o(99
);st
uden
t1.g
et_m
arks
(67.
5,72
.75)
;st
uden
t1.d
ispl
ay();
}
MU
LTIL
EVEL
INH
ERIT
AN
CE
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
199
Ther
e co
uld
be s
ituat
ions
whe
re w
e ne
ed t
o ap
ply
two
or m
ore
type
s of
inh
erita
nce
to
desi
gn a
pro
gram
. For
inst
ance
, con
side
r th
e ca
se o
f pro
cess
ing
the
stud
ent r
esul
ts a
s w
e di
scus
sed
in l
ast
exam
ple.
Ass
ume
that
we
have
to
gi
ve
wei
ghta
gefo
r sp
orts
be
fore
co
mpu
te t
he f
inal
res
ult.
The
spor
ts m
arks
st
ored
in s
epar
ate
clas
s as
SPO
RTS
. In
this
w
e ha
ve to
bui
ld n
ew re
latio
nshi
p as
follo
ws:
HYB
RID
INH
ERIT
AN
CE
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
200
HYB
RID
INH
ERIT
AN
CE
(con
td..)
stud
ent
test
resu
lt
spor
ts
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
201
HYB
RID
INH
ERIT
AN
CE
(con
td..)
#inc
lude
<io
stre
am.h
>cl
ass
stud
ent
{pr
otec
ted
: int
roll_
no;
publ
ic :
void
get
_no(
int);
void
put
_no(
void
);}; vo
id s
tude
nt ::
get
_no(
inta
){
roll_
no=
a;}
void
stu
dent
:: p
ut_n
o()
{co
ut<<
“R
oll N
umbe
r: ”
<<
roll_
no;
}
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
202
HYB
RID
INH
ERIT
AN
CE
(con
td..)
clas
s te
st :
publ
ic s
tude
nt{
prot
ecte
d : f
loat
par
t1,p
art2
;pu
blic
: vo
id g
et_m
arks
(floa
t,flo
at);
void
put
_mar
ks(v
oid)
;}; vo
id te
st ::
get
_mar
ks(fl
oatx
,floa
ty)
{pa
rt1=
x; p
art2
=y;
}vo
id te
st ::
put
_mar
ks()
{co
ut<<
“M
arks
obt
aine
d: ”
<<
endl
;co
ut<<
“Par
t 1=
” <<
par
t1; <
< en
dl;
cout
<< “
part
2=
” <<
par
t2; <
< en
dl;
}
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
203
clas
s sp
orts
{pr
otec
ted
: sco
re:
publ
ic :
void
get
_sco
re(fl
oats
){s
core
=s;
}vo
id p
ut_s
core
(){c
out<
<“sp
orts
Wt:
” <<
scor
e<<
endl
;}};HYB
RID
INH
ERIT
AN
CE
(con
td..)
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
204
clas
s re
sult
: pub
lic te
st, p
ublic
spo
rts
{flo
at to
tal;
publ
ic :
void
dis
play
(voi
d);
}; void
resu
lt ::
disp
lay(
void
){
tota
l = p
art1
+par
t2+s
core
;pu
t_no
();pu
t_m
arks
();pu
t_sc
ore(
);co
ut<<
“To
tal S
core
: ” <
< to
tal <
< en
dl;
}HYB
RID
INH
ERIT
AN
CE
(con
td..)
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
205
void
mai
n()
{re
sult
stud
ent1
;st
uden
t1.g
et_n
o(11
1);
stud
ent1
.get
_mar
ks(1
0.5,
14.
5);
stud
ent1
.get
_sco
re(9
.0);
stud
ent1
.dis
play
();}HYB
RID
INH
ERIT
AN
CE
(con
td..)
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
206
VIR
TUAL
BAS
E C
LAS
S
Con
side
r a s
ituat
ion
whe
re a
ll th
e th
ree
kind
s of
in
herit
ance
are
inv
olve
d. T
his
is e
xpla
ined
in
nex
t dia
gram
. The
CH
ILD
has
two
dire
ct
base
cla
sses
as
PAR
ENT1
& P
AREN
T2w
hich
the
mse
lves
hav
e a
com
mon
bas
e cl
ass
GR
AND
PAR
ENT.
The
CH
ILD
inhe
rit
the
prop
ertie
s of
GR
AND
PAR
ENT
via
two
sepa
rate
pa
ths.
Th
e G
RAN
DFA
THER
is
som
etim
es
refe
rred
to
as
indi
rect
ba
se
clas
s.
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
207
VIR
TUAL
BAS
E C
LAS
S (c
ontd
..)
Gra
ndpa
rent
Chi
ld
Par
ent 1
Par
ent 2
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
208
Inhe
ritan
ce b
y th
e C
HIL
Dm
ight
pos
e so
me
prob
lem
s.
All
the
publ
ic
and
prot
ecte
d m
embe
rs o
f G
RAN
DPA
REN
Tar
e in
herit
ed
into
CH
ILD
twic
e, f
irst
via
PAR
EN
T1an
d ag
ain
via
PAR
EN
T2.
This
m
eans
C
HIL
Dw
ould
hav
e du
plic
ate
sets
of
the
mem
bers
in
herit
ed
from
GR
AND
PAR
EN
T.
This
in
trodu
ces
ambi
guity
and
sho
uld
be a
void
ed.
VIR
TUAL
BAS
E C
LAS
S (c
ontd
..)
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
209
The
dupl
icat
ion
of
inhe
rited
m
embe
rs
due
to
thes
e m
ultip
le p
aths
can
be
avoi
ded
by m
akin
g th
e co
mm
on
base
cla
ss a
s vi
rtua
l bas
e cl
ass
whi
le d
ecla
ring
the
dire
ct o
r int
erm
edia
te b
ase
clas
s as
sho
wn
belo
w:
clas
s G
RA
ND
PA
RE
NT
{--
----
-};
clas
s P
AR
EN
T1 :
virtu
al p
ublic
GR
AN
DP
AR
EN
T{
----
----
-};
clas
s P
AR
EN
T2 :
virtu
al p
ublic
GR
AN
DP
AR
EN
T{
----
----
-};
clas
s C
HIL
D :
publ
ic P
AR
EN
T1, p
ublic
PA
RE
NT2
{--
----
---
};
VIR
TUAL
BAS
E C
LAS
S (c
ontd
..)
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
210
Whe
n a
clas
s is
mad
e a
virt
ual
base
cla
ss,
C++
tak
es n
eces
sary
act
ion
to s
ee t
hat
only
one
cop
y of
tha
t cl
ass
is i
nher
ited,
re
gard
less
of h
ow m
any
inhe
ritan
ce p
aths
ex
ist b
etw
een
the
virtu
al b
ase
clas
s an
d a
deriv
ed c
lass
.
VIR
TUAL
BAS
E C
LAS
S (c
ontd
..)
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
211
VIR
TUAL
BAS
E C
LAS
S (c
ontd
..)
stud
ent
resu
lt
test
spor
ts
As
virtu
al b
ase
clas
sA
s vi
rtual
bas
e cl
ass
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
212
#inc
lude
<io
stre
am.h
>cl
ass
stud
ent
{pr
otec
ted
: int
roll_
no;
publ
ic :
void
get
_no(
int);
void
put
_no(
void
);}; vo
id s
tude
nt ::
get
_no(
inta
){
roll_
no=
a;}
void
stu
dent
:: p
ut_n
o()
{co
ut<<
“R
oll N
umbe
r: ”
<<
roll_
no;
}
VIR
TUAL
BAS
E C
LAS
S (c
ontd
..)
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
213
VIR
TUAL
BAS
E C
LAS
S (c
ontd
..)cl
ass
test
: vi
rtua
l pub
lic s
tude
nt{
prot
ecte
d : f
loat
par
t1,p
art2
;pu
blic
: vo
id g
et_m
arks
(floa
t,flo
at);
void
put
_mar
ks(v
oid)
;}; vo
id te
st ::
get
_mar
ks(fl
oatx
,floa
ty)
{pa
rt1=
x; p
art2
=y;
}vo
id te
st ::
put
_mar
ks()
{co
ut<<
“M
arks
obt
aine
d: ”
<<
endl
;co
ut<<
“Par
t 1=
” <<
par
t1; <
< en
dl;
cout
<< “
part
2=
” <<
par
t2; <
< en
dl;
}
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
214
VIR
TUAL
BAS
E C
LAS
S (c
ontd
..)
clas
s sp
orts
: pu
blic
virt
ual s
tude
nt{
prot
ecte
d : s
core
:pu
blic
: vo
id g
et_s
core
(floa
ts)
{sco
re =
s;}
void
put
_sco
re()
{cou
t<<“
spor
ts W
t: ”
<<sc
ore<
< en
dl;}
};
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
215
VIR
TUAL
BAS
E C
LAS
S (c
ontd
..)cl
ass
resu
lt : p
ublic
test
, pub
lic s
port
s{
float
tota
l;pu
blic
: vo
id d
ispl
ay(v
oid)
;}; vo
id re
sult
:: di
spla
y(vo
id)
{to
tal =
par
t1+p
art2
+sco
re;
put_
no();
put_
mar
ks();
put_
scor
e();
cout
<< “
Tota
l Sco
re: ”
<<
tota
l <<
endl
;}
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
216
void
mai
n()
{re
sult
stud
ent1
;st
uden
t1.g
et_n
o(11
1);
stud
ent1
.get
_mar
ks(1
0.5,
14.
5);
stud
ent1
.get
_sco
re(9
.0);
stud
ent1
.dis
play
();
}VIR
TUAL
BAS
E C
LAS
S (c
ontd
..)
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
217
An
abst
ract
cla
ssis
one
that
is n
ot
used
to c
reat
e ob
ject
s. A
n ab
stra
ct
clas
s is
des
igne
d on
ly t
o ac
t as
a
base
cla
ss (t
o be
inhe
rited
by
othe
r cl
asse
s).
It is
a d
esig
n co
ncep
t in
pr
ogra
m d
evel
opm
ent a
nd p
rovi
des
a ba
se u
pon
whi
ch o
ther
cla
sses
m
ay b
e bu
ilt.
ABS
TRAC
T C
LAS
SES
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
218
As
long
as
no b
ase
clas
s co
nstru
ctor
ta
kes
any
argu
men
ts,
the
deriv
ed
clas
s ne
ed
not
have
a
cons
truct
or
func
tion.
How
ever
, if
any
base
cla
ss
cont
ains
a c
onst
ruct
or w
ith o
ne o
r mor
e ar
gum
ents
, the
n it
is m
anda
tory
for
the
deriv
ed c
lass
to h
ave
a co
nstru
ctor
and
pa
ss t
he a
rgum
ents
to
the
base
cla
ss
cons
truct
or.
CO
NS
TRU
CTO
RS
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
219
Whe
n bo
th
the
deriv
ed
and
base
cl
asse
s co
ntai
n co
nstru
ctor
s, t
he b
ase
cons
truct
or
is e
xecu
ted
first
and
then
the
cons
truct
or in
th
e de
rived
cla
ss is
exe
cute
d.In
ca
se
of
mul
tiple
in
herit
ance
, th
e ba
se
clas
ses
are
cons
truct
ed
in
the
orde
r in
w
hich
the
y ap
pear
in
the
decl
arat
ion
of
the
deriv
ed c
lass
.Sim
ilarly
, in
a m
ultil
evel
in
herit
ance
, th
e co
nstru
ctor
s w
ill
be
exec
uted
in th
e or
der o
f inh
erita
nce.
CO
NS
TRU
CTO
RS
(con
td..)
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
220
The
cons
truct
ors
for
virtu
al b
ase
clas
ses
are
invo
ked
befo
re a
ny
non-
virtu
al b
ase
clas
ses.
If th
ere
are
mul
tiple
virt
ual b
ase
clas
ses,
th
ey a
re in
voke
d in
the
ord
er in
w
hich
they
are
dec
lare
d.
CO
NS
TRU
CTO
RS
(con
td..)
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
221
CO
NS
TRU
CTO
RS
(con
td..)
C()
; virt
ual b
ase
B()
; ord
inar
y ba
seA
(); d
eriv
ed
Cla
ss A
: pu
blic
B, v
irtua
l pu
blic
C {
};
B()
; bas
e (fi
rst)
C();
bas
e (s
econ
d)A
(); d
eriv
ed
Cla
ss A
: pu
blic
B, p
ublic
C
{
};
A()
; bas
e co
nstru
ctor
B()
; der
ived
con
stru
ctor
Cla
ss B
: pu
blic
A {
};O
rder
of e
xecu
tion
Met
hod
of in
herit
ance
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
222
#inc
lude
<io
stre
am.h
>cl
ass
alph
a{
intx
; pu
blic
: al
pha(
inti
){x
=i; c
out<
< “a
lpha
initi
aliz
ed” <
< en
dl; }
void
sho
w_x
(voi
d){ c
out<
< “x
= ” <
< x
<< e
ndl;
}};
CO
NS
TRU
CTO
RS
(con
td..)
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
223
clas
s be
ta{
float
y;
publ
ic :
beta
(flo
at j)
{ y=j
; cou
t<<
beta
initi
aliz
ed” <
< en
dl; }
void
sho
w_y
(voi
d){ c
out<
< “Y
= ”
<< y
<<
endl
; }};
CO
NS
TRU
CTO
RS
(con
td..)
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
224
clas
s ga
mm
a : p
ublic
bet
a, p
ublic
alp
ha{ i
ntm
,n;
publ
ic :
gam
ma(
inta
, flo
at b
, int
c, in
td) :
alp
ha(a
),bet
a(b)
{m
=c; n
=d; c
out<
< “g
amm
a in
itial
ized
” <<
endl
; }vo
id s
how
_mn(
void
){ c
out<
< “M
= ”
<< m
<<
endl
;co
ut<<
“N =
” <<
n <
< en
dl; }
};
CO
NS
TRU
CTO
RS
(con
td..)
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
225
void
mai
n()
{ga
mm
a g(
5, 1
0, 1
5, 2
0);
g.sh
ow_x
();
g.sh
ow_y
();
g.sh
ow_m
n();
}
CO
NS
TRU
CTO
RS
(con
td..)
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
226
The
mec
hani
sm o
f gi
ving
suc
h sp
ecia
l m
eani
ngs
to a
n op
erat
or i
s kn
own
as
oper
ator
ove
rload
ing.
Ope
rato
r ov
erlo
adin
g pr
ovid
es a
fle
xibl
e op
tion
for
the
crea
tion
of
new
de
finiti
ons
for
mos
t of
th
e C
++
oper
ator
s.
OPE
RAT
OR
OVE
RLO
ADIN
G
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
227
We
can
over
load
all
the
C++
ope
rato
rs e
xcep
t th
e fo
llow
ing:
1.C
lass
mem
ber a
cces
s op
erat
ors
(.,.*)
2.Sc
ope
reso
lutio
n op
erat
or (:
:)3.
Size
ope
rato
r (si
zeof
)4.
Con
ditio
nal o
pera
tor (
?:)
OPE
RAT
OR
OVE
RLO
ADIN
G (c
ontd
..)
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
228
Alth
ough
the
sem
antic
s of
an
oper
ator
ca
n be
ext
ende
d, w
e ca
nnot
cha
nge
its
synt
ax. W
hen
an o
pera
tor i
s ov
erlo
aded
, its
or
igin
al
mea
ning
is
no
t lo
st.
For
inst
ance
, th
e op
erat
or
+,
whi
ch
has
been
ove
rload
ed t
o ad
d tw
o ve
ctor
s,
can
still
be u
sed
to a
dd tw
o in
tege
rs.
OPE
RAT
OR
OVE
RLO
ADIN
G (c
ontd
..)
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
229
To d
efin
e an
add
ition
al t
ask
to a
n op
erat
or,
we
mus
t spe
cify
wha
t it m
eans
in re
latio
n to
th
e cl
ass
to w
hich
the
ope
rato
r is
app
lied.
Th
is i
s do
ne w
ith t
he h
elp
of a
spe
cial
fu
nctio
n,
calle
d op
erat
or
func
tion,
w
hich
de
scrib
es th
e ta
sk. T
he g
ener
al fo
rm is
:
retu
rn ty
pe c
lass
nam
e ::
oper
ator
op
(arg
. lis
t){
func
tion
body
}
OPE
RAT
OR
OVE
RLO
ADIN
G (c
ontd
..)
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
230
whe
re r
etur
n ty
peis
the
typ
e of
val
ue r
etur
ned
by
the
spec
ified
ope
ratio
n an
d op
is th
e op
erat
or b
eing
ov
erlo
ad.
The
op
is
prec
eded
by
th
e ke
ywor
d op
erat
or.
Ope
rato
r fu
nctio
ns m
ust
be e
ither
mem
ber
func
tions
(n
on s
tatic
) or
frie
nd f
unct
ions
. A
bas
ic d
iffer
ence
be
twee
n th
em is
that
a fr
iend
func
tion
will
onl
y ha
ve
one
argu
men
t for
una
ry o
pera
tor
and
two
for
bina
ry
oper
ator
s,
whi
le
a m
embe
r fu
nctio
n ha
s no
ar
gum
ents
fo
r un
ary
oper
ator
an
d on
ly
one
for
bina
ry o
pera
tor.
OPE
RAT
OR
OVE
RLO
ADIN
G (c
ontd
..)
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
231
The
proc
ess
of
over
load
ing
invo
lves
th
e fo
llow
ing
step
s:1.
Cre
ate
a cl
ass
that
def
ines
the
data
type
that
is
to b
e us
ed in
the
over
load
ing
oper
atio
n.2.
Dec
lare
the
oper
ator
func
tion
oper
ator
op(
) in
th
e pu
blic
par
t of t
he c
lass
. It m
ay b
e ei
ther
a
mem
ber f
unct
ion
or a
frie
nd fu
nctio
n.3.
Def
ine
the
oper
ator
func
tion
to im
plem
ent t
he
requ
ired
oper
atio
ns.
OPE
RAT
OR
OVE
RLO
ADIN
G (c
ontd
..)
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
232
Ove
rload
ing
unar
y op
erat
or (u
nary
sub
tract
ion)
#inc
lude
<io
stre
am.h
>cl
ass
spac
e{ i
ntx,
y,z;
publ
ic:
void
get
data
(inta
, int
b, in
tc);
void
dis
play
(voi
d);
void
ope
rato
r --(
);};
OPE
RAT
OR
OVE
RLO
ADIN
G (c
ontd
..)
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
233
void
spa
ce ::
get
data
(inta
, int
b, in
tc)
{ x=a
; y=b
; z=c
;}
void
spa
ce ::
dis
play
(voi
d){ c
out<
< x
<< “
” <<
y <
< “
” <<
z; }
void
spa
ce o
pera
tor -
-()
{ x=
-x; y
= -
y; z
= -z
;}
OPE
RAT
OR
OVE
RLO
ADIN
G (c
ontd
..)
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
234
void
mai
n()
{sp
ace
S;S.
getd
ata(
1,2,
-3);
cout
<< “S
: ”;
S.di
spla
y();
--S;
cout
<< “S
: ”;
S.di
spla
y();
}
OPE
RA
TOR
OVE
RLO
AD
ING
(con
td..)
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
235
It is
po
ssib
le
to
over
load
a
unar
y m
inus
op
erat
or u
sing
a fr
iend
func
tion
as fo
llow
s:fri
end
void
ope
rato
r --(
spac
e &s
); //d
ecla
ratio
nvo
id o
pera
tor -
-(sp
ace
&s)
//def
initi
on{
s.x
= --
s.x;
s.y
= --
s.y;
s.z
= --
s.z;
}
OPE
RAT
OR
OVE
RLO
ADIN
G (c
ontd
..)
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
236
NO
TE:
The
argu
men
ts
is
pass
ed
by
refe
renc
e.
It w
ill
not
wor
k if
we
pass
ar
gum
ent b
y va
lue
beca
use
only
a c
opy
of
the
obje
ct th
at a
ctiv
ated
the
call
is p
asse
d to
op
erat
or
--().
Ther
efor
e th
e ch
ange
s m
ade
insi
de t
he o
pera
tor
func
tion
will
not
re
flect
in th
e ca
lled
obje
ct.
OPE
RAT
OR
OVE
RLO
ADIN
G (c
ontd
..)
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
237
Ove
rload
ing
bina
ry o
pera
tor
#inc
lude
<ios
tream
.h>
clas
s co
mpl
ex{
float
x, f
loat
y;
publ
ic:
com
plex
() {
}co
mpl
ex(fl
oatr
eal,
float
imag
){x
=rea
l;y=i
mag
;}co
mpl
ex o
pera
tor +
(com
plex
);vo
id d
ispl
ay (v
oid)
;}
OPE
RA
TOR
OVE
RLO
AD
ING
(con
td..)
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
238
com
plex
com
plex
:: o
pera
tor +
(com
plex
c)
{com
plex
tem
p; te
mp.
x=x+
c.x;
tem
p.y
= y+
c.y;
retu
rn (t
emp)
; }vo
id c
ompl
ex ::
dis
play
(voi
d){ c
out<
< x
<< “+
j” <
< y
<< e
ndl;
}vo
id m
ain(
){c
ompl
ex C
1,C
2,C
3;C
1=co
mpl
ex(2
.5,3
.5);
C2=
com
plex
(1.6
,2.7
);C
3=C
1+C
2;co
ut<<
“C1:
” ; C
1.di
spla
y();
cout
<< “C
2: ”
; C2.
disp
lay(
);co
ut<<
“C3:
” ; C
3.di
spla
y();
}
OPE
RA
TOR
OVE
RLO
AD
ING
(con
td..)
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
239
Not
e th
e fo
llow
ing
feat
ures
of
the
oper
ator
+
func
tion:
1.It
rece
ives
on
ly
one
com
plex
ty
pe
argu
men
ts e
xplic
itly.
2.It
retu
rns
a co
mpl
ex d
ata
type
.3.
It is
a m
embe
r fun
ctio
n of
com
plex
.A
s a
rule
, in
ove
rload
ing
of b
inar
y op
erat
ors,
th
e le
ft ha
nd o
pera
nd is
use
d to
invo
ke th
e op
erat
or
func
tion
and
the
right
ha
nd
oper
and
is p
asse
d as
an
argu
men
ts.
OPE
RA
TOR
OVE
RLO
AD
ING
(con
td..)
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
240
The
com
plex
num
ber
prog
ram
dis
cuss
ed e
arlie
r ca
n be
m
odifi
ed
usin
g a
frien
d op
erat
or
func
tion
as fo
llow
s:1.
Rep
lace
the
mem
ber
func
tion
decl
arat
ion
by
the
frien
d fu
nctio
n de
clar
atio
n.fri
end
com
plex
ope
rato
r+(c
ompl
exa,
com
plex
b)
2. R
edef
ine
the
oper
ator
func
tion
as fo
llow
s:co
mpl
ex o
pera
tor +
(com
plex
a, c
ompl
ex b
){r
etur
n co
mpl
ex ((
a.x+
b.x)
,(a.y
+b.y
))};
OPE
RA
TOR
OVE
RLO
AD
ING
(con
td..)
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
241
In th
is c
ase,
the
stat
emen
tC
3=C
1+C
2; is
equ
ival
ent t
oC
3=op
erat
or+(
C1,
C2)
;
OPE
RA
TOR
OVE
RLO
AD
ING
(con
td..)
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
242
#inc
lude
<io
stre
am.h
>cl
ass
dist
ance
{ priv
ate:
intf
eet;
float
inch
es;
publ
ic: d
ista
nce
() :
feet
(0),i
nche
s(0.
0) {
}di
stan
ce (i
ntft,
floa
t in)
: fe
et(ft
) , in
ches
(in)
{
}
void
get
dist
(){ c
out<
< “E
nter
feet
: ”;
cin
>> fe
et;
cout
<< “E
nter
inch
es: ”
; ci
n>>
inch
es; }
OPE
RA
TOR
OVE
RLO
AD
ING
(con
td..)
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
243
void
sho
wdi
st()
{ cou
t<<
feet
<<
“ ” <
< in
ches
; }di
stan
ce o
pera
tor +
(dis
tanc
e);
}; dist
ance
dis
tanc
e:: o
pera
tor +
(dis
tanc
e d2
) {in
tf =
feet
+ d
2.fe
et; f
loat
i =
inch
es +
d2.
inch
es;
if (i
>= 1
2.0)
{i-=
12.0
; f++
; } re
turn
dis
tanc
e(f,i
);}
OPE
RA
TOR
OVE
RLO
AD
ING
(con
td..)
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
244
void
mai
n()
{ dis
tanc
e di
st1,
dis
t3, d
ist4
; di
st1.
getd
ata(
);di
stan
ce d
ist2
(11,
6.25
); di
st3
= di
st1
+ di
st2;
dist
4 =
dist
1 +
dist
2 +
dist
3;co
ut<<
“dis
t1 =
”; d
ist1
.sho
wdi
st()
; cou
t<<
endl
;co
ut<<
“dis
t2 =
”; d
ist2
.sho
wdi
st()
; cou
t<<
endl
; co
ut<<
“dis
t3 =
”; d
ist3
.sho
wdi
st()
; cou
t<<
endl
; co
ut<<
“dis
t4 =
”; d
ist4
.sho
wdi
st()
; cou
t<<
endl
;}
OPE
RA
TOR
OVE
RLO
AD
ING
(con
td..)
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
245
Mat
hem
atic
al o
pera
tions
on
strin
gs#i
nclu
de <
strin
g.h>
#inc
lude
<io
stre
am.h
>cl
ass
strin
g{ c
har *
p; in
tlen
;pu
blic
: strin
g ()
{len=
0;p=
0;} /
/cre
ate
null
strin
gst
ring
(con
st c
har
*s);/
/cre
ate
strin
g fro
m
arra
yst
ring
(con
st s
tring
&s)
;//co
py c
onst
.~s
tring
() {d
elet
e p;
} //d
estru
ctor
OPE
RA
TOR
OVE
RLO
AD
ING
(con
td..)
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
246
frien
d st
ring
oper
ator
+ (
cons
t st
ring
&s,
cons
t stri
ng &
t); //
+ op
erat
orfri
end
int
oper
ator
<=
(c
onst
st
ring
&s,
cons
t stri
ng &
t); //
<= o
pera
tor
frien
d vo
id s
how
(con
st s
tring
s);
};st
ring
:: st
ring
(con
st c
har *
s){ l
en=
strle
n(s)
; p =
new
cha
r [le
n+1]
;st
rcpy
(p,s
) }
OPE
RA
TOR
OVE
RLO
AD
ING
(con
td..)
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
247
strin
g ::
strin
g (c
onst
stri
ng &
s)
{ len
= s.
len;
p =
new
cha
r[len
+1];
strc
py(p
,s.p
); }
strin
g op
erat
or +
(co
nst
strin
g &s
, co
nst
strin
g &t
){ s
tring
tem
p; te
mp.
len=
s.le
n+t.l
en;
tem
p.p=
new
cha
r[tem
p.le
n+1]
;st
rcpy
(tem
p.p,
s.p
); st
rcat
( tem
p.p,
t.p)
;re
turn
(tem
p);
}
OPE
RA
TOR
OVE
RLO
AD
ING
(con
td..)
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
248
int
oper
ator
<=
(c
onst
st
ring
&s,
cons
t st
ring
&t)
{ int
m =
strl
en(s
.p);
intn
= s
trlen
(t.p)
;if
(m<=
n) re
turn
(1);
else
retu
rn (0
);} vo
id s
how
(con
st s
tring
s)
{ cou
t<<
s.p;
}
OPE
RA
TOR
OVE
RLO
AD
ING
(con
td..)
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
249
void
mai
n()
{ stri
ng s
1 =
“Mum
bai”;
st
ring
s2 =
“Del
hi”;
strin
g s3
= “U
daip
ur”;
strin
g t1
, t2,
t3;
t1=s
1; t2
=s2;
t3=t
1+t2
; co
ut<<
sho
w(t1
); co
ut<<
show
(t2)
; co
ut<<
sho
w (t
3);
if (t1
<=t3
) sho
w (t
1);e
lse
show
(t3)
;}
OPE
RA
TOR
OVE
RLO
AD
ING
(con
td..)
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
250
Rul
es fo
r ope
rato
r ove
rload
ing:
1.O
nly
exis
ting
oper
ator
s ca
n be
ove
rload
ed.
New
ope
rato
r ca
nnot
be
crea
ted.
2.Th
e ov
erlo
aded
ope
rato
r mus
t hav
e at
leas
t one
ope
rand
that
is
of u
ser d
efin
ed ty
pe.
3.W
e ca
nnot
cha
nge
the
basi
c m
eani
ng o
f any
ope
rato
r.4.
Ove
rload
ed o
pera
tors
fol
low
the
syn
tax
rule
s of
the
orig
inal
op
erat
ors.
The
y ca
nnot
be
over
ridde
n.5.
Ther
e ar
e so
me
oper
ator
s th
at c
anno
t be
over
load
ed.
6.W
e ca
nnot
use
frie
nd fu
nctio
n to
ove
rload
cer
tain
ope
rato
rs.
7.W
hen
usin
g bi
nary
ope
rato
rs o
verlo
aded
thr
ough
a m
embe
r fu
nctio
n, t
he l
eft
hand
ope
rand
mus
t be
an
obje
ct o
f th
e re
leva
nt c
lass
.8.
Bin
ary
arith
met
ic o
pera
tors
mus
t exp
licit
retu
rn a
val
ue.
OPE
RA
TOR
OVE
RLO
AD
ING
(con
td..)
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
251
A p
oint
er i
s ju
st t
he m
emor
y ad
dres
s of
a
varia
ble,
so
that
a p
oint
er v
aria
ble
is j
ust
a va
riabl
e in
w
hich
w
e ca
n st
ore
diffe
rent
m
emor
y ad
dres
ses.
Po
inte
r va
riabl
es
are
decl
ared
usi
ng a
"*"
, and
hav
e da
ta ty
pes
like
the
othe
r va
riabl
es
we
have
se
en.
For
exam
ple,
the
decl
arat
ion
int*
num
ber_
ptr;
stat
es t
hat
"num
ber_
ptr"
is a
poi
nter
var
iabl
e th
at c
an s
tore
add
ress
es o
f va
riabl
es o
f da
ta
type
"in
t". A
use
ful a
ltern
ativ
e w
ay t
o de
clar
e po
inte
rs is
usi
ng a
"ty
pede
f" c
onst
ruct
.
Poin
ters
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
252
For
exam
ple,
if
we
incl
ude
the
stat
emen
t:ty
pede
fint
*IntP
trTy
pe;
we
can
then
go
on t
o de
clar
e se
vera
l po
inte
r va
riabl
es i
n on
e lin
e, w
ithou
t th
e ne
ed to
pre
fix e
ach
with
a "
*":
IntP
trTy
penu
mbe
r_pt
r1,
num
ber_
ptr2
, num
ber_
ptr3
;
Poin
ters
(con
td..)
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
253
Man
ipul
atio
n of
poi
nter
s;W
e ca
n m
anip
ulat
e a
poin
ter
with
th
e in
dire
ctio
n op
erat
or ‘*
’, w
hich
is a
lso
know
n as
def
eren
ce o
pera
tor.
With
thi
s op
erat
or,
we
can
indi
rect
ly a
cces
s th
e da
ta v
aria
ble
cont
ent.
It ta
kes
the
follo
win
g ge
nera
l for
m:
*poi
nter
_var
iabl
e;
Poin
ters
(con
td..)
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
254
#inc
lude
<io
stre
am.h
>#i
nclu
de <
coni
o.h>
void
mai
n ()
{in
ta =
10,
*pt
r;pt
r=&
a; c
lrscr
();
cout
<< “t
he v
alue
of a
is: ”
<<
a;*p
tr=
(*pt
r)/2
;co
ut<<
“The
val
ue o
f a is
: ” <
< (*
ptr);
} //c
heck
the
outp
ut
Poin
ters
(con
td..)
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
255
Poi
nter
exp
ress
ions
and
poi
nter
arit
hmet
ic:
A
poin
ter
can
be
incr
emen
ted
or
decr
emen
ted.
Any
inte
ger
can
be a
dded
or
subt
ract
ed
from
a p
oint
er.
One
po
inte
r ca
n be
su
btra
cted
fro
m
anot
her.Po
inte
rs (c
ontd
..)
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
256
Exa
mpl
e:#i
nclu
de <
iost
ream
.h>
#inc
lude
<co
nio.
h>vo
id m
ain(
){
intn
um[]
= {1
,2,3
,4,5
};in
t*pt
r; in
ti; c
lrscr
();
cout
<< “T
he a
rray
val
ues
are:
”;fo
r (i=
0; i<
5;i+
+) c
out<
< nu
m[i]
;pt
r= n
um;
cout
<< “V
alue
of p
tr: ”
<< *
ptr;
Poin
ters
(con
td..)
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
257
cout
<< “V
alue
of p
tr++”
<<
*ptr;
ptr-
-;co
ut<<
“Val
ue o
f ptr-
-” <
< *p
tr;pt
r=pt
r+2;
cout
<< “V
alue
of p
tr+2”
<<
*ptr;
ptr=
ptr-
1;co
ut<<
“Val
ue o
f ptr-
1” <
< *p
tr;pt
r+=3
;co
ut<<
“Val
ue o
f ptr+
3” <
< *p
tr;pt
r-=2
;co
ut<<
“Val
ue o
f ptr+
2” <
< *p
tr;}
Poin
ters
(con
td..)
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
258
Poin
ters
with
Arr
ays
& S
trin
gsPo
inte
rs i
s on
e of
the
effi
cien
t to
ols
to
acce
ss
elem
ents
of
an a
rray
. Po
inte
rs a
re u
sefu
l to
al
loca
te a
rray
dyn
amic
ally
, i.e
. we
can
deci
de
the
arra
y si
ze a
t run
tim
e. F
or th
is, w
e us
e th
e fu
nctio
n m
allo
c() a
nd c
allo
c().
We
can
decl
are
the
poin
ters
to a
rray
s as
follo
ws:
int*
nptr
;np
tr=
num
ber[
0]; o
r npt
r= n
umbe
r;H
ere
nptr
poin
ts t
o th
e fir
st e
lem
ent
of t
he in
tege
r ar
ray.
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
259
#inc
lude
<io
stre
am.h
>vo
id m
ain(
){in
tnum
ber[5
0], *
ptr;
intn
,i;co
ut<<
“ent
er th
e co
unt”;
cin
>> n
;co
ut<<
“ent
er th
e nu
mbe
r one
by
one”
;fo
r (i=
0;i<
n;i+
+)ci
n>>
num
ber[i
];
Poin
ters
with
Arr
ays
& S
trin
gs
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
260
ptr=
num
ber;
ints
um=0
;fo
r (i=
0;i<
n;i+
+){
if (*
ptr%
2==0
)su
m=s
um+*
ptr;
ptr+
+;} co
ut<<
“sum
of e
ven
num
bers
” <<
sum
;}Po
inte
rs w
ith A
rray
s &
Str
ings
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
261
Arr
ay o
f poi
nter
s:Th
e ar
ray
of p
oint
ers
repr
esen
ts a
col
lect
ion
of
addr
esse
s. B
y de
clar
ing
arra
y of
poi
nter
s, w
e ca
n sa
ve a
sub
stan
tial
amou
nt o
f m
emor
y sp
ace.
An
arra
y of
poi
nter
s po
int t
o an
arr
ay o
f dat
a ite
ms.
Ea
ch e
lem
ent o
f the
poi
nter
arr
ay p
oint
s to
an
item
of
the
data
arr
ay.
Dat
a ite
ms
can
be
acce
ssed
di
rect
ly
or
by
defe
renc
ing
the
elem
ents
of p
oint
er a
rray
.
Poin
ters
with
Arr
ays
& S
trin
gs
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
262
We
can
decl
are
an a
rray
of p
oint
ers
as fo
llow
s:in
t*in
arra
y[10
];Th
is s
tate
men
t de
clar
e an
arra
y of
10
poin
ters
, ea
ch o
f w
hich
po
ints
to
th
e in
tege
r. Th
e ad
dres
s of
firs
t po
inte
r is
ina
rray[
0] a
nd s
o on
. B
efor
e in
itial
izat
ion,
th
ey p
oint
to s
ome
unkn
own
valu
es.
Exa
mpl
e:
#inc
lude
<io
stre
am.h
>#i
nclu
de <
coni
o.h>
#inc
lude
<st
ring.
h>#i
nclu
de <
ctyp
e.h>
Poin
ters
with
Arr
ays
& S
trin
gs
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
263
void
mai
n()
{int
i=0;
char
*ptr
[10]
= {“
book
s”, “
TV”,
“co
mpu
ter”
}ch
ar s
tr[2
5];
clrs
cr();
cout
<< “
ente
r you
r fav
orite
leis
ure:
”;ci
n>>
str
;fo
r (i=
0;i<
3;i+
+){
if (!s
trcm
p(st
r,*pt
r[i])
){
cout
<< “
your
leis
ure
is a
vaila
ble”
; br
eak;
}} if
(i==3
)co
ut<<
“yo
ur le
isur
e is
not
ava
ilabl
e he
re”;
getc
h();
}Poin
ters
with
Arr
ays
& S
trin
gs
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
264
Poin
ters
to fu
nctio
nsTh
e po
inte
r to
fun
ctio
n is
kno
wn
as c
allb
ack
func
tion.
We
can
use
this
func
tion
poin
ters
to
re
fer
to
a fu
nctio
n us
ing
func
tion
poin
ters
. W
e ca
n al
low
a C
++ p
rogr
am t
o se
lect
a f
unct
ion
dyna
mic
ally
at
run
time.
W
e ca
n al
so
pass
a
func
tion
as
an
argu
men
t to
an
othe
r fu
nctio
n.
Her
e th
e fu
nctio
n is
pa
ssed
as
a
poin
ter.
The
func
tion
poin
ter c
anno
t be
dere
fere
nced
.
Poin
ters
with
Arr
ays
& S
trin
gs
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
265
C++
als
o al
low
s us
to
com
pare
tw
o fu
nctio
n po
inte
rs.
C++
pro
vide
s tw
o fu
nctio
n po
inte
rs a
s1.
Func
tion
poin
ter
poin
t to
sta
tic m
embe
r fu
nctio
n2.
Func
tion
poin
ter
poin
t to
no
n st
atic
m
embe
r fu
nctio
n, f
or n
on s
tatic
mem
ber
func
tion,
we
requ
ire h
idde
n ar
gum
ents
.
Poin
ters
with
Arr
ays
& S
trin
gs
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
266
we
can
defin
e as
follo
ws:
int(
*num
_fun
ctio
n(in
tx))
;It
only
cre
ates
a p
oint
er.
It do
es n
ot c
reat
e ac
tual
func
tion.
Exa
mpl
e:#i
nclu
de <
iost
ream
.h>
type
defv
oid
(*fu
nptr)
(int
, int
)
Poin
ters
with
Arr
ays
& S
trin
gs
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
267
void
add
(int
I, in
tj)
{cou
t<<
i “+”
<<
j <<
“=”
<< i+
j; }
void
sub
trac
tion
(intI
, int
j){ c
out<
< i “
-” <
< j <
< “=
” <<
i-j;
}vo
id m
ain(
){f
unpt
rptr
;pt
r= &
add;
ptr(
1,2)
; ptr
=&su
btra
ctio
n;
ptr(
3,2)
}Poin
ters
with
Arr
ays
& S
trin
gs
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
268
POIN
TER
TO
OB
JEC
TO
bjec
t poi
nter
are
use
ful i
n cr
eatin
g ob
ject
s at
run
tim
e. W
e ca
n al
so u
se a
n ob
ject
poi
nter
to
acce
ss th
e pu
blic
mem
ber o
f an
obje
ct.
Con
side
r a
clas
s ite
m
in
whi
ch
we
have
tw
o m
embe
r fu
nctio
n as
ge
tdat
a()
and
show
data
().
We
can
refe
r to
the
mem
ber
func
tions
of
item
s in
tw
o w
ays:
1.B
y us
ing
the
dot o
pera
tor a
nd th
e ob
ject
.2.
By
usin
g th
e ar
row
ope
rato
r an
d th
e ob
ject
po
inte
r.
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
269
so x
.get
data
(); a
nd x
.sho
wda
ta()
Is s
ame
as p
tr->g
etda
ta()
and
ptr->
sho
wda
ta();
Sinc
e *p
tris
an
alia
s of
x; w
e ca
n al
so u
se a
s(*
ptr)
.sho
wda
ta()
;N
ote:
the
par
enth
esis
are
nec
essa
ry b
ecau
se
the
dot
oper
ator
has
hig
her
prio
rity
then
in
dire
ctio
n / d
eref
eren
ce o
pera
tor.
POIN
TER
TO
OB
JEC
T
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
270
Exam
ple:
#inc
lude
<io
stre
am.h
>cl
ass
item
{int
code
; flo
at p
rice;
publ
ic:
void
get
data
(inta
, flo
at b
){c
ode
= a;
pric
e =
b;}
void
sho
w()
{cou
t<<
“cod
e:”
<< c
ode;
co
ut<<
“pr
ice:
” <<
pric
e}};
POIN
TER
TO
OB
JEC
T
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
271
cons
t int
size
= 2
;in
tmai
n()
{item
*p =
new
item
[siz
e];
item
*d =
p; i
ntx,
I; flo
at y
;fo
r (i=
0;i<
size
;i++)
{cou
t<<
“inpu
t cod
e an
d pr
ice”
<<
i+1;
cin
>> x
>>
y; p
->ge
tdat
a(x,
y); p
++;}
POIN
TER
TO
OB
JEC
T
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
272
for (
i=0;
i<si
ze;i+
+){
cout
<< “i
tem
: ” <
< i+
1;d-
>sho
w()
;} retu
rn 0
;}
POIN
TER
TO
OB
JEC
T
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
273
C++
U
ses
a un
ique
key
wor
d ca
lled
this
to
repr
esen
t a
obje
ct t
hat
invo
kes
a m
embe
r fu
nctio
n. th
isis
the
poin
ter t
hat p
oint
to th
e ob
ject
for
whi
ch t
his
func
tion
was
cal
led.
Fo
r ex
ampl
e, th
e fu
nctio
n ca
ll A.
max
() w
ill
set
the
poin
ter
this
to t
he a
ddre
ss o
f th
e ob
ject
A.
the
star
ting
addr
ess
is s
ame
as
the
addr
ess
of th
e fir
st v
aria
ble
in th
e cl
ass
stru
ctur
e.THIS
PO
INTE
R
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
274
This
un
ique
po
inte
r is
au
tom
atic
ally
pa
ssed
to
a
mem
ber
func
tion
whe
n it
is c
alle
d. T
he p
oint
er
this
ac
ts
as
an
impl
icit
argu
men
t to
al
l th
e m
embe
r fun
ctio
n.
Exa
mpl
e:
clas
s X
{ in
ta;
----
-};
The
priv
ate
varia
ble
a ca
n be
use
d di
rect
ly i
nsid
e a
mem
ber f
unct
ion,
like
a =
123;
We
can
also
use
the
fol
low
ing
stat
emen
t to
do
the
sam
e jo
b as
:th
is->
a=12
3;
THIS
PO
INTE
R
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
275
Rec
all t
hat,
whe
n a
bina
ry o
pera
tor i
s ov
erlo
aded
usi
ng
a m
embe
r fun
ctio
n, w
e pa
ss o
nly
one
argu
men
t to
the
func
tion.
Th
e ot
her
argu
men
t is
im
plic
itly
pass
ed
usin
g th
e po
inte
r th
is.
One
im
port
ant
appl
icat
ion
of t
he p
oint
er t
his
is t
o re
turn
the
ob
ject
it p
oint
s to
.Fo
r exa
mpl
e, th
e st
atem
ent,
retu
rn *t
his;
//che
ck th
e ou
tput
insi
de a
mem
ber
func
tion
will
ret
urn
the
obje
ct
that
in
voke
d th
e fu
nctio
n.
This
st
atem
ent
assu
mes
im
port
ance
whe
n w
e w
ant
to c
ompa
re
two
or m
ore
obje
cts
insi
de a
mem
ber
func
tion
and
retu
rn th
e in
voki
ng o
bjec
t as
a re
sult.
THIS
PO
INTE
R
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
276
#inc
lude
<io
stre
am.h
>#i
nclu
de <
cstri
ng.h
>cl
ass
pers
on{c
har n
ame[
20];
float
age
;pu
blic
:pe
rson
(cha
r *s,
floa
t a)
{strc
py(n
ame,
s);
age=
a; }
pers
on &
per
son
:: gr
eate
r (pe
rson
& x
){ i
f (x.
age
>= a
ge) r
etur
n x
else
retu
rn *t
his;
}vo
id d
ispl
ay()
{cou
t<<
“Nam
e: ”
<< n
ame
<< “A
ge: ”
}};
THIS
PO
INTE
R
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
277
void
mai
n()
{per
son
P1(
“Kap
il”,
22.2
5),
P2(
“Kar
tik”,
20.5
0),
P3(
“Lak
han”
, 19.
50);
pers
on P
= P
1.gr
eate
r(P3)
;co
ut<<
“Eld
er p
erso
n is
:”P
.dis
play
();
pers
on P
= P
1.gr
eate
r(P2)
;co
ut<<
“Eld
er p
erso
n is
:”P
.dis
play
();
}
THIS
PO
INTE
R
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
278
VIR
TUA
L FU
NC
TIO
NW
hen
we
use
the
sam
e fu
nctio
n na
me
in b
oth
the
base
and
der
ived
cla
ss, t
he fu
nctio
n in
bas
e cl
ass
is d
ecla
red
as v
irtua
lus
ing
the
keyw
ord
virtu
alpr
eced
ing
its n
orm
al d
ecla
ratio
n. W
hen
a fu
nctio
n is
mad
e vi
rtual
, C++
det
erm
ines
whi
ch fu
nctio
n to
us
e at
run
tim
e ba
sed
on t
he t
ype
of o
bjec
t po
inte
d to
by
the
base
poi
nter
, ra
ther
tha
n th
e ty
pe o
f th
e po
inte
r. Th
us b
y m
akin
g th
e ba
se
poin
ter
to
poin
t to
di
ffere
nt
obje
cts,
w
e ca
n ex
ecut
e di
ffere
nt v
ersi
ons
of th
e vi
rtual
func
tion.
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
279
#inc
lude
<io
stra
m.h
>cl
ass
Bas
e{p
ublic
:vo
id d
ispl
ay()
{cou
t<<
“dis
play
bas
e”;}
virtu
al v
oid
show
() {c
out<
< “s
how
bas
e”;}
}; clas
s D
eriv
ed :
publ
ic B
ase
{vo
id d
ispl
ay()
{cou
t<<
“dis
play
der
ived
”;}vo
id s
how
() {c
out<
< “s
how
der
ived
”;}};
VIR
TUA
L FU
NC
TIO
N
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
280
void
mai
n()
{Bas
e B
; Der
ived
D; B
ase
*bpt
r;co
ut<<
“bpt
rpoi
nts
to b
ase”
;bp
tr=&
B;
bptr-
>dis
play
(); b
ptr-
>sho
w()
;co
ut<<
“bpt
rpoi
nts
to d
eriv
ed”;
bptr=
&D
;bp
tr->d
ispl
ay()
; bpt
r->s
how
();
}
VIR
TUA
L FU
NC
TIO
N
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
281
Whe
n bp
tris
mad
e to
poi
nt t
o th
e ob
ject
D,
the
stat
emen
tbp
tr->d
ispl
ay()
; ca
lls o
nly
the
func
tion
asso
ciat
ed
with
th
e B
ase,
w
here
as
the
stat
emen
tbp
tr->s
how
(); c
alls
the
Der
ived
ver
sion
of
sh
ow.
This
is
be
caus
e th
e fu
nctio
n di
spla
y()
has
not
been
mad
e vi
rtual
in
the
Bas
e cl
ass.
VIR
TUA
L FU
NC
TIO
N
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
282
PUR
E VI
RTU
AL
FUN
CTI
ON
It is
nor
mal
pra
ctic
e to
dec
lare
a fu
nctio
n vi
rtual
in
side
the
base
cla
ss a
nd r
edef
ine
it in
the
deriv
ed c
lass
. The
func
tion
insi
de th
e ba
se
clas
s is
sel
dom
use
d an
d on
ly s
erve
s as
a
plac
ehol
der.
Such
“do-
noth
ing”
func
tion
may
be
defin
ed a
s:vi
rtual
voi
d di
spla
y()=
0;
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
283
Such
fun
ctio
ns a
re c
alle
d pu
re v
irtua
l fun
ctio
n.A
pure
vi
rtual
fu
nctio
n is
a
func
tion
decl
ared
in
a
base
cl
ass
that
ha
s no
de
finiti
on
rela
tive
to
the
base
cl
ass.
R
emem
ber
that
a c
lass
con
tain
ing
pure
vi
rtual
fun
ctio
n ca
nnot
be
used
to
decl
are
an o
bjec
t of
its
ow
n. S
uch
clas
ses
are
calle
d ab
stra
ct b
ase
clas
s.
PUR
E VI
RTU
AL
FUN
CTI
ON
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
284
Rul
es fo
r Virt
ual F
unct
ions
:1.
The
virt
ual f
unct
ion
mus
t be
mem
ber o
f som
e cl
ass.
2. T
hey
cann
ot b
e st
atic
mem
bers
.3.
The
y ar
e ac
cess
ed b
y us
ing
obje
ct p
oint
ers.
4. A
virt
ual f
unct
ion
can
be a
frie
nd o
f ano
ther
cla
ss.
5. A
virt
ual f
unct
ion
in a
bas
e cl
ass
mus
t be
defin
ed, e
ven
thou
gh it
may
not
be
used
.6.
The
pro
toty
pe o
f th
e ba
se c
lass
ver
sion
of
a vi
rtual
fu
nctio
n an
d al
l th
e de
rived
cla
ss v
ersi
on m
ust
be
iden
tical
. If
two
func
tion
with
the
sam
e na
me
have
di
ffere
nt
prot
otyp
e,
C++
co
nsid
ers
them
as
ov
erlo
aded
fu
nctio
ns,
and
the
virtu
al
func
tion
mec
hani
sm is
igno
red.
VIR
TUA
L FU
NC
TIO
N
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
285
7. W
e ca
nnot
hav
e vi
rtual
con
stru
ctor
s, b
ut w
e ca
n ha
ve v
irtua
l des
truct
ors.
8. W
hile
a b
ase
poin
ter c
an p
oint
to a
ny ty
pe o
f the
de
rived
obj
ect,
the
reve
rse
is n
ot tr
ue. T
hat i
s to
say
, w
e ca
nnot
use
a p
oint
er t
o a
deriv
ed
clas
s to
acc
ess
an o
bjec
t of t
he b
ase
type
.9.
If a
virt
ual f
unct
ion
is d
efin
ed in
a b
ase
clas
s, it
ne
ed
not
be
nece
ssar
ily
rede
fined
in
th
e de
rived
cla
ss. I
n su
ch c
ases
, cal
ls w
ill in
voke
th
e ba
se fu
nctio
n.
VIR
TUA
L FU
NC
TIO
N
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
286
CO
NSO
LE I/
O O
PER
ATI
ON
SPU
T() &
GET
() Fu
nctio
nPu
t() i
s us
ed w
ith c
out
and
get()
is
used
w
ith c
in.
Synt
ax:
Cha
r c;
cout
.put
(‘x’)
Cin
.get
(c);
valu
e m
ust b
e ch
arac
ter,
if w
e pa
ss
the
num
eric
va
lue
its
corr
espo
ndin
g A
SCII
char
acte
r will
be
prin
ted.
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
287
Exam
ple:
#inc
lude
<io
stre
am.h
>vo
id m
ain(
){i
ntco
unt =
0; c
har c
;co
ut<<
“in
put t
ext:
”; c
in.g
et(c
);w
hile
(c!=
‘\n’
){
cout
.put
(c);
coun
t ++;
cin
.get
(c);
}} /
/ che
ck th
e ou
tput
CO
NSO
LE I/
O O
PER
ATI
ON
S
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
288
Get
line(
) and
writ
e() f
unct
ion
We
can
read
and
dis
play
a l
ine
of t
ext
mor
e ef
ficie
ntly
us
ing
the
line
orie
nted
I/O
fu
nctio
ns.
The
getli
ne()
fun
ctio
n re
ads
the
who
le l
ine
of
text
that
end
s w
ith th
e ne
wlin
ech
arac
ter.
It ca
n be
invo
ked
as:
cin.
getli
ne(li
ne, s
ize)
;
CO
NSO
LE I/
O O
PER
ATI
ON
S
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
289
This
fun
ctio
n ca
ll in
voke
s th
e fu
nctio
n ge
tline
() w
hich
re
ads
char
acte
r in
put
into
the
var
iabl
e lin
e. T
he
read
ing
is
term
inat
ed
as
soon
as
ei
ther
th
e ne
wlin
ech
arac
ter
is
enco
unte
red
or
size
ch
arac
ter a
re re
ad. T
he n
ewlin
ech
arac
ter i
s re
ad
but
not
save
d. I
nste
ad,
it is
rep
lace
d by
the
nul
l ch
arac
ter.
Exa
mpl
e: char
nam
e[20
];ci
n.ge
tline
(nam
e, 2
0);
CO
NSO
LE I/
O O
PER
ATI
ON
S
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
290
We
can
also
read
stri
ngs
usin
g th
e op
erat
or >
>as
follo
ws:
cin
>> n
ame;
But
cin
can
read
stri
ngs
that
do
not
cont
ain
whi
te /
blan
k sp
ace.
Afte
r re
adin
g th
e st
ring,
cin
auto
mat
ical
ly a
dds
the
term
inat
ing
null
char
acte
r to
th
e ch
arac
ter a
rray.
CO
NSO
LE I/
O O
PER
ATI
ON
S
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
291
Exa
mpl
e: #inc
lude
<io
stre
am.h
>vo
id m
ain(
){i
ntsi
ze=2
0; c
har c
ity[2
0];
cout
<< “e
nter
city
nam
e:”;
cin
>> c
ity;
cout
<< “c
ity n
ame:
” <<
city
;co
ut<<
“ent
er c
ity n
ame
agai
n: ”;
cin.
getli
ne(c
ity, s
ize)
;co
ut<<
“city
nam
e no
w: ”
<<
city
;co
ut<<
“ent
er a
noth
er c
ity n
ame:
” ;ci
n.ge
tline
(city
,siz
e);
cout
<< “n
ew c
ity n
ame”
<<
city
;}//
che
ck th
e ou
tput
with
city
nam
es c
onta
inin
g w
hite
spa
ces
CO
NSO
LE I/
O O
PER
ATI
ON
S
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
292
The
writ
e()
func
tion
disp
lays
an
entir
e lin
e an
d ha
s th
e fo
llow
ing
form
:
cout
.writ
e(lin
e, s
ize)
;
The
first
arg
umen
t lin
e re
pres
ents
the
nam
e of
th
e st
ring
to b
e di
spla
yed
and
the
seco
nd
argu
men
t si
ze
indi
cate
s th
e nu
mbe
r of
ch
arac
ters
to d
ispl
ay.
CO
NSO
LE I/
O O
PER
ATI
ON
S
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
293
Exam
ple:
#inc
lude
<io
stre
am.h
>#i
nclu
de <
strin
g.h>
void
mai
n()
{cha
r *st
ring1
=“C
++ “;
cha
r *st
ring2
= “P
rogr
amm
ing”
;in
tm =
strl
en(s
tring
1); i
ntn
= st
rlen(
strin
g2);
for (
inti
=1; i
<n;i+
+){
cout
.writ
e(st
ring2
,i);
cout
<< “\
n”;
}fo
r (in
ti=n
; i>0
;i--)
{co
ut.w
rite(
strin
g2,i)
;co
ut<<
“\n”
;}
cout
.writ
e(st
ring1
,m).w
rite(
strin
g2,n
);co
ut<<
“\n”
;co
ut.w
rite(
strin
g1,1
0);
}//ch
eck
the
outp
ut
CO
NSO
LE I/
O O
PER
ATI
ON
S
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
294
CO
NSO
LE I/
O O
PER
ATI
ON
SFo
rmat
ted
cons
ole
I/O O
pera
tions
:
To c
lear
the
flags
spe
cifie
dU
nset
f()
To s
peci
fy fo
rmat
flag
s th
at c
an c
ontro
l the
form
of o
utpu
t di
spla
y (s
uch
as le
ft / r
ight
just
ifica
tion)
Setf(
)
To s
peci
fy a
cha
ract
er th
at is
use
d to
fill
the
unus
ed
porti
on o
f a fi
eld
Fill(
)
To s
peci
fy th
e no
. of d
igits
to b
e di
spla
yed
afte
r the
de
cim
al p
oint
of a
floa
t val
uePr
ecis
ion(
)
To s
peci
fy th
e re
quire
d fie
ld s
ize
for d
ispl
ayin
g an
out
put
valu
eW
idth
()
Task
sFu
nctio
n
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
295
Exe
rcis
e fo
r stu
dent
s:Pr
ogra
m 1
0.4
Prog
ram
10.
5Pr
ogra
m 1
0.6
Prog
ram
10.
7Pr
ogra
m 1
0.8
(from
Obj
ect
Orie
nted
Pro
gram
min
g w
ith C
++,
Third
Edi
tion
by E
Bal
agur
usam
y)
CO
NSO
LE I/
O O
PER
ATI
ON
S
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
296
WO
RK
ING
WIT
H F
ILES
Det
ails
of F
ile s
tream
cla
sses
Pro
vide
s su
ppor
ts fo
r sim
ulta
neou
s in
put a
nd o
utpu
t ope
ratio
ns.
Inhe
rits
the
func
tions
from
isst
ream
and
osst
ream
clas
ses
thro
ugh
iost
ream
.
fstre
am
Pro
vide
s ou
tput
ope
ratio
ns. I
nher
its p
ut()
, see
kp()
, tel
lp()
, writ
e()
func
tions
from
ost
ream
.O
fstre
am
Pro
vide
s in
put o
pera
tions
. Inh
erits
get
(), g
etlin
e(),
read
(), s
eekg
()
and
tellg
() fu
nctio
ns fr
om is
tream
.Ifs
tream
Pro
vide
s op
erat
ions
com
mon
to th
e fil
e st
ream
s. S
erve
s as
a
base
of f
stre
am, i
fstr
eam
,and
ofs
trea
mcl
ass.
Con
tain
s op
en()
and
clos
e()f
unct
ions
.
Fstre
amba
se
Its p
urpo
se is
to s
et th
e fil
e bu
ffers
to re
ad a
nd w
rite.
Con
tain
s O
penp
rotc
onst
ant u
sed
in th
e op
en()
of fi
le s
tream
cla
sses
. A
lso
cont
ain
clos
e()a
nd o
pen(
)as
mem
bers
.
File
buf
Con
tent
sC
lass
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
297
Ope
ning
a fi
le:
A fil
e ca
n be
ope
ned
in tw
o w
ays:
1.U
sing
the
cons
truct
or fu
nctio
n of
the
clas
s.2.
Usi
ng t
he m
embe
r fu
nctio
n op
en()
of t
he
clas
s.
WO
RK
ING
WIT
H F
ILES
(con
td.)
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
298
Ope
ning
file
usi
ng c
onst
ruct
or:
A c
onst
ruct
or is
use
d to
initi
aliz
e an
obj
ect
whi
le it
is
bein
g cr
eate
d.
Her
e,
a fil
enam
e is
us
ed
to
initi
aliz
e th
e fil
e st
ream
obj
ect.
This
invo
lves
the
follo
win
g st
eps:
a)C
reat
e a
file
stre
am o
bjec
t to
man
age
the
stre
am
usin
g th
e ap
prop
riate
cla
ss i.
e. th
e cl
ass
ofst
ream
is u
sed
to c
reat
e th
e ou
tput
stre
am a
nd th
e cl
ass
ifstre
amis
use
d to
cre
ate
the
inpu
t stre
am.
b)In
itial
ize
the
file
obje
ct w
ith th
e de
sire
d fil
enam
e.
WO
RK
ING
WIT
H F
ILES
(con
td.)
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
299
Exa
mpl
e:Th
is p
rogr
am u
ses
a si
ngle
file
for
bo
th w
ritin
g an
d re
adin
g th
e da
ta.
Firs
t, it
take
s da
ta f
rom
key
boar
d an
d w
rites
it t
o th
e fil
e. A
fter
the
writ
ing
is c
ompl
eted
, th
e fil
e is
clo
sed.
The
pro
gram
aga
in o
pen
the
sam
e fil
e, r
eads
the
inf
orm
atio
n al
read
y w
ritte
n to
it a
nd d
ispl
ays
the
sam
e.
WO
RK
ING
WIT
H F
ILES
(con
td.)
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
300
#inc
lude
<io
stre
am.h
>#i
nclu
de <
fstre
am.h
>vo
id m
ain(
){o
fstre
amou
tfile
(“Ite
m”)
;co
ut<<
“Ent
er it
em n
ame:
”;ch
ar n
ame[
30];
cin
>> n
ame;
outfi
le<<
nam
e <<
“\n”
;co
ut<<
“Ent
er it
em c
ost:
”;flo
at c
ost;
cin
>> c
ost;
outfi
le<<
cos
t <<
“\n” ;
WO
RK
ING
WIT
H F
ILES
(con
td.)
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
301
outfi
le.c
lose
();ifs
tream
infil
e(“It
em”)
;in
file
>> n
ame;
infil
e>>
cos
t;co
ut<<
“\n”
; co
ut<<
“ite
m n
ame:
” <<
nam
e;co
ut<<
“ite
m c
ost”
<< c
ost;
infil
e.cl
ose(
);}//
chec
k th
e ou
tput
WO
RK
ING
WIT
H F
ILES
(con
td.)
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
302
Ope
ning
file
usi
ng o
pen(
)S
ynta
x: file_
stre
am_c
lass
stre
am_o
bjec
t;st
ream
_obj
ect.o
pen(
“file
_nam
e”);
Exa
mpl
e:of
stre
amou
tfile
;ou
tfile
.ope
n(“d
ata1
”); o
utfil
e.cl
ose(
);ou
tfile
.ope
n(“d
ata2
”); o
utfil
e.cl
ose(
);
WO
RK
ING
WIT
H F
ILES
(con
td.)
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
303
The
prev
ious
pro
gram
seg
men
t op
ens
two
files
in
sequ
ence
for
writ
ing
the
data
. N
ote
that
the
firs
t fil
e is
clo
sed
befo
re
open
ing
the
seco
nd
one.
Th
is
is
nece
ssar
y be
caus
e a
stre
am
can
be
conn
ecte
d to
onl
y on
e fil
e at
a ti
me.
WO
RK
ING
WIT
H F
ILES
(con
td.)
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
304
#inc
lude
<io
stre
am.h
>#i
nclu
de <
fstre
am.h
>vo
id m
ain(
){o
fste
amfo
ut; f
out.o
pen(
“cou
ntry
”);
fout
<< “U
nite
d st
ates
of A
mer
ica\
n”;
fout
<< “U
nite
d Ki
ngdo
m\n
”;fo
ut<<
“Sou
th K
orea
\n”;
fout
.clo
se()
;
WO
RK
ING
WIT
H F
ILES
(con
td.)
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
305
fout
.ope
n(“c
apita
l”);
fout
<< “W
ashi
ngto
n\n”
;fo
ut<<
“Lon
don\
n”;
fout
<< “s
eoul
\n”;
fout
.clo
se();
cons
t int
N =
80;
char
line
[N];
ifstre
amfin
;fin
.ope
n(“c
ount
ry”)
;
WO
RK
ING
WIT
H F
ILES
(con
td.)
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
306
cout
<< “c
onte
nts
of c
ount
ry fi
le\n
”;w
hile
(fin)
{fin
.get
line(
line,
N);
cout
<< li
ne; }
fin.c
lose
();
fin.o
pen(
“cap
ital”)
;co
ut<<
“\nc
onte
nts
of c
apita
l file
\n”;
whi
le(fi
n){ f
in.g
etlin
e(lin
e,N
); co
ut<<
line
; }fin
.clo
se()
; }
//che
ck th
e ou
tput
WO
RK
ING
WIT
H F
ILES
(con
td.)
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
307
At t
imes
we
may
req
uire
to
use
two
or m
ore
files
sim
ulta
neou
sly.
For
exa
mpl
e, w
e m
ay
requ
ire t
o m
erge
tw
o so
rted
files
int
o a
third
so
rted
file.
Th
is
mea
ns,
both
th
e so
rted
flies
hav
e to
kep
t op
en f
or r
eadi
ng
and
the
third
one
kep
t op
en f
or w
ritin
g. I
n su
ch
case
s w
e ne
ed
to
crea
te
two
sepa
rate
in
put
stre
am
for
hand
ling
two
inpu
t fil
es
and
one
outp
ut
stre
am
for
hand
ling
the
outp
ut fi
le.
WO
RK
ING
WIT
H F
ILES
(con
td.)
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
308
#inc
lude
<io
stre
am.h
>#i
nclu
de <
fstre
am.h
>#i
nclu
de <
stdl
ib.h
>vo
id m
ain(
){c
onst
ints
ize
= 80
; cha
r lin
e[si
ze];
ifstre
amfin
1, fi
n2;
fin1.
open
(“cou
ntry
”); fi
n2.o
pen(
“cap
ital”)
;fo
r (in
ti=1
;i<=1
0;i+
+){ i
f (fin
1.eo
f()!=
0){ c
out<
< “e
xit f
rom
cou
ntry
\n”;
exit(
1);
}
fin1.
getli
ne(li
ne,s
ize)
; cou
t<<
“cap
ital o
f ” <
< lin
e;
WO
RK
ING
WIT
H F
ILES
(con
td.)
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
309
WO
RK
ING
WIT
H F
ILES
(con
td.)
if (fi
n2.e
of()!
=0)
{ cou
t<<
“exi
t fro
m c
apita
l\n”;
exit(
1);
}
fin2.
getli
ne(li
ne,s
ize)
; co
ut<<
line
;}
} The
outp
ut w
ill b
e sh
own
like:
Cap
ital o
f Uni
ted
stat
es o
f am
eric
aW
ashi
ngto
n ---
-----
-an
d so
on.
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
310
Det
ectin
g eo
f()
Whi
le (f
in)
An if
stre
amob
ject
, suc
h as
fin,
ret
urns
a v
alue
of
0 if
any
erro
r occ
urs
in th
e fil
e op
erat
ion
incl
udin
g th
e eo
fcon
ditio
n. T
hus
the
whi
le
loop
term
inat
es w
hen
fin re
turn
s a
valu
e of
0
on re
achi
ng th
e eo
fcon
ditio
n.
WO
RK
ING
WIT
H F
ILES
(con
td.)
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
311
Ther
e is
ano
ther
app
roac
h to
det
ect
the
eof
cond
ition
as
if (fi
n1.e
of()
!=0)
{exi
t(1);}
eof()
is
a
mem
ber
func
tion
of
ios
clas
s.
It re
turn
s a
non-
zero
val
ue if
the
end
of
file
cond
ition
is
en
coun
tere
d an
d a
zero
ot
herw
ise.
The
refo
re, t
he a
bove
sta
tem
ent
term
inat
es t
he p
rogr
am o
n re
achi
ng t
he
eof.
WO
RK
ING
WIT
H F
ILES
(con
td.)
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
312
We
have
us
ed
ifstre
aman
d of
stre
amco
nstru
ctor
s an
d th
e fu
nctio
n op
en()
to
cr
eate
new
file
s as
wel
l as
to
open
the
ex
istin
g fil
es.
In b
oth
the
case
s, w
e ha
ve
used
onl
y on
e ar
gum
ent
as t
he f
ilena
me.
H
owev
er,
thes
e fu
nctio
n ca
n ta
ke
two
argu
men
ts,
the
seco
nd o
ne f
or s
peci
fyin
g th
e fil
e m
ode.
WO
RK
ING
WIT
H F
ILES
(con
td.)
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
313
Syn
tax: st
ream
-obj
ect.o
pen(
“file
nam
e”,m
ode)
;Th
e se
cond
arg
umen
t sp
ecify
the
pur
pose
for
w
hich
the
file
is o
pene
d.
Se n
ext s
lide
for f
ile m
ode
para
met
ers.
WO
RK
ING
WIT
H F
ILES
(con
td.)
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
314
WO
RK
ING
WIT
H F
ILES
(con
td.)
FILE
MO
DE
PAR
AMET
ERS
Del
ete
the
cont
ents
of t
he fi
le if
it e
xist
sIo
s::tr
unc
Ope
n fil
e fo
r writ
ing
only
Ios:
:out
Ope
n fa
ils fi
le th
e fil
e al
read
y ex
ist
Ios:
:nor
epla
ce
Ope
n fa
ils if
the
file
does
not
exi
stIo
s::n
ocre
ate
Ope
n fil
e fo
r rea
ding
onl
yIo
s::in
Bin
ary
file
Ios:
:bin
ary
Go
to e
nd o
f file
on
open
ing
Ios:
:ate
App
end
to e
nd o
f file
Ios:
:app
Mea
ning
Para
met
er
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
315
File
poi
nter
s an
d th
eir m
anip
ulat
ions
:Ea
ch fi
le h
as it
s tw
o as
soci
ated
poi
nter
s kn
own
as t
he f
ile p
oint
ers.
One
of
them
is c
alle
d th
e in
put
poin
ter
(or
get
poin
ter)
and
the
othe
r is
cal
led
the
outp
ut p
oint
er (
or p
ut
poin
ter)
.W
e ca
n us
e th
ese
poin
ters
to
mov
e th
roug
h th
e fil
es w
hile
rea
ding
or
writ
ing.
WO
RK
ING
WIT
H F
ILES
(con
td.)
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
316
The
inpu
t po
inte
r is
us
ed
for
read
ing
the
cont
ent
of t
he g
iven
file
loc
atio
n an
d th
e ou
tput
poi
nter
is u
sed
for w
ritin
g to
a g
iven
fil
e lo
catio
n. E
ach
time
an in
put
or o
utpu
t op
erat
ion
take
s pl
ace,
th
e ap
prop
riate
po
inte
r is
auto
mat
ical
ly a
dvan
ced.
WO
RK
ING
WIT
H F
ILES
(con
td.)
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
317
Func
tions
for m
anip
ulat
ion
of fi
le p
oint
ers:
seek
g()
mov
es
get
poin
ter
to
a sp
ecifi
ed
loca
tion
seek
p()
mov
es
put
poin
ter
to
a sp
ecifi
ed
loca
tion
tellg
()gi
ve
the
curre
nt
posi
tion
of
the
get
poin
ter
tellp
()gi
ves
the
curre
nt
posi
tion
of
the
put
poin
ter
WO
RK
ING
WIT
H F
ILES
(con
td.)
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
318
Sequ
entia
l Inp
ut a
nd O
utpu
t ope
ratio
ns:
The
file
stre
am c
lass
su
ppor
t a
num
ber
of
mem
ber
func
tion
for
perfo
rmin
g th
e in
put
and
outp
ut o
pera
tions
on
files
.O
ne
pair
of
func
tion
put()
an
d ge
t()ar
e de
sign
ed fo
r han
dlin
g a
sing
le c
hara
cter
at
a tim
e.
WO
RK
ING
WIT
H F
ILES
(con
td.)
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
319
Anot
her
pair
of f
unct
ion
writ
e()
and
read
()ar
e de
sign
ed to
writ
e an
d re
ad b
lock
s of
bin
ary
data
.
Exe
rcis
e:Pr
ogra
m 1
1.4
Prog
ram
11.
5(fr
om O
bjec
t O
rient
ed P
rogr
amm
ing
with
C++
, Th
ird E
ditio
n by
E B
alag
urus
amy)
WO
RK
ING
WIT
H F
ILES
(con
td.)
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
320
Rea
ding
and
Writ
ing
a C
lass
Obj
ect:
One
of t
he s
hort
com
ing
of th
e I/O
sys
tem
in
C i
s th
at i
t ca
nnot
han
dle
user
-def
ined
da
ta ty
pes
such
as
clas
s ob
ject
s. S
ince
th
e cl
ass
obje
cts
are
the
cent
ral
elem
ents
of
C
++
prog
ram
min
g,
it is
qu
ite
natu
ral
that
th
e la
ngua
ge
supp
orts
fe
atur
es
for
writ
ing
to
and
re
adin
g fr
om
the
disk
fil
e ob
ject
s di
rect
ly.
WO
RK
ING
WIT
H F
ILES
(con
td.)
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
321
The
bina
ry
inpu
t an
d ou
tput
fu
nctio
ns
read
() an
d w
rite(
) ar
e de
sign
ed to
do
exac
tly th
is jo
b. T
hese
fu
nctio
ns h
andl
es th
e en
tire
stru
ctur
e of
an
obje
ct
as a
sin
gle
unit.
O
ne p
oint
to r
emem
ber
is th
at o
nly
data
mem
bers
are
w
ritte
n to
the
disk
file
and
the
mem
bers
are
not
.E
xerc
ise:
Pro
gram
11.
6(fr
om O
bjec
t O
rient
ed P
rogr
amm
ing
with
C++
, Th
ird
Edi
tion
by E
Bal
agur
usam
y)
WO
RK
ING
WIT
H F
ILES
(con
td.)
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
322
Upd
atin
g a
file:
Ran
dom
Acc
ess
Upd
atin
g is
a r
outin
e ta
sk i
n th
e m
aint
enan
ce o
f an
y da
ta fi
le. T
he u
pdat
ing
wou
ld in
clud
e on
e or
mor
e of
the
follo
win
g ta
sks:
1.D
ispl
ayin
g th
e co
nten
ts o
f a fi
le2.
Mod
ifyin
g an
exi
stin
g ite
m3.
Add
ing
a ne
w it
em4.
Del
etin
g an
exi
stin
g ite
mE
xerc
ise:
Pro
gram
11.
7(fr
om O
bjec
t O
rient
ed P
rogr
amm
ing
with
C++
, Th
ird
Edi
tion
by E
Bal
agur
usam
y)
WO
RK
ING
WIT
H F
ILES
(con
td.)
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
323
Err
or h
andl
ing
durin
g fil
e op
erat
ions
:O
ne o
f the
follo
win
g th
ings
may
hap
pen
whe
n de
alin
g w
ith th
e fil
e:1.
A fi
le w
hich
we
are
atte
mpt
ing
to o
pen
for r
eadi
ng
does
not
exi
st.
2.Th
e fil
e na
me
used
for
a n
ew f
ile m
ay a
lread
y ex
ist.
3.W
e m
ay a
ttem
pt a
n in
valid
file
.4.
Ther
e m
ay n
ot b
e an
y sp
ace
in t
he d
isk
for
savi
ng m
ore
data
.5.
We
may
use
an
inva
lid fi
le n
ame.
6.W
e m
ay a
ttem
pt t
o pe
rform
an
oper
atio
n w
hen
the
file
is n
ot o
pene
d fo
r tha
t pur
pose
.
WO
RK
ING
WIT
H F
ILES
(con
td.)
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
324
WO
RK
ING
WIT
H F
ILES
(con
td.)
ER
RO
R H
AN
DLI
NG
FU
NC
TIO
NS
Ret
urns
true
if n
o er
ror o
ccur
red.
Thi
s m
eans
all
the
abov
e fu
nctio
ns a
re fa
lse.
Whe
n it
retu
rns
fals
e, n
o fu
rther
ope
ratio
n ca
n be
car
ried
out
Goo
d()
Ret
urns
true
if a
n in
valid
ope
ratio
n is
atte
mpt
ed o
r an
y un
reco
vera
ble
erro
r has
occ
urre
d. H
owev
er, i
f it
is fa
lse,
it m
ay b
e po
ssib
le to
reco
ver f
rom
any
ot
her e
rror r
epor
ted,
and
con
tinue
ope
ratio
n.
Bad(
)
Ret
urns
true
whe
n an
inpu
t or o
utpu
t ope
ratio
n ha
s fa
iled
Fail(
)
Ret
urns
true
(non
zer
o) if
end
of f
ile is
enc
ount
ered
w
hile
read
ing;
oth
erw
ise
retu
rn fa
lse
(zer
o)Eo
f()
Ret
urn
valu
e an
d m
eani
ngFu
nctio
n
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
325
The
two
mos
t com
mon
type
s of
bug
s ar
e lo
gic
erro
rs a
nd s
ynta
ctic
erro
rs.
The
logi
c er
ror
occu
rred
due
to
poor
un
ders
tand
ing
of th
e pr
oble
m.
The
synt
actic
er
rors
aris
e du
e to
po
or
unde
rsta
ndin
g of
the
lang
uage
itse
lf.
EXC
EPTI
ON
HA
ND
LIN
G
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
326
We
ofte
n co
me
acro
ss
som
e pe
culia
r pr
oble
ms
othe
r th
an
the
two
men
tione
d pr
oble
ms.
The
y ar
e kn
own
as E
XCEP
TIO
NS.
Ex
cept
ions
are
run
time
anom
alie
s th
at a
pr
ogra
m
may
en
coun
ter
whi
le
exec
utin
g. I
t m
ay i
nclu
de c
ondi
tions
as
div
isio
n by
zer
o, a
cces
s to
an
arra
y ou
tsid
e of
its
boun
dary
etc
.
EXC
EPTI
ON
HA
ND
LIN
G (c
ontd
.)
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
327
Exce
ptio
n ha
ndlin
g w
as n
ot p
art
of t
he
orig
inal
C++
.It i
s a
new
feat
ure
adde
d to
AN
SI C
++.
Toda
y, a
lmos
t al
l th
e co
mpi
lers
sup
port
thi
s fe
atur
es.
C++
ex
cept
ion
hand
ling
prov
ides
a
inte
grat
ed a
ppro
ach
for
copi
ng w
ith
the
unus
ual
pred
icta
ble
prob
lem
s th
at
aris
es
whi
le
exec
utin
g a
prog
ram
.
EXC
EPTI
ON
HA
ND
LIN
G (c
ontd
.)
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
328
Bas
ics
of e
xcep
tion
hand
ling:
Exc
eptio
ns
are
two
kind
s as
sy
nchr
onou
s an
d as
ynch
rono
us e
xcep
tions
.E
rrors
suc
h as
out
of r
ange
inde
x or
ove
rflow
bel
ong
to
sync
hron
ous
exce
ptio
ns.
The
erro
rs t
hat
are
caus
ed b
y ev
ents
bey
ond
the
cont
rol
of t
he p
rogr
am a
re c
alle
d as
ynch
rono
us
exce
ptio
ns.
The
prop
osed
ex
cept
ion
hand
ling
mec
hani
sm i
n C
++ i
s de
sign
ed t
o ha
ndle
onl
y sy
nchr
onou
s ex
cept
ions
.
EXC
EPTI
ON
HA
ND
LIN
G (c
ontd
.)
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
329
The
mec
hani
sm
sugg
ests
a
sepa
rate
er
ror
hand
ling
code
tha
t pe
rform
s th
e fo
llow
ing
task
s:1.
Find
the
prob
lem
(HIT
EXC
EPTI
ON
)2.
Info
rm th
at a
n er
ror h
as o
ccur
red
(TH
RO
W
EXC
EPTI
ON
)3.
Rec
eive
th
e er
ror
info
rmat
ion
(CAT
CH
EX
CEP
TIO
N)
4.Ta
ke a
ctio
n (H
AND
LE E
XCEP
TIO
N)
EXC
EPTI
ON
HA
ND
LIN
G (c
ontd
.)
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
330
C++
exc
eptio
n ha
ndlin
g m
echa
nism
is b
asic
ally
bu
ilt u
pon
thre
e ke
ywor
ds a
s:
1.TR
Y2.
THR
OW
3.C
ATC
H
EXC
EPTI
ON
HA
ND
LIN
G (c
ontd
.)
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
331
……
.. try {……
..th
row
exc
eptio
n;…
…..
} catc
h (ty
pe a
rg)
{ ……
.}
EXC
EPTI
ON
HA
ND
LIN
G (c
ontd
.)
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
332
Exa
mpl
e:#i
nclu
de <
iost
ream
.h>
void
mai
n()
{int
a,b;
cout
<< “e
nter
val
ue o
f a a
nd b
”;ci
n>>
a; c
in>>
b; i
ntx
= a-
b;try { i
f (x!
=0)
cout
<< “R
esul
t is:
” <<
a/x;
el
seth
row
(x);
}
EXC
EPTI
ON
HA
ND
LIN
G (c
ontd
.)
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
333
catc
h (in
ti)
{cou
t<<
“exc
eptio
n ca
ught
: x =
” <<
x; }
cout
<< “e
nd”;
} // c
heck
the
answ
er w
ith fo
llow
ing
inpu
ta=
10, b
=2a=
20, b
=20
EXC
EPTI
ON
HA
ND
LIN
G (c
ontd
.)
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
334
Mor
e of
ten,
exc
eptio
ns a
re th
row
n by
func
tions
th
at a
re in
voke
d fro
m w
ithin
the
try b
lock
s.
The
poin
t at w
hich
the
thro
w is
exe
cute
d is
ca
lled
the
thro
w p
oint
. O
nce
an e
xcep
tion
is th
row
n to
the
catc
h bl
ock,
con
trol c
anno
t re
turn
to
th
e th
row
po
int.
This
ki
nd
of
rela
tions
hip
is a
s fo
llow
s:
EXC
EPTI
ON
HA
ND
LIN
G (c
ontd
.)
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
335
type
func
tion(
arg
list)
{ ….
thro
w(o
bjec
t); …
…. /
/ thr
ow e
xcep
tion
} ……
..try { …
…..
//in
voke
func
tion
here
} catc
h (ty
pe a
rg)
{ ……
… /
/ han
dles
exc
eptio
n he
re}
EXC
EPTI
ON
HA
ND
LIN
G (c
ontd
.)
Dee
pak
Gou
r, Fa
culty
-Dep
artm
ent o
f IT
SP
SSE,
Uda
ipur
336
Exe
rcis
e fo
r stu
dent
s:Pr
ogra
m 1
3.2
Prog
ram
13.
3Pr
ogra
m 1
3.4
Prog
ram
13.
5Pr
ogra
m 1
3.6
(from
Obj
ect
Orie
nted
Pro
gram
min
g w
ith C
++,
Third
Edi
tion
by E
Bal
agur
usam
y)
EXC
EPTI
ON
HA
ND
LIN
G (c
ontd
.)