0% found this document useful (0 votes)
106 views47 pages

Oops Assignment PDF

1. Class templates provide a way to create reusable and flexible classes that can work with multiple data types. 2. Once a class template is defined it can be instantiated with different data types to create classes, eliminating the need for separate classes for each data type. 3. Class templates allow for type safety by ensuring that the types used match the expected types.

Uploaded by

Dinesh Choudhary
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)
106 views47 pages

Oops Assignment PDF

1. Class templates provide a way to create reusable and flexible classes that can work with multiple data types. 2. Once a class template is defined it can be instantiated with different data types to create classes, eliminating the need for separate classes for each data type. 3. Class templates allow for type safety by ensuring that the types used match the expected types.

Uploaded by

Dinesh Choudhary
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/ 47

Date

Pord-A
Saatht
e tho ul ot Mu opinat.
GOLatoh_danHO3 O nequalA
The n o s

mamey Jocotiom om ha stoe. 9to


,Lbiciant mamo avaigable naus
a
Opexato9 inukial20 tha
mamoy nd nttunn
theadd a nuwly allecated ond
edmemhsto th point urndialne
viabl
aRat mimoy tto vauabla
tapainkr vaiable =_meo data-type

Allo cote a block oh memos


taxpointer-vaiable s mew_data-tspe Lsize].
n t *ptr : Mew i n t [5)

Sunciut us tha phoqrammeia


TespamAubality tv daall ccato dynamicaly
allecatad mamony uing dalate oplhatos

Sue2 uhot us momben Jnling tuncticm?


9nCtt, an unlun memben tuncton dka
tunckoni.e.dasand ukto indioda o clask
nkinatomand uu makad uth JAi unune
Raywd. whon a mambun tunctiun mankad
a Sinline? tha Compulan tiea to eplaca Call

to that funchon uth tha actal cocdsr


tho tuncticm at tho call t qath than
gengnating a tunckon call Fage No
xampleclass Myclass Saathn
public
Lnline void ptnt)
Cout " Hello
world "cendl
Tha inline
hunt to tha compulsn kuywnd uul mlya
thacommpila to and ut ud up to
a
tuntiom r mot.docdawhathon to inlins
inchoal the SI2e oinling tùnhon can_
So ining Campulid poqram
uuo_anly for Smalland tregLunt
callo.dfunction
uw3 What u o VItual Bako claua
Ara InIn Ctta uitual Baso olo that a
bale clau 1.e. untanoled
to b unhrtadL
VIDtually y multpla dorived clal203
in a clak
hwochs. uhana baueiclae
Jnhoruod VirtualyJt enaui tha
ondy onu Jnatance o thu bal claa uald
eben u applnnl in mulapls Juels o hs
cla huwnarchy.
To coatu a uttual bauo clais
uou Need tto u l tho S tual ?Kouwosd un
thabalo_cla cloclrak1on
Page No.
Date

class B tual public


Syntor A

class public VIstualA

what us laks Banding


TnCtt,lalt Bindir 9
hana to h
phocoo
calattha runtimLrath
.
reSoruinghaappophig
ul allo
tuncter.
thanai ompl
Knaun al duynamac
quntimapalymophum. bircdung c
Late Binding Jl achid
tha ulo o vtualtunctions trou
uhan a tunton
ib doolarad as
vntual un abale cla, du
markad.tm.lati Bindurg Thu mona thot
han dha tunctian d callod on a dautd
olasobject througha bae clad pountin, th
tunction call us tsalued
dynamitaainun
tumabaodom adtual type a tha abect sun
pountad
uSwhat us tha uL o anamymous Namdpacea.
Same ude calA o anaymrtu Nama SpacA
a)Gncapkulatirg_implamantatondstails
hulps to huda tha hy datatso ha Jmplkmant
Page No.
Date
Saathi
atoo otho pant of gamand atduo
he mstk nams claiius ua
b)
b)RaducLng globalnamespacepallytim
Qt Can
Lad to decdanahelpn funckom o Vanua!
that Vahg
an_my ulnd wtthin a Singln
tanalatom uniit
Puidung a pxtuado namespace tur Stadte
tadie vgnahs
Vaomabk
2 an usod to derlart statc
vantaklo& that ano loca tu a tunckioner
la alko endunLs had ul
omlu v
thun uhunt tanalation unit.

Syntaei namespaee
L/Code

uo 6 what u tha uwo Anuns funddion.


In Ctth hun a ftunctiom u mankud wnth
h a Jnina Reyw ods, tohen a mamben
tuntiom u mawked wuth thi Compulon
mea to aplaces Cal to thal tun tin
tth thg actuol codo o tha functiom
al tha Call ste athan than geninatirg
a tunctom call
Sqnta nl1ne etuan-typc tuncttan-name (parame tes)

LICode
Page No.
Date Saath
ThaPinline Ruwhdul
tha onlua
hunt to compulih and ut upto th
decdo
compilh to
whuthaa tu ununs_a
funckhom o not.unline fundtiom Can
A SI2e ot Cmpulod proqgm ince

Detuna this point


9n
an Cttthis pointn ula pointn to ha
alth2ent 6Qject dnstance and t con se uo
LacO tha mambeni and mthods o tha
CIhn objeat Iha this pointa uaudomati-
ral chatad byCompLlan bhan.a mambn funchon
Callad and tt us pabked as a huddan aaqumad
to tha tunctom
xclass Myclaos
pvak
int valuej
public
voidget-dlafalint valuel

this->value = va lue

tnt man C)

lCade
geturmO,

Page No
Date

u9. ohan ue
we stattc data
Saatht
Amg In
Cttstatic datu membumambers 2
a data mamben oaclasa On Wod t
to bshaatd ammg alU or stut ndb m

das. Unuke regula unstaces of tH


datu
datc membuu, strthe
nmAmbenk mot paxt cunduWd
object unstance o tha ckal 6ud
paat o th clau utselk ana'untlaru
Use& o static d acta -mamben
a)Count1ng mumber of nstanc
6) Shatrg data amomg InstancL

us lo. what ua VIatual tunction


Ana In Ct4, a uwtual tuncthon
ul a
tuncttoo aabase clal that Can bemember
ya drud cla.Whan a UTtual otehdd
uL calUsd om a Bae claJd fundton
pointbn or aeduan
that points to be doruued
clas progeck
the umplemantatcm o tha UIatual
hat coUAponds to thu atual danwed
functon
clas obj et us collud.
Ex
class Rase
publk
UIatualvoid print )

/ltode

Pag No.
Date
Caath
class De1vedpublicBase

public
UDLd print O

lcode

intmain)

Deatued dj
Base bd
b2pr10t)j
thunn 0j

fage No
Saath

a)Raakility
Claas templaki poudes a
Uy to Chaata at ulakle. Cods. Once a clau
tmplad ui dahunad tcan 6 tud th
dhurand data typo3 to Chaat instanc
h i clai Thu sauts ttmi and ehbot aJ
t eliminats thA ned tu Chaata sepnate
dosaes ft each data-type

)flexiblity
Claa templata oulut t r Alaxikd
In paqammig.Sinco a cla ttmplati can
beud in duferent us data typed
allaus dudopeni to oaats paoqram that
Can w k uth a
vanlty e data typas
Th paxtialonly wsthul dn s Huotion
h u the data typix a not Enamom ad ha
tm of coduns
C)yPSo-aty
clus templak1 prautdas tyee Sahcty
whuch MaoNJ ithat tho COmmpilocheck daka
type wd ith ha omplatr clas u
enn thai t matcho: the expedod typ
Tha holp to catth eros al
tim puhuch ui mone
erCompil
ehbiciont than catt hur
tbemat nuntme

Page No3Y
Date

4)StandandJuby
AAth
Most o Jhsstandaad
cloaA dn. Ctfo dmplamardhd oda
lura
d ukan
Lcmplals8,Such as condau
alsand mapaThu mak
Jika dto
towethode clasäeg tth
eay iur dautlega
typs oubnantdata
Thael ny o unstantat
tunctom
a
ttmplak
Imalicit 9natcnkatun
Implut undtardat-
Ld
erCUnb when ih
Compiln enurdunz
a call ta o tunction ttmplata. At Hhu
paint
thicompilun geninatel a n intanca o th
functim tcmplau uding tho datn tyAs
hoxqumintd pamadad. Iad unstantatn
happeni autmaticaly at Compul tma. Ond
th oesLuling tuntn s ad fa tuncton call.
Ex tcmplate< typenameI
Iadd(Ta,Tb)
getuan atb)

9 we cab addC121',thu compln und


LMpAicikly unstanktab tha tunction temalatu
Lth ini)a olota.type and geNAalu atunckn
that adds tur Jugu

fageNe3S.
Saathi
2)txplicu e9natantatiom
expuctyinstantiatt a tunctim tmplat
tor a partiaulan data-typeThu don L
th template keywohd follmutd by h
tunckiom sanotuns and he idatatyee in
anql 6xoc kots.
E
tmplatefloat add<t loat>(tioat a yfloat

Ths Jalths Compulan to genaNb a


tun chonthit odda tu float ualues

3) ExplicttSpecuaunaton
t u d when
wand to prourels a specule umplamuntation
oh a templas tunchen t r a pastheculan

data tyee. Thil done b4 psCtdng a

specalaed utn&lom tmplada functhon


ttr tha datu type dn qution

for exampla ,wt can pratds a specLalized


t h add' funchion templad t
uclcn c
shing' al tolau
e
8 femplate

sthins add (Strrng a, shn b)

e tun + 4b
36
Date Saathi)
What u a
templak Explain wth thu halp
2Explain

example haw to Cheata a fun o


an
and acla templake. damplad
Templat
Ans AA a fatun thad Templat a
allerud t genDlpIogTammi A
elentully a Supint ttmplad
f Cheak1ng aty
n A h u c k i o n i

ha tuncton
a t Cao be
udd knth dublgruns daka
ypes Ths elumunatu
thanud to wd
enats tunckim& or clauu fr eath
typa and impxaUt Codaatulabi y.
Templatus ana ued ta Chaats
uc claslatunctiam and daku shucturo
thal caa. be s k uth cdhbnunt doto
yp
Thins ona tun t y r ttmplalu inc4
aFuncttonTemplate:
b)class Templat

xample at tunctiom Templatt


Syntor
templatKtypename T
T functtan NameLT paamekri,T para mett

lcode

3
Date
Saathi
Ex
Hincludeioshcom)
uSinq mamespace stdj
ttmplate<typename T7

Todd CInumlInuml

Tetum num1+ numlj.

LnE maind

Cout & Sum of Int type paxameler (2,3):


add (3) Klendlj
Cout "Sum at 1 loat typepaaameftrl2.243.3):
oddl 2.2,3.3) < endlË

etum O).

Oukput Sum ot int type paaa mt trr (2,3 5


5.5
Sum oi tleat type pasameer (2.2,3.3)

example at cla Templal

emplat <class I
class classname

p Vate
Page No. 3
Date

I varj

Dublic
TfunctianiNa meLTury)

Example

tt ncludekiostatam
LSLA naMespace Std
template <closs T
class Myclass

piuatt
Inuml,num2 j
public
MyclagsO
Myclass(T numlI num)
thisnuml: numl
this-mum2: numl

I addU

Tetun numltnum2

35
SaathD
..ntmain1

Muclass &int2 unt-aby (23);


Caut 2_+3 Kint-obj-addCkend
Mycla sseflaal) float_oly(2.2,3.3
endi
cOut 22+3.3 tlaat-ol.addlkcerd
nd

2 +35
2.243.3: 5.5

u1Hau mult1etel inhanutanct d dushene


tam multpla tnhuutance 2 what ambigut
anud Jn mulk1pl unhiutana and h u
asoltdl
Multipk unhunutance athsns to th
Camcepl ons odai extendungCo inhuut
mo than ome bahe clasa h n multhleud
InhAtance tAswtuna cnsan inÁend
hama dunuurd cla,thanus mak1ry thu
diurd class the bae clau fo Jhu new
clas
Multpl inhuutonu
Baeclass Bae class 2

eye N QQ
e)Ived Cous
Drte

Multloeel inhuutunca

HBase class of y
Subc lassl dey veg class ct
Base eless of 2
Z deatusd clas.at Y

Multpl 9nhinta nta not wndalu


Ad eaaluit makia th Sy sttm ma
Com but Multlu 9nhutanki udal
uad.
Mulhe inhirutuNCL ha tun claisus
Lul namaly basa cdau and duud cla
Multll inhinutant hal thht luuts namaly
haoclaM Intenmacakk clau and derued
cla

Ambiguuty augeA_in multila nherutanc


hamast obuguprablam t h
ulkplo Jihurutanca occuu cduuns
functuon autudun
Suppate tu baue claius hut a
Scmu funckion wthuch ul nt autuudden 4n
denuxd clals.
you fry to caluth tunckon using
tha object on the doud clasl ,conple
.Shaud eHNON. U us becaul Complin dotsn
Date Saath
Kmcw which funckom to call

tttncude KLostream
USLng mamespace Std
class Basel

publc
vaLd Shou)

Cautk s Rase class-1calledend

class Base2

public:
VOId Shou)

Cout°Rase class -2 calledkcerl

class Deiued publr Basel,public Bae2

public
llCode

n t manl-

Paye No.2
Date athi
Dea1ue d d
d.show0, biguutya
2tun 0

Rexalpe Ambiguuty
Thu Ambyuutyca
sourd i n y thuS
SCope aesaluton tunction

specLgyhich funchonto cdau lhas


Baselc Rase2

tntmain

Dexiued di
d Basel:i
shaw);
c .Base 2Show ( ;
aetu mj-

Outpuk
Base class-cali4d
Rase cla sS -2 called

Gunlowhat uobjeet aaentsd prog1amming


How i t u dupginnnd trem procesdune oventsd
Patogaammin? Explain.

Page No 2
Saathiy
Object-omentcd parpaamminy(oOP)
oop da prognammMng
paxndym that atsduelanaund thacCanta
a obiectiuhuch_ane dnstonced c l a
that encapsulad data and behaulcu
In oaP0 clalua lupInt
ttmalat that dskinti thaphopentuu
and mthorela d an_objeck.Propnai ane
ha dlata mimbao vanalblai that str
thidata an obyect and mithcels ana
th tunckin puaduns thad opera
n that data
Obiect con pBenct wath each_
athea bu Sencdung mageswtich ana requo
t r a particular behavLan oh achn
Thu untthactonu baled m thpr1NLpla
en.capaulation,unhinita nu andpolyma
philm.
inherutance uhuch maku ths 00p
Cacla moi moeluulan Raln touldn
tuld a relak1onshp betuten claiue
Cncapauilakon can hds Sora oha piual
datas ok a.das from.othan olyecti uhuls:
Lpulymomphiam Con oullw dlnula a Cornmon
opAnaton un duskrund unya
00P udaly uuad un softwat erginai
lecaus J praudu a pcwtrhul and flextba
y mcdel compliz systmy
Page No.uy
ath
Date

DIfferang blu OoP and Pocsdunal psojiamnaun

cdunal@auented Pang hect axntrd png-


1) pncaduholprogan-19nobject or1entcd prcg
mmingthe prngomltha_progzam d durdad
lclutdsd unto small into Smal pads call-
paats cold tuntims abjects

2 PaocadurolprngaamiOop falLnua
to-Uatusa top- daTum bottom -up apprCaCh
appraach

Thue noaccSpect- ha aceA specdL


H u In Pop Lke pxtvaula Dubuc
protects.d ef.

data and|Addurg nw dok and


u Addung mew
tunctni s moteay tunction u eaiy.

5) POP dotlnot hauy any OOP prauudsa data


propln uway oh huoire higuirg So,ut m
datu So ut ul Jas Secu
Sec

9n PoP, oudoding 9n oop oUUUoa du


mot poa1ta pOssibla

19n PoP,t N no 9n ooP, Concepi ot clota


Date Saathy

anc inhmutance id
Jnhant
Cancapt ok clata huduryhdiny and unha

9nPoP thefuncton 9n_0aP data udm


s m o important tmpatant than fune
than data
funch
S POP bada om OaP baled em thu
Pha unal wrld awonld

layPoPul ulad ftor duat DOP U od fr du loni


-nig mdum S12dLange and Complex_
prDam pngaam3

1lPOP l uaad H OoP uA0A h


Concopt
Cmcp prosdun oodata abshachon
abstracton

1 Coda ocus ability Coda ne tLaablity ptsend


absent in prDCLdunaIn ooOP
rUum mIN

13&xample:
13 CfoRTRAN,|Examplk*ctt Java
Pascal Basts etc. Pythen

PageNo. U6
Saathi
Dote

fage N

You might also like