You are on page 1of 16

THE ORIGIN OF C++

In 1979, a young engineer at Bell (now AT&T) Labs, Bjarne Stroustrup, started to
eperi!ent wit" etensions to # to !a$e it a better tool %or i!ple!enting large&s'ale
proje'ts( In t"ose days, an a)erage proje't 'onsisted o% tens o% t"ousands o% lines o% 'ode
(L*#)(
In 19+,, se)eral !odi%i'ations and etensions "ad already been !ade to # wit" 'lasses(
In t"at year, t"e na!e -#..- was 'oined( /)er sin'e t"en, t"e .. su%%i "as be'o!e a
synony! %or obje't&orientation( (Bjarne Stroustrup 'ould "a)e !ade a %ortune only by
registering .. as a trade!ar$) It was also in t"at year t"at #.. was %irst used outside
AT&T Labs( T"e nu!ber o% users was doubling e)ery %ew !ont"s && and so was t"e
nu!ber o% 'o!pilers and etensions to t"e language(
Between 19+0 and 19+9, #.. underwent a !ajor re%or!( 1rote'ted !e!bers, prote'ted
in"eritan'e, te!plates, and a so!ew"at 'ontro)ersial %eature 'alled !ultiple in"eritan'e
were added to t"e language( It was 'lear t"at #.. needed to be'o!e standardi2ed(
OBJECT-ORIENTED PROGRAMMING
BASIC INPUT/OUTPUT FUNCTIONS
CIN>>: It is used %or input li$e s'an%( ) in #(3443 T"is is 'alled insertors(
COUT>>: It is used %or output li$e print%( ) in #(3443 T"is is 'alled eertors(
KEYWORDS
CLASS
A #lass is way to bind t"e data and its asso'iated %un'tions toget"er( It allows t"e data
and %un'tions to be "idden, i% ne'essary, %ro! eternal use( 5"en de%ining a 'lass, we are
'reating a new abstra't data type t"at 'an be treated li$e any ot"er built&in data type( T"e
'lass de'laration des'ribes t"e type and s'ope o% its !e!bers( T"e general %or! o%
de'laration is as %ollows6
Synt!:
C"## $"##%n&'
(
P)*+t':
+)*,"' -'$")t*.n#/
01n$t*.n -'$")t*.n#/
21,"*$:
+)*,"' -'$")t*.n/
01n$t*.n -'$")t*.n/
3/
T"e 'lass de'laration is si!ilar to stru't de'laration( T"e body o% a 'lassis en'losed
wit"in bra'es and ter!inated by a se!i'olon( T"e 'lass body 'ontains t"e de'laration o%
)ariables and %un'tions( T"ese %un'tions and )ariables are 'olle'ti)ely $nown as 'lass
!e!bers(
ASSESSORS
PRI4ATE: T"e #lass !e!bers t"at "a)e been de'lared as pri)ate 'an be a''essed only
wit"in t"e 'lass(
PUBLIC: T"e #lass !e!bers t"at "a)e been de'lared as publi' 'an be a''essed outside
t"e 'lass also(
PROTECTED : li$e pri)ate e'ept t"at deri)ed 'lasses "a)e a''ess to t"e! too( Try not
to use t"is & it 'an "elp per%or!an'e but it brea$s en'apsulation(
E!&2"':
5*n$"1-'6*.#t)'&78>
C"## *t'&
( *nt n1&,')/ // 2)*+t' ,y -'01"t
0".t $.#t/
21,"*$:
+.*- 9't-t :*nt ; 0".t ,</
+.*- 21t-t:+.*-<
(
$.1t66=n1&,'):=66n1&,')66=>n=/
$.1t66=$.#t:=66$.#t66=>n=/
3
3/
+.*- *t'&::9't-t:*nt ; 0".t ,<
(
n1&,') ?/
$.#t?,/
3
*nt &*n: <
(
*t'& !/
$.1t 66=>n O,@'$t !=66=>n=/
!79't-t:ABB; CDD7DE<
!721t-t: </
*t'& y/
$.1t66=>n O,@'$t y=66=>n=/
y79't-t:CBB;AFG7HB</
y721t-t: </
)'t1)n B/
3
OBJECT
*bje'ts are t"e basi' run ti!e entity in an obje't&oriented syste!( T"ey !ay represent a
person, a pla'e, a ban$ a''ount or any t"at t"e progra! "as to "andle( T"ey !ay also
represent user de%ined data su'" as )e'tors, ti!e, and lists(
5"en a progra! is ee'uted, t"e obje't intera'ted by sending !essages to one anot"er(
E!&2"':
C"## '&2
(
C8) n&'/
0".t 9'/
21,"*$:
+.*- 9't-t:+.*-<
(
$.1t66=ENTER NAME:=/
$*n>>n&'/
$.1t66=ENTER AGE:=/
$*n>>9'/
3
+.*- 21t-t: +.*-<
(
$.1t66=NAME:=66n&'/
$.1t66=AGE:=669'/
3
3/
*nt &*n: <
(
'&2 '/
'79't-t: </
'721t-t: </
)'t1)n B/
3
CONSTRUCTORS
A 'onstru'tor is used to initiali2e an obje't( A de%ault 'onstru'tor is one t"at 'an be
in)o$ed wit"out any argu!ents( I% t"ere is no user&de'lared 'onstru'tor %or a 'lass, and i%
t"e 'lass does not 'ontain const or re%eren'e data !e!bers, t"e i!ple!entation
i!pli'itly de'lares a de%ault 'onstru'tor %or it(
5*n$"1-'6*.#t)'&78>
C"## *nt'9')
(
*nt &;n/
P1,"*$:
*nt'9'):*nt; *nt< //$.n#t)1$t.) -'$")'-
4.*- -*#2"y: <
(
$.1t66=&?=66&66=>n=/
$.1t66=n?=66n66=>n=/
3
3/
*nt'9')::*nt'9'):*nt !; *nt y< //$.n#t)1$t.) -'0*n'-
(
&?!/
n?y/
3
Int &*n: <
(
*nt'9') *ntA:B;ABB</
*nt'9') *ntC?*nt'9'):CG;FG</
$.1t66=>n OBJECTA=66=>n=/
*ntA7-*#2"y: </
$.1t66=>n OBJECTC=66=>n=/
*ntC7-*#2"y: </
)'t1)n B/
3
T"e progra!!er did not de'lare a 'onstru'tor in 'lass C && an i!pli'it de%ault 'onstru'tor
was de'lared and de%ined by t"e i!ple!entation in order to 'reate an instan'e o% 'lass C.
CONST MEMBERS
T"e )alue o% a 'onst data !e!ber 'an7t be '"anged( A 'onst !e!ber %un'tion 'an7t
'"ange t"e data o% t"e 'lass(
int day() 'onst 8 return d9:
doesn7t !odi%y state o% t"e 'lass(
STATIC MEMBERS
A stati' !e!ber is part o% a 'lass, but not part o% an obje't o% a 'lass, so t"ere7s only one
instan'e o% t"e! "owe)er !any obje'ts o% t"at 'lass are 'reated( T"is is use%ul i%, %or
instan'e, you want to $eep a 'ount o% "ow !any obje'ts are 'reated(
A stati' !e!ber %un'tion
1( 'an a''ess pri)ate data
;( is in s'ope o% 'lass
,( but 'an7t be in)o$ed on an obje't & obje'tna!e(stati'!e!ber%un'tion() 'an7t a''ess any
obje't&spe'i<' data(
DEFAULT CONSTRUCTOR:
T"is 'onstru'tor "as no argu!ents in it( =e%ault #onstru'tor is also 'alled as no
argument constructor(
>or ea!ple6
class Exforsys
{
private:
int a,b;
public:
Exforsys();
...
};
Exforsys :: Exforsys()
{
a=0;
b=0;
}
COPY CONSTRUCTOR:
T"is 'onstru'tor ta$es one argu!ent( Also 'alled one argu!ent 'onstru'tor( T"e !ain use
o% 'opy 'onstru'tor is to initiali2e t"e obje'ts w"ile in 'reation, also used to 'opy an
obje't( T"e 'opy 'onstru'tor allows t"e progra!!er to 'reate a new obje't %ro! an
eisting one by initiali2ation(
>or ea!ple to in)o$e a 'opy 'onstru'tor t"e progra!!er writes6
Exforsys e3(e);
or
Exforsys e3=e;
Bot" t"e abo)e %or!ats 'an be sued to in)o$e a 'opy 'onstru'tor(
>or /a!ple6
!inclu"e #iostrea$.%&
class Exforsys()
{
private:
int a;
public:
Exforsys()
{ }
Exforsys(int ')
{
a=';
}
Exforsys(Exforsys( e)
{
a=e.a;
cout##) Exa$ple of Copy
Constructor);
}
voi" result()
{
cout## a;
}
};
voi" $ain()
{
Exforsys e*(+0);
Exforsys e3(e*);
cout## ,-ne3=);e3.result();
}
DESTRUCTORS
=estru'tors are also spe'ial !e!ber %un'tions used in #.. progra!!ing language(
=estru'tors "a)e t"e opposite %un'tion o% a 'onstru'tor( T"e !ain use o% destru'tors is to
release dyna!i' allo'ated !e!ory( =estru'tors are used to %ree !e!ory, release
resour'es and to per%or! ot"er 'lean up( =estru'tors are auto!ati'ally na!ed w"en an
obje't is destroyed( Li$e 'onstru'tors, destru'tors also ta$e t"e sa!e na!e as t"at o% t"e
'lass na!e(
G'n')" Synt! .0 D'#t)1$t.)#
. classna$e();
5*n$"1-'6*.#t)'&78>
C"## $"##%n&'
(
*nt ;,/
P1,"*$:
$"##%n&':*nt !;*nt y</ //$.n#t)1$t.)#
I$"##%n&': </ //-'#t)1$t.) 01n$t*.n
+.*- #8.J: </ //01n$t*.n -'$")'
3/
$"##%n&'::$"##%n&':*nt !;*nt y<
(
?!/
,?y/
3
+.*- $"##%n&'::#8.J: <
(
$.1t66=>n ?=6666=,?=66,/
3
$"##%n&'::I$"##%n&': <
(
$.1t66=>n D'#t)1$t*n9 t8' .,@'$t:=/
3
*nt &*n: <
(
$"##%n&' .,@:H;K</
.,@7#8.J: </
)'t1)n B/
3
CONSTRUCTORS AND DESTRUCTORS SHOULD BE MINIMAL
5"en you are designing a 'lass, re!e!ber t"at it !ig"t ser)e as a base %or ot"er
sub'lasses( It 'an also be used as a !e!ber obje't in a larger 'lass( As opposed to
ordinary !e!ber %un'tions, w"i'" 'an be o)erridden or si!ply not 'alled, t"e base 'lass
'onstru'tor and destru'tor are auto!ati'ally in)o$ed( It is not a good idea to %or'e users
o% a deri)ed and e!bedded obje't to pay %or w"at t"ey do not need, but are %or'ed to
a''ept( In ot"er words, 'onstru'tors and destru'tors s"ould 'ontain not"ing but t"e
!ini!al %un'tionality needed to 'onstru't an obje't and destroy it(
FRIEND FUNCTIONS
At ti!es you will want to grant t"is le)el o% a''ess not to an entire 'lass, but only to one
or two %un'tions o% t"at 'lass( ?ou 'an do t"is by de'laring t"e !e!ber %un'tions o% t"e
ot"er 'lass to be %riends, rat"er t"an de'laring t"e entire 'lass to be a %riend( In %a't, you
'an de'lare any %un'tion, w"et"er or not it is a !e!ber %un'tion o% anot"er 'lass, to be a
%riend %un'tion(
=e'lare a %un'tion to be a %riend by using t"e $eyword frien" and t"en t"e %ull
spe'i%i'ation o% t"e %un'tion( =e'laring a %un'tion to be a %riend does not gi)e t"e %riend
%un'tion a''ess to your t%is pointer, but it does pro)ide %ull a''ess to all pri)ate and
prote'ted !e!ber data and %un'tions(
INLINE FUNCTION
Inline %un'tions are %un'tions w"ere t"e 'all is !ade to inline %un'tions( T"e a'tual 'ode
t"en gets pla'ed in t"e 'alling progra!(
N''- .0 In"*n' F1n$t*.n:
@or!ally, a %un'tion 'all trans%ers t"e 'ontrol %ro! t"e 'alling progra! to t"e %un'tion
and a%ter t"e ee'ution o% t"e progra! returns t"e 'ontrol ba'$ to t"e 'alling progra!
a%ter t"e %un'tion 'all( T"ese 'on'epts o% %un'tion sa)ed progra! spa'e and !e!ory
spa'e are used be'ause t"e %un'tion is stored only in one pla'e and is only ee'uted w"en
it is 'alled( T"is 'on'ept o% %un'tion ee'ution !ay be ti!e 'onsu!ing sin'e t"e registers
and ot"er pro'esses !ust be sa)ed be%ore t"e %un'tion gets 'alled(
T"e etra ti!e needed and t"e pro'ess o% sa)ing is )alid %or larger %un'tions( I% t"e
%un'tion is s"ort, t"e progra!!er !ay wis" to pla'e t"e 'ode o% t"e %un'tion in t"e
'alling progra! in order %or it to be ee'uted( T"is type o% %un'tion is best "andled by t"e
inline %un'tion( In t"is situation, t"e progra!!er !ay be wondering Aw"y not write t"e
s"ort 'ode repeatedly inside t"e progra! w"ere)er needed instead o% going %or inline
%un'tionB3 Alt"oug" t"is 'ould a''o!plis" t"e tas$, t"e proble! lies in t"e loss o% 'larity
o% t"e progra!( I% t"e progra!!er repeats t"e sa!e 'ode !any ti!es, t"ere will be a loss
o% 'larity in t"e progra!( T"e alternati)e approa'" is to allow inline %un'tions to a'"ie)e
t"e sa!e purpose, wit" t"e 'on'ept o% %un'tions(
E!&2"':
T"e 'on'ept o% inline %un'tions6
5*n$"1-' 6*.#t)'&78>
*nt '!0.)#y#:*nt</
+.*- &*n: <
(
*nt !/
$.1t 66 L>n Ent') t8' In21t 4"1': =/
$*n>>!/
$.1t66=>n T8' O1t21t *#: L 66 '!0.)#y#:!</
3
*n"*n' *nt '!0.)#y#:*nt !A<
(
)'t1)n GM!A/
3
T"e output o% t"e abo)e progra! is6
Ent') t8' In21t 4"1': AB
T8' O1t21t *#: GB
PURE 4IRTUAL FUNCTION:
1ure Cirtual >un'tion is a Cirtual %un'tion wit" no body(
Deneral Synta o% 1ure Cirtual >un'tion ta$es t"e %or!6
$"## $"##n&' //T8*# -'n.t'# t8' ,#' $"## .0 C++ +*)t1" 01n$t*.n
(
21,"*$:
+*)t1" +.*- +*)t1"01n$t*.n&':< ? B //T8*# -'n.t'# t8' 21)' +*)t1" 01n$t*.n *n
C++
3/
E!&2"':
5*n$"1-' 6*.#t)'&78>
5*n$"1-'6$.n*.78>
$"## B#' (
21,"*$:
+*)t1" +.*- 0:*nt< ? B/
3/

$"## C"##A : 21,"*$ B#' (
21,"*$:
+*)t1" +.*- 0:*nt< ($.1t 66 NC"##AN 66 'n-"/3
3/

$"## C"##C : 21,"*$ B#' (
21,"*$:
+*)t1" +.*- 0:*nt< ($.1t 66 NC"##CN 66 'n-"/3
3/

*nt &*n:<
(
B#'M 2/
C"##A $A/
C"##C $C/

2 ? O$A/
2->0:EF</

2 ? O$C/
2->0:PF</
)'t1)n B/
3
#lass1 and #lass; "a)e no deri)ation or in"eritan'e relations"ip wit" ea'" ot"er( T"ey
are independent 'lasses wit" a 'o!!on base(
ABSTRACT CLASS
A #lass 'ontaining abstra't !et"ods is 'alled an abstra't 'lass(T"e abstra't 'lass is one
t"at used %or only %or in"eritan'e i(e 'an not 'reate t"e obje't o% base 'lass( T"e abstra't
'lass is one t"at 'ontains at least one pure )irtual %un'tion(
An abstra't 'lass is a 'lass t"at is designed to be spe'i%i'ally used as a base 'lass( ?ou
de'lare a pure )irtual %un'tion by using a pure spe'i%ier (E F) in t"e de'laration o% a
)irtual !e!ber %un'tion in t"e 'lass de'laration( It is a design 'on'ept in progra!
de)elop!ent and pro)ides a base upon w"i'" ot"er 'lasses !ay be built(
I% in a design, we want t"e base 'lass to present only an inter%a'e %or its deri)ed
'lasses( T"is is a''o!plis"ed by !a$ing t"at 'lass abstra't using t"e Aabstra't3 $eyword(
T"e %ollowing is an ea!ple o% an abstra't 'lass6 A #lass
$"## AB
(
21,"*$:
+*)t1" +.*- 0:< ? B/
3/
ENCAPSULATION
T"e pa'$aging o% data )alues and !e!ber %un'tions wit"in one obje't is 'alled an
en'apsulation( It is also 'alled data "iding w"i'" "elps to !aintain t"e integrity o% t"e
obje't( It sa)es t"e data %ro! !isuse and outside inter%eren'e( T"e data 'annot be
a''essed dire'tly but a''ess 'ontrols 'an be spe'i%ied in order to obtain t"e in%or!ation(
T"e data or obje't 'an be !ade publi' or pri)ate depending on t"e needs( T"e data w"i'"
is pri)ate is not a''essible outside t"e s'ope o% t"e obje't( 5"en t"e data is publi' it 'an
be a''essed by t"e ot"er parts o% t"e progra!(
5*n$"1-'6*.#t)'&>
$"## )'$tn9"'
( 2)*+t':
-.1,"' "'n9t8/
21,"*$:
-.1,"' ,)'-t8/
-.1,"' )':<
(
)'t1)n:"'n9t8M,)'-t8</
3
-.1,"' "'n:<
(
)'t1)n:"'n9t8</
3
)'$tn9"':-.1,"' "'n98tA;-.1,"' ,)'-t8A<
(
"'n9t8?"'n98tA/
,)'-t8?,)'-t8A/
3
3/
*nt &*n:<
(
)'$tn9"' )A:E7G;P7H</
-.1,"' ?)A7"'n:</
-.1,"' ,?)A7,)'-t8/
$.1t 66 NT8' "'n98t *# : N 66 66 'n-"/
$.1t 66 NT8' ,)'-t8 *# : N 66 , 66 'n-"/
$.1t 66 NT8' )' *# : N 66 )A7)':< 66 'n-"/
)'t1)n:B</
3

BINDING
Binding re%ers to t"e pro'ess t"at is used to 'on)ert identi%iers (su'" as )ariable and
%un'tion na!es) into !a'"ine language addresses( Alt"oug" binding is used %or bot"
)ariables and %un'tions(
E)"y ,*n-*n9
Gost o% t"e %un'tion 'alls t"e 'o!piler en'ounters will be dire't %un'tion 'alls( A dire't
%un'tion 'all is a state!ent t"at dire'tly 'alls a %un'tion( >or ea!ple
5*n$"1-' 6*.#t)'&>

+.*- P)*nt4"1':*nt n4"1'<
(
#t-::$.1t 66 n4"1'/
3

*nt &*n:<
(
P)*nt4"1':G</ // T8*# *# -*)'$t 01n$t*.n $""
)'t1)n B/
3
=ire't %un'tion 'alls 'an be resol)ed using a pro'ess $nown as early binding( E)"y
,*n-*n9 (also 'alled stati' binding) !eans t"e 'o!piler is able to dire'tly asso'iate t"e
identi%ier na!e (su'" as a %un'tion or )ariable na!e) wit" a !a'"ine address( He!e!ber
t"at all %un'tions "a)e a uniIue !a'"ine address( So w"en t"e 'o!piler en'ounters a
%un'tion 'all, it repla'es t"e %un'tion 'all wit" a !a'"ine language instru'tion t"at tells
t"e #1J to ju!p to t"e address o% t"e %un'tion(
LetKs ta$e a loo$ at a si!ple 'al'ulator progra! t"at uses early binding6
5*n$"1-' 6*.#t)'&>

*nt A--:*nt nQ; *nt nY<
(
)'t1)n nQ + nY/
3

*nt S1,t)$t:*nt nQ; *nt nY<
(
)'t1)n nQ - nY/
3

*nt M1"t*2"y:*nt nQ; *nt nY<
(
)'t1)n nQ M nY/
3

*nt &*n:<
(
*nt nQ/
$.1t 66 NEnt') n1&,'): N/
$*n >> nQ/
*nt nY/
$.1t 66 NEnt') n.t8') n1&,'): N/
$*n >> nY/

*nt nO2')t*.n/
-.
(
$.1t 66 NEnt') n .2')t*.n :B?--; A?#1,t)$t; C?&1"t*2"y<: N/
$*n >> nO2')t*.n/
3 J8*"' :nO2')t*.n 6 B RR nO2')t*.n > C</

*nt nR'#1"t ? B/
#J*t$8 :nO2')t*.n<
(
$#' B: nR'#1"t ? A--:nQ; nY</ ,)'S/
$#' A: nR'#1"t ? S1,t)$t:nQ; nY</ ,)'S/
$#' C: nR'#1"t ? M1"t*2"y:nQ; nY</ ,)'S/
3

$.1t 66 NT8' n#J') *#: N 66 nR'#1"t 66 'n-"/

)'t1)n B/
3
Be'ause Add(), Subtra't(), and Gultiply() are all dire't %un'tion 'alls, t"e 'o!piler will
use early binding to resol)e t"e Add(), Subtra't(), and Gultiply() %un'tion 'alls( T"e
'o!piler will repla'e t"e Add() %un'tion 'all wit" an instru'tion t"at tells t"e #1J to
ju!p to t"e address o% t"e Add() %un'tion( T"e sa!e "olds true %or %or Subtra't() and
Gultiply()(
Lt' B*n-*n9
In so!e progra!s, it is not possible to $now w"i'" %un'tion will be 'alled until runti!e
(w"en t"e progra! is run)( T"is is $nown as "t' ,*n-*n9 (or dyna!i' binding)( In #..,
one way to get late binding is to use %un'tion pointers( To re)iew %un'tion pointers
brie%ly, a %un'tion pointer is a type o% pointer t"at points to a %un'tion instead o% a
)ariable( T"e %un'tion t"at a %un'tion pointer points to 'an be 'alled by using t"e %un'tion
'all operator (()) on t"e pointer(
>or ea!ple, t"e %ollowing 'ode 'alls t"e Add() %un'tion6
*nt A--:*nt nQ; *nt nY<
(
)'t1)n nQ + nY/
3

*nt &*n:<
(
// C)'t' 01n$t*.n 2.*nt') n- &S' *t 2.*nt t. t8' A-- 01n$t*.n
*nt :M2F$n<:*nt; *nt< ? A--/
$.1t 66 2F$n:G; E< 66 'n-"/ // -- G + E

)'t1)n B/
3
#alling a %un'tion )ia a %un'tion pointer is also $nown as an indire't %un'tion 'all( T"e
%ollowing 'al'ulator progra! is %un'tionally identi'al to t"e 'al'ulator ea!ple abo)e,
e'ept it uses a %un'tion pointer instead o% a dire't %un'tion 'all6
5*n$"1-' 6*.#t)'&>

*nt A--:*nt nQ; *nt nY<
(
)'t1)n nQ + nY/
3

*nt S1,t)$t:*nt nQ; *nt nY<
(
)'t1)n nQ - nY/
3

*nt M1"t*2"y:*nt nQ; *nt nY<
(
)'t1)n nQ M nY/
3

*nt &*n:<
(
*nt nQ/
$.1t 66 NEnt') n1&,'): N/
$*n >> nQ/

*nt nY/
$.1t 66 NEnt') n.t8') n1&,'): N/
$*n >> nY/

*nt nO2')t*.n/
-.
(
$.1t 66 NEnt') n .2')t*.n :B?--; A?#1,t)$t; C?&1"t*2"y<: N/
$*n >> nO2')t*.n/
3 J8*"' :nO2')t*.n 6 B RR nO2')t*.n > C</

// C)'t' 01n$t*.n 2.*nt') n&'- 2F$n :y'#; t8' #ynt! *# 19"y<
*nt :M2F$n<:*nt; *nt</

// S't 2F$n t. 2.*nt t. t8' 01n$t*.n t8' 1#') $8.#'
#J*t$8 :nO2')t*.n<
(
$#' B: 2F$n ? A--/ ,)'S/
$#' A: 2F$n ? S1,t)$t/ ,)'S/
$#' C: 2F$n ? M1"t*2"y/ ,)'S/
3

// C"" t8' 01n$t*.n t8t 2F$n *# 2.*nt*n9 t. J*t8 nQ n- nY # 2)&'t')#
$.1t 66 NT8' n#J') *#: N 66 2F$n:nQ; nY< 66 'n-"/

)'t1)n B/
3
In t"is ea!ple, instead o% 'alling t"e Add(), Subtra't(), or Gultiply() %un'tion dire'tly,
weK)e instead set p>'n to point at t"e %un'tion we wis" to 'all( T"en we 'all t"e %un'tion
t"roug" t"e pointer( T"e 'o!piler is unable to use early binding to resol)e t"e %un'tion
'all p/cn(n0, n1) be'ause it 'an not tell w"i'" %un'tion p>'n will be pointing to at
'o!pile ti!eL
Late binding is slig"tly less e%%i'ient sin'e it in)ol)es an etra le)el o% indire'tion( 5it"
early binding, t"e 'o!piler 'an tell t"e #1J to ju!p dire'tly to t"e %un'tionKs address(
5it" late binding, t"e progra! "as to read t"e address "eld in t"e pointer and t"en ju!p
to t"at address( T"is in)ol)es one etra step, !a$ing it slig"tly slower( Mowe)er, t"e
ad)antage o% late binding is t"at it is !ore %leible t"an early binding, be'ause de'isions
about w"at %un'tion to 'all do not need to be !ade until run ti!e(

You might also like