0 ratings0% found this document useful (0 votes) 34 views14 pagesOops 03
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content,
claim it here.
Available Formats
Download as PDF or read online on Scribd
95 /\ /2023
Th Fundion oveuriding., derived class pxovices
specific implumentadion Fox a function thar s
Sa ol@eady, dikined in _panunt class » Twis allows
wna derived’ ass te override the behaviour of
ru function inhaitd fw parunt clo» -
ode= | Rindudecbits|stdesh> |
wing, mMormespace Std:
class amimal § .
public +
void Speakt)¢
cout ¢ “ speaking" « endl;
oF
Clans dog 1 public animal t -
of
publi ‘
void speak) §
coutc" barking "Zeemdl:
y.
int maint) ¢
onima) a;
os speak:
5
dom dy
detpukU;
af
zam}
4
é
Tb parent class “Ra pointer child
r abject par ouote hrte hai.
eur child ();
nila clom Ra pointer _parmt
DLLLULLEbeeueddddad
b
peck par crate hate hai.
BOEELEEE
h
Luit, pornter- type pr depend rego. Rit
i function Ro accon karna,
By ¢
data mumber tho. memba,
| has
Bur, fF wre womt Phat obyect-type pr depend
kre, then male te parunt clay Nivtued.
SSMUSBBEBEB EDS!(Note AQa isi code mai parent class ko vistual bra
denge them obyect- type pr depend Rrego Aother
Mhom pointer Hy pe . In that case, pales
ourpur= speaking, barking , barking Speakeirey
Heinctucle chits |stde t+-h A
wing. Nownes pace Std ;
}
_ clams animol ¢
__public:
void speak e c
__ Gout co's peateing eéendty
yO
zy, ;
Cary deg : public _amimel f
Publics
void speak ( )
couk << “barking "<< endl;
3,
int maint) £
Omimal * ad: = Wau animal (); _
ad ~> speakl);
dog * di = nou dog W);
Ad speak),
omimal ¥ a2 = neu dog t),;
a2 > speak);
dog Faz = @og*) nun animal ( ): speaking
42> speak) barking.
speakin
teahingdeclared in the base
idelem bu “the dlenived clas -
Kohen ore m objec of dexived clas —
JMsiAg co _ | pointer_of- base class, then. ved
class “inmplenaemtotion sh tnak Funuion is_invoked +
la function ia made viatual to achiewe Aut =
[time polynne rsphisnn« ee =
Every clay Hat ures wuual Ginctions has its
foun (vivtual table. Te ts a lookup table used te
wesolve Fanction calls,
Vioruat pointer iso pointer, aoe points + the
foble db function pointers fe. Viable. is a |
hidden deta member to atl those ctais cahidh has
viewed fundien
Base class> Bind vToble
- Wintual eat )4———p eat) |
> Ftyg=———_F Fg) |
[| Beivedelass: sporrous__—Dvived class : Cagle |
pet] [eet :
{ Table oo vToble _) -
— |_ eax 1 =
AsAinclude cbits|stdehs
US 0
mnowmes pace Std5
“|class bird g ; a
tau) Niue
Cour ce" bind is casing Neca;
Void Pye
cour c “bird is Biying" candy
3, 5
—_|elass_spansour | public_bind £
| Upublics
noid eat) &
tam age
wuk ce" sparveoin 1s easing," ceed!
void Ty tit oi
scout ce" spaioun 1s Figing sez endl;
B35
Public:
votd eat OE
__couk ce“ eagle tis’ eating." endl;
a 3
void fry re
ws “eomte is Frying, “ceed 5Lint main ()¥
|
Migectel
|
|
~ bind ™ bind| = neu spasurow () 5 7 _
bind! — eat); __
binds Fly o);
bind * bind2 = nun eagle O);
bind2—> catl ); 7 =
biad2 > Fly,
basay
u :
tal I
pasrour_is eating.
a (9
ao aale is eacting
= bind f Fiying
a
=
=
= Constnutkons Camnot be mode virtual » hen
> Char, is _execed, there is mo vTable. in the
73 ibasemno y+ .
a2 Also, constauctons have ‘Some mame as clon
i nome: TF we make constructors Virtual in
- hase Clan _, tk means thy ode ne- dened ow
a» overt iw_chi bur we ! ONS So,
a2 __|noume fog two Clay
I-Destuuctons com be made Vistual, One of the
IsMearon is VvTable i emt tn mm ak
tne Hine dh ewAueien cain : ae Es
1Bu4 asin one clam “dertyu ctor Vijluol, y
last emaures Hat denived Clos dleststictors
axe abso calied abide prenents memory leakage»
Othenmise if hase clans destauttor is not virtual
Hnein, base clam dutructoy if cabled tahide
midst lea to_murnony, Veak
So, alwoye make bare clo, Autrey a3
lvigawal . : bn) bw
Code 5 | teinclucke cbits| stdctt-h
WIN nomerpace Std,
¢ nae.
Class AL
Pwolie:
AL
Cour ce“ ckose AMLc endls : : =
via “ALL :
Cource dtos A" wondls:2 ,
~
ined /
i -
3 leis epublica (me ae emai ET
ay |. ___publie: ee -
iS Oey oe es et
P coukccScton @ ec emdl, ein =
r= eet aoa
iS coux cNdton BV eeendly eo
= 4 oe Pe
FS fink wang STEN abet shai
i A * oby| = newt 85 : “ _
A> | ddlete obyl; J
=
Se — es
= Ss
1, XA _ 1
om Uror B __ _
_ akon @ a
dio nA
SOBSESSEEGEUL:
TE bore class destauctoh “tos nol made. Natal _
Haun ourput will be - Clon A —
aston A
Tn that cose, derived clans ka destructor cath
nahi hota.
eds
wONS~
=
3 | _/
=
bess i
= _
= _ _ _ Ba <—:
[Showing only esentia) informeadion : \idi h
a Seat “ things = a
—=— — — — —— —
Be |Abstnaction ts the _supee-s sek of the othe
== ion, nheritonte.
tamed) _polijnoxphiata
thee pillars of 00PS ives emeapsulod
=
—
7
ay =
io
=
-
la
>
eval Rime
An Foncate base “clan _\s_a class thet= comnot _
be instamtiated ive: absinact class Ka obyect a
lel abstract clans if it Contain’,
&
Jotleast one pune virtue) finction. _
[pered clarses that inherits fa0m abStuact bare.
clas mut provide implementation fox its _
_ | obstiacr functions ov methods. _
a Is a funuion that must be ewiden,_ Ina
> derived clam oMenwise that derived class will
L> abyo become on abstraack clay + ee
[>
SS$b
|
vidual Noid func ( ) =include < bits |Stdett ho
a wring. nomeapace Std 5
Clogs bis Y
publics
Winitual- void eatl) =O;
virtual void Fig) 05
y-
clans sparviow ? public bind
PAivate:
oid gate
tout <<"spamous eats "cundl,
void fy OE
Cout ¢< “sparrows Pin" eeundl;
clams: Piqnons public bind f
peivotes elorees '
Noid eat LE ne
Coutéc “ Pigeon. eaks"ecemdls
noid ty) &
cource Spigeon Ties" ce end);
int_moinl £
bind Kbirdl= hau pigeon \);
bind eatl)} :
|
bind * bind 2 = “neris sparrow) + i | Pigeon eats
een a [spinous fligg =
}Code S| teinclude cbits(stdeda he bail Tt _
| wsing, Namespace Std y
| access private membeu of class B then it
fsdend keyword is used te ayamt a class
On function, access te He pitivette amd
protected members of
Lamothur class _
Le 2 cwamt that a clay A cam be able to
Cam be clone using faiend Rejword:»
publics as
ACint x): x(x) £4
7 int gee C VLoonst f
_ ekwin x 5
—_ Fuiend clowns B- [=
fiend void paring Const A &)-[elo BE
public; ea
|___weld _ paint Const A doby)X
cour ce obs -x ceendly
vi
|
|
|
a
|
|
ei
St —
poe
uv
|
_|_veid paint Const A ob )e at
cour ce oby- x ecendl; —
u
vos
|
= int mainl )t _ :
C A obit (oe); s
i @ oby2; tt no :
S| ob y2.- print (obs); [calling cla
a
ie print Cobs1))
lo
lo
SOVSEEOEESEEE EELSYes, Constructors com be made private bus
oe com's creake any} instance of thot clos
cuhose Conatnuckow is made private
clas aL
ink x5
A Cint x Lh
ens) cn amen heen
Tn the obove code, constourtor is private amd
ths code will not give ony compilasion erunye »
Ainclude cbits |stde +4-h>
Using. amespace stds,
int x;
int 45 eee
ACint x, int 4s x) yy) t
meal
| a
clant AL : ; =
:
public :
int geex OE
sarkwin % 5
ink gex7C) €
satu Ys
Friend class 8; by
rr___pwvlic: _ = —
| A Fane Cink x, int ye
serwnin A Cx, 4);
un : :
\_\ i
tsla'y ena |
a
i
fine man) t_ —
_|Tn the above code
| 8 eby ty —
A oby2 = obs|: func (1,2)
Cou obj: gtx Dee" “ce ob 2: git ( Ie enndls
clams A Ra const nuctos.
parlvare inal «So, fiend claw brake claw A
ae private memnbers ko accu ka skte hal.
Nha clay 8 hai friend ts chan A+
So, clas 6 ka imstomce _crrate kik:
class A ka instanu, Creare ka shie hot +
| Tatine Punekions
Taline functons ars optimisation Rarune Just od
mates: In inline function, the compiler places
a copy ds the code of that function at each
vha_ Se
point wher, fincHon is called ak compile time,
crekumtype Make the fun ch on
intine | fune ©) €| inline only if the code
I code [etonion is only of
y one or two Vines-
cs
cs
cs
if
BZ
3
IN
Aga tmnoion tnline h to colt hone pe Call stack m entra
ani bnegi wother thon vo ek Une ka code. tha se
[eodk aaigh bh whe copy, he Jayega. ob compilasion fume: