You are on page 1of 34

LC231 Cb[ecL CrlenLed

rogrammlng and uaLa


SLrucLures uslng C++
LecLure 3
LecLure ueLalls
W Cverloaded luncLlons
W lnllne luncLlons
W uefaulL ArgumenLs
luncLlon Cverloadlng
W Cverloaded luncLlons
W Same name
W ulfferenL parameLers
ulfferenL number of parameLers or
ulfferenL Lype of parameLers
used Lo perform slmllar Lasks
Lg funcLlon Lo calculaLe Lhe square of lnLs and funcLlon Lo
calculaLe square of floaLs
nt square{nt x)
{return x*x,)
11oat square{11oat x)
{return x*x,)
over1oaded1unc.cpp
ow does Lhe compller know whlch funcLlon Lo call??
W 1he compller looks aL Lhe siqnoture of Lhe funcLlon
name and parameLer Lypes (order maLLers)
name mangllng
W Lncodes funcLlon ldenLlfler wlLh parameLers
W a mechanlsm of C++ compller Lo llnk funcLlons and varlables of
same names Lo resolve Lhem Lo some unlque symbol names
1ypesafe llnkage
W Lnsures proper overloaded funcLlon called
W Lnforces Lhe rlghL number and Lype of parameLers are passed aL
llnk Llme
funcLlonoverload2
luncLlon Cverloadlng
luncLlons
ros
W lmprove sLrucLure and clarlLy
W Avold repeLlLlon of code
Save memory
Cons
W call Lo every funcLlon Lakes up Llme
SoluLlon
W lnllne luncLlons
1radeoff beLween slze of memory saved (funcLlon body ) and
Llme saved (funcLlon call)
lnllne luncLlons
W 8equesL Lo Lhe compller Lo lnserL Lhe funcLlon
body ln Lhe program where lL ls called
Saves Llme ln funcLlon calls

lunc1()

lunc2()

lunc2()

lunc1()

lunc2()

lunc1()

maln()
maln()
8epeaLed code
placed lnllne
8epeaLed code
placed ln
funcLlons
lnllne luncLlons
W 8equesL Lo Lhe compller Lo lnserL Lhe funcLlon
body ln Lhe program where lL ls called
Saves Llme ln funcLlon calls
W Cnly sulLable for very small funcLlons
W MalnLalns organlzaLlon and clarlLy
W MusL be glven before maln!
W AlmosL slmllar Lo macros
8emember #deflne??
lnllne luncLlons
n1ne 11oat 1bstokg{11oat pounds)
{return .4S2S*pounds,)
vod man{)
{11oat 1bs,
cout << "1ease enter weght n ounds ",
cn >> 1bs,
cout << "your weght n kgs s " <<
1bstokg{1bs) << end1,
)
lnllnefunc
eyword
MusL be
deflned before
maln

uefaulL ArgumenLs
W luncLlon call wlLh omlLLed parameLers
lf enough parameLers are noL provlded ln Lhe
funcLlon call rlghLmosL go Lo Lhelr defaulLs
uefaulL values
W Can be consLanLs global varlables or funcLlon calls
W SeL defaulLs ln funct|on prototype
nt myIuncton{ nt x = , nt y = , nt z = 2 ),
10
uefaulL ArgumenLs
W luncLlon call wlLh omlLLed parameLers
lf enough parameLers are noL provlded ln Lhe
funcLlon call rlghLmosL go Lo Lhelr defaulLs
uefaulL values
W Can be consLanLs global varlables or funcLlon calls
W SeL defaulLs ln funct|on prototype
nt myIuncton{ nt x = , Z` , Z` ),
myIuncton{2),,x=2, y and z get de1au1ts {rghtmost)
11
uefaulL ArgumenLs
W luncLlon call wlLh omlLLed parameLers
lf enough parameLers are noL provlded ln Lhe
funcLlon call rlghLmosL go Lo Lhelr defaulLs
uefaulL values
W Can be consLanLs global varlables or funcLlon calls
W SeL defaulLs ln funct|on prototype
nt myIuncton{ nt x = , nt y = , Z` ),
myIuncton{2, S),,x = 2, y = S and z gets de1au1t
LecLure ueLalls
W Cverloaded CperaLors
Cverloadlng unary CperaLors
Cverloadlng 8lnary CperaLors
uaLa Converslon
lLfalls of CperaLor Cverloadlng
LecLure ueLalls
W Cverloaded CperaLors
er|oad|ng Unary perators
Cverloadlng 8lnary CperaLors
uaLa Converslon
lLfalls of CperaLor Cverloadlng
8eadlng
W ChapLer erotor er/oodinq CC ln c++
by 8oberL Lafore
CperaLor Cverloadlng
W Clve addlLlonal meanlng Lo normal operaLors
when Lhey are used wlLh osetJefloeJ
Jototypes (eg classes)
ulsLance dlsL3 dlsL1 + dlsL2
Cverloaded #+#
W userdeflned operaLor funcLlon wlLh userdeflned daLa
Lype
W 1o overload an operaLor we declare oerotor functions
followed by Lhe oerotor siqn LhaL we wanL Lo overload
W SynLax
returnType operator sign (parameters)
,
/.../
,
CperaLor Cverloadlng
W ow does Lhe compller know whlch operaLlon
Lo perform???
1he compller check Lhe dototye of Lhe operand
lf lL ls an lnbullL daLaLype varlable Lhe defaulL op
ls performed
lf lL ls a userdeflned daLaLype Lhe acLlon speclfled
by Lhe operaLor overloadlng funcLlon ls
performed
CperaLor Cverloadlng
Cverloadlng unary CperaLors
W unary CperaLors
W 1hese operaLor can be overloaded Lo perform
any operaLlon speclfled ln Lhe code
8equlres an operaLor overloadlng funcLlon
++ !
c1ass Counter
{prvate:
unsgned nt count, ,,count
pub1c:
Counter{) {count=,) ,,constructor
unsgned nt get_count{) ,,return count
{ return count, )
vod operator ++ {) ,,ncrement {pre1x)
{++count,)
),
vod man{)
{ Counter c, c,,,de1ne and nta1ze
cout << "\nc=" << c.get_count{),,,dsp1ay
cout << "\nc=" << c.get_count{),
++c,,,ncrement c
++c,,,ncrement c
++c,,,ncrement c
cout << "\nc=" << c.get_count{),,,dsp1ay agan
cout << "\nc=" << c.get_count{) << end1,
)
Cverloaded operaLors
CperaLor Cverloadlng
luncLlon
keyword oerotor
followed by overloaded
symbol
W vod operator ++ {) ,,ncrement
{++count,)
1ells Lhe compller Lo call Lhls member funcLlon
everyLlme operaLor '++# ls encounLered
W ++c,,,ncrement c
1ells Lhe compller Lo overload '++# operaLor Lo
lncremenL once ln Lhe ob[ecL n
petottetloJ
Cverloadlng unary CperaLors
CperaLor 8eLurn values
W An Cb[ecL of a class can access all Member
funcLlons and varlables of LhaL class
W 1he calllng funcLlon cannoL access Lhe
Member funcLlons or Lhe varlables unLll a
value ls expllclLly reLurned
W So Lo use Lhe resulL of ++c1 ln Lhe maln we
musL reLurn a value from Lhe operaLor
overloadlng funcLlon
vod operator ++ {) ,,ncrement
{++count,)
W A vold funcLlon cannoL reLurn an ob[ecL
Counter operator ++ {) ,,ncrement
{++count,)
W Change Lhe reLurn Lype Lo CounLer
now lL can reLurn a CounLer Lype ob[ecL back Lo
Lhe calllng funcLlon (maln)
CperaLor 8eLurn values
Counter operator ++ {) ,,ncrement
{++count,
Counter temp,
temp.count = count,
return temp,
)...
vod man{)
{
...
++c,
c = ++c,
...
)
countpp.cpp
1emporary CounLer
Lype ob[ecL for
reLurnlng value Lo
maln
esu/t
c10
c20
c12
c22
CperaLor 8eLurn values
W AnoLher way of reLurnlng an Cb[ecL
counLpp3cpp
W 1he program ls uslng Lhe slngle sLaLemenL
return Counter{count),
Create an 0bect o1 type CounLer
t s nta1zed to the va1ue o1 the
argument [aZ`
W 8oLh approaches make copy of Lhe ob[ecL
W olnLers can access Lhe acLual ob[ecLs
c1ass Counter
{prvate:
unsgned nt count, ,,count
pub1c:
Counter{) {count=,) ,,constructor
Counter{nt c) {count=c,) ,,constructor
unsgned nt get_count{) ,,return count
{ return count, )
Counter operator ++ {) ,,ncrement {pre1x)
{++count,
return Counter{count),
)
),
vod man{)
{
Counter c, c,,,de1ne and nta1ze
cout << "\nc=" << c.get_count{),,,dsp1ay
cout << "\nc=" << c.get_count{),
++c,,,ncrement c
c = ++c,
cout << "\nc=" << c.get_count{),,,dsp1ay agan
cout << "\nc=" << c.get_count{) << end1,
)
ConsLrucLor
8eLurn Lype
Cb[ecL
counLpp3cpp
osLflx noLaLlon
lnL++ s ++lnL
W lnL ++
W ++ lnL
W ow can we dlfferenLlaLe beLween ++counL
and counL++ ??
lnbullL
operaLlons for
lnbullL daLaLype
posLflx noLaLlon
preflx noLaLlon
nt x, = ,
x = ++,
nt x, = ,
x = ++,
osLflx noLaLlon
lnL++ s ++lnL
W lnL ++
W ++ lnL
W ow can we dlfferenLlaLe beLween ++counL
and counL++ ??
lnbullL
operaLlons for
lnbullL daLaLype
posLflx noLaLlon
preflx noLaLlon
nt x, = ,
x = ++,
x 1
l 2
nt x, = ,
x = ++,
osLflx noLaLlon
lnL++ s ++lnL
W lnL ++
W ++ lnL
W ow can we dlfferenLlaLe beLween ++counL
and counL++ ??
lnbullL
operaLlons for
lnbullL daLaLype
posLflx noLaLlon
preflx noLaLlon
nt x, = ,
x = ++,
x 1
l 2
nt x, = ,
x = ++,
x 2
l 2
osLflx noLaLlon
lnL++ s ++lnL
W lnL ++
W ++ lnL
W ow can we dlfferenLlaLe beLween ++counL
and counL++ ??
lnbullL
operaLlons for
lnbullL daLaLype
posLflx noLaLlon
preflx noLaLlon
osLflx noLaLlon
W 1wo separaLe declaraLors
Counter operator ++ {) ,,pre1x
Counter operator ++ {nt),,post1x
W lnL ls not on orqument here buL merely a
slgnal Lo dlfferenLlaLe beLween preflx and
posLflx noLaLlons
posLflxcpp
dlfferenLlaLes
beLween preflx and
posLflx
c1ass Counter
{prvate:
unsgned nt count, ,,count
pub1c:
Counter{) {count=,) ,,constructor
Counter{nt c) {count=c,) ,,constructor
unsgned nt get_count{) ,,return count
{return count, )
Counter operator ++ {) ,,pre1x
{return Counter{++count),)
Counter operator ++ {nt) ,,post1x
{return Counter{count++),)
),
Cb[ecL
overloadlng
funcLlons
vod man{)
{
Counter c, c,,,de1ne and nta1ze
cout << "\nc=" << c.get_count{),,,dsp1ay
cout << "\nc=" << c.get_count{),
++c,
c = ++c,
cout << "\nc=" << c.get_count{),,,dsp1ay agan
cout << "\nc=" << c.get_count{),
c = c++,
cout << "\nc=" << c.get_count{),,,dsp1ay agan
cout << "\nc=" << c.get_count{) << end1 << end1,
)
,,ncrement c
,,ncrement c by pre1x and return
va1ue to c
,,ncrement c by post1x and return
va1ue to c
vod man{)
{
Counter c, c,,,de1ne and nta1ze
cout << "\nc=" << c.get_count{),,,dsp1ay
cout << "\nc=" << c.get_count{),
++c,
c = ++c,
cout << "\nc=" << c.get_count{),,,dsp1ay agan
cout << "\nc=" << c.get_count{),
c = c++,
cout << "\nc=" << c.get_count{),,,dsp1ay agan
cout << "\nc=" << c.get_count{) << end1 << end1,
)
,,ncrement c
,,ncrement c by pre1x and return
va1ue to c
,,ncrement c by post1x and return
va1ue to c
LND 1DA# LLC1UkL

You might also like