0% found this document useful (0 votes)
12 views25 pages

Paper4WS

The document discusses the advantages of inline functions over macros in programming, emphasizing safety, functionality in classes, and improved readability. It also addresses the concept of dangling pointers in C++, explaining their risks and providing code examples for memory management. Additionally, it covers object creation and destruction, detailing constructors and destructors in C++ classes.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views25 pages

Paper4WS

The document discusses the advantages of inline functions over macros in programming, emphasizing safety, functionality in classes, and improved readability. It also addresses the concept of dangling pointers in C++, explaining their risks and providing code examples for memory management. Additionally, it covers object creation and destruction, detailing constructors and destructors in C++ classes.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

Rollne :- 223423

ol. ) Jsg
&iteble
Hous

exam ple:
inline Runcton
s]
is betez han Macros oh
Ans - In object- osien ted Peogaromirg . inllne kunction aže
bette than macno Kunctiorn for thee olooig neoton
1. ee Salety
Macaos do not check dota types, ohich com legd to
CEEOES
Jnline tunchton eeloarn tye checking at Conpik 4ne
-
#
Ekarnple

+ includeiosteom>
define Squaze Cx) Cx)
ioline
sing
int sqLanse
narnes pcLQe stdy
Gntx)pctuen x*x ;] n atine du

Cout < "Maono: <e Sqyuase (s.5) ehl unerperled


Cout Inine: squote Cs.s)x n ) ty pe-ste

2. funetonai y n casses
Macroos carnot be used inade closles effectivey
Jnline tuncton con be use d in closses t
sptimize
peeRoemane
EKarnpl e -
dass Mathf
Ablte !
Inline Int squaxe Gind ){ ||inline unelso
inside cass.
3. Re adabi Hy ond Mointaina bilHy
Macroos use
use tert eelacement, oaking debggng
hoede
fenctos Irgpzove reacbiHy nd Bloo
Jnline

G0P panalples

Relezences :
t.Baagurasamy Pg 80.
ciget.
Year. Ap Q093
[Roll no 22348]
|Paper 4]
3b. Wr1te a proqram tn Ctt.to awodb oanglng
ponter sttuation. (4m)
that
Danglng pointer : dn C langunge, ponde
dlallocated earlier in the pre qam is cal
iht Dargling ointer.
COmmon occwene
H Danglng p8nter is c
Ccs uhen ue lse the delete!
hat geneally duas pieviouily alocated
ihat
te dealecate memoy
ponte that was ponting do thad
and the Jhoe
kÐ dhe Jame aclads
also thor caes uhich lead to prter
are
eng dangeng poirder
" Deallscaton sf memey ung delete o
"Rehrencing ihe local vaiaie of the functon
alles is executed
Vauablt gees out of scope

'hy hana baga pouter’ If a progtam attemts


Je ncces macuty mey hrougk a cdang"
frtoa, st can boad o unpiedictau aehai o
alhes cnd data coruption as &h

method! e handlung pontos


yay depandng On thc
Coeated. Belo the st ceohch ivc
danglng pentes.
to the pointes that we
NOLL OR nollptr
not u USe.

Code
# tnclude iosteam>
sing nanmespace std
int main()
l lmemo rg allocahon
int*value- new intli)
D ireeing it
delete value )

Vass igning null


vallue nullptr;
checking nl befor e accessing value
Cout <x "Menory deallocated 'Kend l:
else
Coutxr val '"<< Value Ks endl;
retun o;
3. /otput ’ Memosy u deallocated.
LlUsve DYNAmic MeMoRy ALLlo CATION FOR LOCA L
VARIABLES hat are to be teturned

The memory allo cated using new or malloc th


C+t will not be deallocated til we do +
man valty using deleie os Fce(). we can use thi
type of mensy
thad e wani to etuN te ihe calle furc'
Code
7ctt proqram tT illustrate how to reforn dynamfca
created fnls (ocal vaçable
# indude <iostream>
USing n amespace std;
getpoiater ()
JAllo cate on the heap
nt tocal va new int (4o2)
retwn local var;

cnt man ()
/getting the pointer lo the warialsle
Cout<< value: !
<*ptr<r end t;
/manvalls dealo
delete p~r ; caho g tb
)setioq pr to rule
p NLLL )
return 0;
3.
OUT POT ’ 9
Vaue. y2
Reference ’ Dangling poiltes n Ctt -qeks forfeeks
geeYs for geeks.o9g
22352
Roll 9o:22352 Apn
Year: 2023

K'apen-4)

4.c the
Explain concept o object cAeation and
destction in Ctt

DObjects aae instance of a casS or can also be


hrown as Vabigble of type (class-nome) of the
class
IR the class doesot have a constocto, the
ceaed object is mesely a Vasiable hat hs to
be initialized seperatey
)we Hence,fon automatic
of
0se a initializahion
constocor
ob,ect,

9 Constboctor sa special memben


fonction hauing
Same name as class ame
and is automahally
invohed uwhen cbjec ass ociated with the
dass
-Properies o onstctor
D Aloays declared in poblic sechon
i) Doesnt have a ehusn type, not even
) InNoked autonatically on object
void

coeathon.

B) Sntax for Constoctor:

class inteqen
poblic:

integer (vord); /Consuctor


declaration

inteqer :: inteqer (void) /Conslbcton


denation.
G) Aer, okiect eretion, some deS)tces ate
prouided t0 the cbiect in fosmm oP nemory
Space.
DIn onder to monage These memony tesatces by
Fiteng them atfertne vse of the
okec we need destooctonS.
gven

8) A destoUdor is aspecialmenben tonchion,


USed to destroy the cseated objects which

) go

class
ousde
A desbuctor
tne

hame dnd le
program Scope.
also has same name as the
ls patceded by a tilde
siqn.
-Properhes o Destoctor:
Salyys declated in poblic sechion
i) Does not have any arqumests
n) Does not haue a tetntype, not euen Void
iwy Destroys the objects in teierse erden of
Cheation once autside the
program scope
10) Syntax for Destrucor:
class Tnteger nClass definaton
poblo:
/Data Members
Memben torctionS

w]hteqer (0 VDestouctor declarahion

Destoucton body

Referance: E Bolaqurusomy oject Otiented Proramming oith c")


Pa:(44,145, 4G,162) - Chapien: &
Roll no: 22352

You might also like