Function_Overloading
Function_Overloading
iEducat
ionSoci
ety'
s
St
.Xav
ier
s’Engli
shHi
ghSchool&Jr.Col
lege,
Manpada,
Thane(
w)
Subject:
-ComputerSci
ence–I
Topic–C++
SubTopi c-Funct
ionOv
erl
oadi
ng
Probablemarks–41
Funct
ionOv
erl
oadi
ng
Functi
onoverl
oadi
ngi
saC++pr ogr
ammi ngfeat
uret
hatal
lowsustohavemorethanone
funct
ionhavi
ngsamenamebutdiff
erentpar
ameterl
ist
,whenIsaypar
ameterl
i
st,i
tmeans
thedataty
peandsequenceoft
heparameters.
Forexamplethepar
ametersl
i
stofafunct
ionv
oidsum (
inta,f
loatb)is(i
ntandf
loat
)whi
ch
i
sdif
ferentf
rom t
hefunct
ionv
oidsum (
fl
oata,
intb)whi
chi s(
float
,int)
.
Funct
ionov
erl
oadi
ngi
sacompi
l
e-t
imePol
ymor
phi
sm.
E.
g
#include<iostream. h>
#include<coni o.h>
voidadd( i
nta, intb);
i
ntadd( inta,intb,intc);
i
ntadd( i
nta, i
ntb, i
ntc, i
ntd);
voidmai n()
{
Inta,b,c,d,sum;
cout<<” Enter4number s”;
cin>>a>>b>>c>>d;
add( a,b);
add( a,b,c);
sum =add( a,b,c,
d);
cout<<” Sum of4number sis”<<sum;
getch();
}
voidadd( i
nta,i
ntb)
{
cout <<”Sum of2numberi s”<<a+b;
}
intadd( inta,intb,intc)
{
cout <<”Thesum of3numberi s”<<a+b+c;
return0;
}
i
ntadd( i
nta, i
ntb, i
ntc, i
ntd)
{
intsum=a+b+c+d;
r
etur
n(sum);
}
Quest
ion:
-
1.Explai
ntheconceptoff
unct
ionov
erl
oadi
ngwi
thexampl
e. (
prev
iousy
earquest
ion)
Ans:-1)Twoormor efuncti
onscanhavethesamenamebutdiff
erentpar
amet er
ssuch
functi
onsarecal
ledfuncti
onoverl
oadi
ng.C++hasmanyfeat
ures,andoneofthemost
i
mpor tantf
eat
uresisfuncti
onoverl
oadi
ng.Iti
sacodewit
hmor ethanonefuncti
onwit
h
thesamenamehav i
ngv ar
ioust
ypesofargumentl
ist
s.
2)
Over
loadi
ngmeanst
heuseofsamet
hingsf
ordi
ff
erentpur
poses,
iti
saexampl
eof
Compi
leti
mepol
ymorphi
sm.
3)usi
ngt
heconceptoffuncti
onoverl
oadi
ng,
meanscr
eat
ingaf
ami
l
yoff
unct
ionwi
th
samenmaebutdi
ff
erentargumentl
ist
.
4)Tocorr
ectf
uncti
oni
st obei
nvokedisdet
ermi
nedbychecki
ngt
henumberandt
ypeof
argument
sbutnott
hetypeoff
uncti
on.
(
Addoneexampl
eofy
ourownatt
heend)