Function Overloading Karnataka State Board
Function Overloading Karnataka State Board
hrtpter H
bjects:
int main()
{
int i}
char c;
1--====-= function_name(c);
~""==~=---="""""""'== function_narne( i);
...,_,,,,,,,,~,,,,,,_,,=-=~==--.,,-fun c-tic n _name ( ) ;
..... ..... . ,
}
. ov
f' unction
208 erlc¾
8.1 Introduction
· I t • W'·' J , ve learnt about classes and ob·~eets
In t lw previous c wp er, .: 1 c1 • 1
· · · h d t • b"'1·s and mem bet functions is lh
H c1 m< m .. · , : .
·
c Prinl1
J
~•pJ>ltcf1tton ol cl.,sses wit
k,H m c of oh,1cct oriented pt ogramming _to develop a.t:-d ~rmplify prograrnrn.~
18
methods. Another feature to help for eas1_er programming polymorphi~rn. Tl
<lcfi11i11011 of polvmo1 phism has appea1 ed 1n chapter 6._ Let us recall the definitic
otH'<' agmn. Polymorphism re-fcrs to "one ~ame h~ving. m~y forms, differe
behavior of nn instance of an object depending on s1tuat10ns .
C++ implements polymorphism through function overloading and opera,
ov('rloading. Thf' function overloading allows the user to create new absl~·
data types. In this chapter we learn about the need for function overloaclin
definition m1d dcdaration of function overloading and some examples. T
discussion is limited to design of a set of functions that perform essentially
sam<· thing, but with a different argument list. The selection of overloaded functi
fi<·p<'ncls on matching arguments at the time of compilation. The study in t1i
du1ptcr is also extended to inline and friend functions.
8.2 Need for function overloading
Fu net ion overloading means two or more functions have same name, b...
dilkr in the numb~r of arguments or data type of arguments. Therefore it is »a1
that (function nwn<') is overloaded. Function overloading therefore is the proce
ol cldininr, sa1nc function name lo carry out similar types of activities with rnriou
data items. The rnlvm1tages of function overloading arc:
, Wtwu clifl<-rcnl functions are created for different operations, then us~
lws Io c,111 rt•<.;J><'ciivt· fu11c1ion dcpcnding on the situation. Instead, fo
diff<·n·nt !-litwllions if the same runction is called with different argmnen'
using fund i~m ovctloct_ding, 1h<'n the compiler automatically decides abi,.'
IJH' Hppropnatc 1.unct10n by comprning the argument types used in th,
c,tlJ to lhc fu11cl10n ,md calls lhe required funcrion. Thus the cod!' '
exec u I (•d f: ,ski'.
•
lacto, 111 lu1wt1011 OV( t lo , uli 11g1~.\ ,,. t ltst ~.
.
'J }H n1w11
• 1.u11t·tion~.trguni~ 11 1
, mi <1rnl111g1wil1 ovc1lowlt•d l1t11<·t1on ., h, tit. f ·g u1.1(1 ts., £1
< tHllH l ll'l' ,u1d q pe o HI 1
tlJ• ,c ;11e tw,, 11111, 11011•. lwv11u; N,u 11 ,. 11 lrll<' , 11 1<101 , 1• · t 101,uufnt•,u.111
11 er<"nttvpcso Uc,
dill<i11il111111du1ol.111•,1111h•11(1 1li,nliti1<lt 0
. . I . . · l 11 ,,mDfll,.
11 o\ ('1 ond111g ts 111vnk<'< .it 1rll'
I I V I I 1• • ( I ' I 11 I ' l I ( I • I 11 11 ( • I 1() 11 ( ) " (' I I , ) \ ({ 1 11 /!. 1 ' • 1I :-::o known .,s (' on l J' tl ,~ l
I
pofyu101 p I ll'·Hlt ,
I){)
fanctions
ll!Jre>21",amming example shows how overloaded functions can
functions
~ember functions are compact function calls.
e size of the object code is considerably reduced .
execution of a program increases.
;t code can be .generated.
·ty of the program increas~s. •
e(S);
<"Square of 5 • "<<x<<endl;
(10);
<"Square of 10 = "<<, <<cndl;
O;
Fu net ion over1o .
22~12_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ ____ d(1l
r
SquHre ol 5 • 25
Squa 1 e of 10 = 100 . !!9'£M..tt ~ . .•
- - - - - ~ - - - - - ~llill!5~.~--.,·:() ·8 •111 .inlinc functwn that hnds the sq~
In the abuV<" t")xamp 1<" sqH<lt < h ' t'
,..}
of a number. ,. (
,. , 1nay not wot·1·\ Horne, times
Note: The inline funct10n · for one of the followinu '
I
I".)
reasons:
, The in.line function definition is too long or too complicated.
,. The inli1w function is rent rsivc.
,. The inlinc function has looping constructs t~~~(
r The inline function has a switch ~r goto. l,e
8.5.2 friend functions ~,r10,
\\' e have seen that private and protected members of a class cannot be \tiflO\
accessed from outside the class in which they are declared. In other words non•
member function does not have access to the private data members of a class. ·•.mtm
But there could be a situation where two classes must share a common function. •·,~nr ·
C-r-r allm.vs the common function to be shared between the 1.wo classes by making ~n·
11
the common function as a friend to both the classes, thereby allowing the function 1
to haYe access to the private data of both of these classes.
A friend function is a non-mem her function that is a friend of a class. The
friend functi?n is declared_within a class with the prefix friend. But it should be ~I.~
defined out~1de the class like ~ normal function without the prefix friend. It can
access public data members like non-member functions.
Syntax: class class_name
public:
fr~end void functionl(void ); · ..
,. fne nd ieturntypt,_spe cifer function_name(arguments):
j,
friend function cannot be called using the object of that class. It can be
oked like any normal function.
ey are normal external functions that are given special access privileges.
cannot access the member variables directly and has to use an object
e.membername (Here, is a membership operator).
e function is declared with keyword friend. But while defining friend
ction it does not use either keyword friend or : : operator.
elude <iostream.h>
ssmyclass
private:
int a,b;
public:
void set_val(int i, int j)_;_ •
friend int add(myclass obJ);
id myclass::set_val(int i,intj)
add(myclass obj)
return (obj.a+obj.b);
214
int main()
Su rn of 34 ::lnd 56 is 90
e bO\ e proonun
~ . add() is a friend
seg1nent the function
Su1C'e it is not a rnember of any class, 1t cannot be calle
ce 1t is a non-men1ber function, add() should be _declar
er public or private or protected access specifier.
remember