0% found this document useful (0 votes)
3 views

Oops Assignment

The document discusses various concepts related to object-oriented programming, including virtual constructors, destructors, and the importance of using virtual functions for polymorphism. It explains the implications of using base class pointers with derived class objects, exception handling in C++, and the differences between function overloading and member function overloading. Additionally, it highlights the advantages and disadvantages of exception handling, as well as the significance of anonymous objects.

Uploaded by

palnhip835
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Oops Assignment

The document discusses various concepts related to object-oriented programming, including virtual constructors, destructors, and the importance of using virtual functions for polymorphism. It explains the implications of using base class pointers with derived class objects, exception handling in C++, and the differences between function overloading and member function overloading. Additionally, it highlights the advantages and disadvantages of exception handling, as well as the significance of anonymous objects.

Uploaded by

palnhip835
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

2) Eopl an vintual construtto,

AN a Destouttor wth Guampe.


vÙstuel onstructor
The viul mecheiSm tu0KS Ony when we have a
boge uoS poites to a deoived Class Objet
n ctt, the constvtor cannot be'vitul beLauto
when a constvtoo enelUted there
is no vintual trble in the memoay means no vitua
pointer defined yet So the iconstauCAor is pessible.
Shauid aduaye be non- viotul.
BUt vistua destuttor is posscble
inclde <iostoemm

CAas Ba

public :
vigtUCl Basec)

void Shpwl )

CoUt << show the boge wass ObjeCt

CUys deoived i public bose.


pubtic:
DeoiVed)

Covt 2<tDeoived Coorted end A3


VoLd showl)

CoUt< lshow the deaived claS objec "2<erdli


int main C)

BaSe pt;
Derived d;
pt=&d;
ptr show ():
vintua
This pruogram wll QONON because
It troies to cou a viotuol constovcto
vintua destouto
we can not dolet a dooived Class cbjt Using a
bose CAas poiter that has a non-votual destrvctoo.
To deLOk the deoiied Cass bbjeck Using the base
base,n Desthvctiny
deoivedt Destning
lenvdDconstoutiny
constutiny
bge p-
bpto delet
v¹d deri bptr= b
d); new demved= d* de
(vod ) Mun nt
denved; Detovcling Covt
e<
d)
doived
; eonstruting t Covt
publiC:
pubiicb dUass
baseDestrutiny <<U Ut
b)vintuau
base";eonstauctingr tl( CoUt
ho b)
public:
lass
b
namocpaue
stdj Using
<iostenmy include
ri viotual contain
a must Cins bage the poite
3) Emplain chjeck siicing
tohen ademive dCaaiS objet is Signmed to,a bayo
in ctt) tho deaived ClasS objects entoa
CAass objeck
the bate
Qttebvtes ase sicod off to gnerto fomed abip.
this whol e paoe1s 's
Class objp t and
eiicig
’ The base claS obect Can' aeee33 Only the base
’ This aUso
implies he repartion 0f bage CLas nembegs
Foom doved Cos mem bers.
’ 1nert:derivedUos obet can be signed to a basp
lass obect but the other wey S`not possible. To
tackie this siing obem we can Use adynamic
pointer
pubtic :
int i
Uas deoved public base.

pUbtjc:
intK.

int mointy

deoived d j
b= dj
4) Erpiain krormous ohjet.
Ans ’Ah Ob10c4 is an in an instantR of a uas
’ uss has n0 memgy tulocatod it is JUst ike ablveint,
but when tsmst n objeet is (aeded memoy s
Culoc otead just iKe the eal - wotd instone Of tha
blvepaint
’ An objeet that does not have a mOfarente vaocbte is
nonymoUS bbjpt These aMe the type
0f objets Cocated For, jnstane use oniy
pooperties
’we n coeatO in objeet wthout aname sUch tpes
Of nameless objeet are caUied anonymous objet
’ The pojmcnyy puapose op the noymous bbjek is Just
Fos. înston t use
’ Hn AnongmouS Dbject Can be passed an aogument
to a unti on:
’ These aoe Sed to duee memony conSUmption

eloss nanelpaometoas );
inuvde <iostreany
vsing namespaLe std

int 0yb)
pubtic;

(out << DeFault onstoudor neuted


úrn (int a int b1)

CoUt " panametooised tonstnutto PnetUtpd ;


b b!,
CoUt <e l DestoUltor eoedutod;

int mein()

5) Enplain ose f pue vis tuau


Funion.
’ n a pure VistUau untion, the deived cout woUd
not cou the baye Uos Funtion os ik has not
derined instead it cals the dooived Funtion which
implemertt that Sone puae viatual Funtjon in the
deve d u
’F there is any puoe vituau FUndtion in au
o uass,
then it beeoMes au bstot Uas
we con doClas it bUt we
car't implement it
’ o mUst be
3Signed to the Fun fon to make them
pure
’ These rUntion auso have a poein
befose the fundiont
vintuau
’ The maun USe OF pune vistual Funltion is to
Coect on cbstat class thot deFines an intesfee
Por is demved Clags
’ An bstoat cos8 1S IKe a boge Class Fos otho
cajses to poovide a common 1ntenFace to tmplement
’ This heips us the poymonphism TootUre of the
pangra ming langunge
U`ing namag peu e Std ;
Uass Base

int X}
publ ie :
viatval void Funt) -0;
int get x(){ netvm X;

Uass degjved i publÍC Base

int y
pUblic :
voíd FunO S cout <<.Fno cued ?

at int meun (void)e

1) Dajvedd
dFunC),

O/p ’ Func) coalled


Q) Copiotnenception hadtiog wih camptes
Sceptionn cne
e bromal conditions hot
enLOUHess duming its neution The pott ess Of hordlingy
these eneepti on is cal ed eieption harndling vsing
tho enception hdliny melhansm the contol Faom
onepot Of the whene the Mception cUraed
Can be toons Fored to roher pant of the code
’ An ecepti on is un unen petecd problem thut ateses
duniny tho eneeutton of a progrem bUm 3progem
.

teominatos suidenty with somo errs /lssue


+ es ception beeurs duming the quning Df: the pogam.
It is Of tuo type
) Syrcmorous i upoctions hd happen
whem
Something qocs anong becaUe of omisfaro in-the
input dota
9) ASynerorous Exception that ae beyprd he
progoun contools , Such as disc
inteaupt# eke.
Fallve s Keybcar
ett pnovides n inbuilt Featuyo or
t con be done using the neption hndling.
Folbwing Keyons-
The ty Keyuod epnesents a blcck oF Code bat ia
plaLed inside tho toyy bloK. t is Foltliued by one ory
Cateh ol cCKS. IF Cun eneeption ocUdS, toy bldcK
thaows that encepton.

"The cateh statoment meSent a bloK OF Code thet


is Qneeutod when a paoticut crs eception is thaoh
Faom the troy bloK, The cade to.bordle. ho enception
is twriten ingide the tatth blocK
9)
An exeeptlon in ctt can bo thnown Using the
thaow Keyvosd cUhen a poog nam, onountens a thoow
Statement then it jmmeiately terminates the
tuent Funetion nd staots Findiog 'a matthi n
caten bl oK to hale the thown ecep ion.
thcay' some eeptionype(emor mesa_e ")

need of eneeption handling


’ Seponcthion of roo handling Cade Foom nome tode
’ Function/Methods Lan hre onty the enceptions they
Uhoose
’ Uooping OF erro ypes
Proertier
1) ASpecial tatth blox coued 'Cath- n" bl oeK
witten
Cs catuhl---) hat Can beUsed to (oh oUA tpe
oe. eneption
2) tmplicf type tonvens ion does not
3) 1F an eNception is thapwn tcnd happen For prmitive tyre
n0t Caugnt aonywhere
the pgram temincues abnomaut
Gump
# inuudeLio storemy
Using namspale stds
iot' mant)

ty
int numemtor lo;
int derominctor 0}

iF (denominator =0)

.thooua untime enroY(DIMsion by Zno not


res numa rotor /denominatorr
Cout << ResUt ate division
""<res k ends
eaten (const eeepttond e)

op- eeptioy pivision oy ze0 not uowed


Advarta_es
) ROMOVe enor handling code Fom the Softcrg main
tine o code
a An eNeeption that OceUs in a Function con be
handled anywhere inthe Function:caul StacCK.
3) Seporating erSOT herdl ing cade Foom regutar Code .
4) poopagatiny
Disacivota_es
yeIP eMLeption handling is not done property con lecd

a) 1t is hand to tearn how to wite eneption code that


is Safe
3) Thein is no C+t stardasd on how to useCheeption
hordtig 2 hente many vaiction in eeption hordlin
poatice e i t
) Dirfogentiote bet
Fuoction over loading &
Mermbex Function ovealoading wh etamptei
Funcion ovor toading
wheneveN
Funton wih Sarme name differont types
OP panometes cied RUr' overtoading
’ Overload Funtios are in Same Seope,t
’* FuNdion ha the obtilty to locd multpe imeg
tnhemtante
In fun oves loing we do not need
Compite. time overleodin.
incvde <iot neem)
Using nmespaue Stdj
void add Cint ay intb

CoUt L sum =Latb) ;

Vod add (double a,doublo b)

add (lo, 2) ;
add (s.3, 6"2)3

Yp - SUm =12

Members Funtion overtOOding


It nvo1Ve3 Function
definod uwmjn a Uas.
OUSO Known cs membes Function .
MeAbe |t is Sim u to FUntion ovos
mem be Fun. An haVO the loadinq
Same name bt must
have dferent paoanetex is4 .
’This enoles to perafom diffroent opention
bosed on the anguments passod to tho Unction .
’ 1t is a powoTUl Forturp Fon Crcting
moe
intutfive and pooubte cAoss dessng.

You might also like