yooPs) Ans C4
i
bStamesbmata aim: of) 00? = laste? bind.» kogether th
120
dnd= athe functiong that» operate on ae =
mo other part of he code Can access this dato
mlignexcept this -Rinetion » s c
Class + Tk 18; a user defined) data type» Which» ho\dg
Sts own data members and member functiong:
con be accessed and used by creating an Anstance
of -nat claga.
|
Object : When a class is defined no memory is allocated
but when % i8 “nstontiated Ci-e,object is created) |
memo iB allocated,
t
Encapsulation : In oP , Encapsulation iS defined as bind!
togetner the data and the functions that “mans pulates Mt
¢
Abstraction: Abstraction meana display ing
information and hiding —he detail.
sAbswaction using claskeg
-Avstaaction usith, Header files Cmatn-y = powd)
Only essenrial
ag
Polymorphism + —h Simple: Words We candace poly
mee. tthe ability of a meSsage +p be display ed
mere than one form. 7
+ Function —ovevtoadi
+ Operator overtoadi
"
G
tne tum C10) 26 , 39)
‘mt gum Co, 20)or,
Thheritance + “the Ga pabiitny oF a-elass +o. “der SUSE
properti eB and charocterlaticz
iS catteg Inheritance,
« SubClass
+) Superctass ta79
io Reusabtiity, a oe
q
Dyra mite Binding > =H dynamic binding , ne code to
be execure: Rn wesponse 40 fihction® ean te >
decided nartbo myrun ct ean
rom another claas
Conptructons : A conStructor i8 a member function of
a class Which inittalizes objects ofivah, class% Hq Crt
Conssuctor i automatically catied When the objeck
createa -
TR has *6ame name a8 Class Rrseft .oig¢Hiuhs .&
Consrsuctor don't have as setum +ype-
ae, Defaule Constsuctoy CNo poramexer Passed)
Re Parometrized Consvsuctos
3. Copy Gonsteuctd¥ Inter? /nsidoNen Sse
q .
Desteuctos in Crt * Derived claay desteuctor Will
joe Unvoked Firat, then -the bose clasz destsicto¥
will be “invoked, és
Acceaa Modifier! fublic —)can be accessed’ ccbynany class
Private !~. Can be accessed bry iby 11a fanetiod ig= a om
Provected = TX ia also Anaccessibie outside the
Clase Einaccestble oourside -theiClasa) 5
Class but. can be saccessed . ley Qub clogs ot
thar class » io eis 1 adeany ess — Medi fiey
defau\t the a
will be pxive
Notes rf We do wot specify
Qnaide the class ther! by
modifier fox —-the member
Grenticie ch wuAmetmend class” can aceees
and protected members of other closs in wh
fr is declared as friend. a
Ex friend class 8;
2 mhe vitrance,
Class Subclass 2 acessmodé ~baseclas
=
t
a. Single mheritance
v
2. Multiple Fnhevtrance
[gat none
es
4+ Weerarchical nhertrance s. Hybrid
LA Combination 2 F
IBIAS Or more, tViPeiey
iz aay 3
:
i j
Polymorphism
2 Operator Overloading
=> Compile time Poly 23 Function Serena
> Run ttme Poy
Lb Funetion “overtdin
Oceusg When a-
Class has Q_ definition of
one or more
member2 of base cagAsvantagex of Data Abstraction 2 a a
« Avoid) code duplication “and Fne. Beusavility. é
» CAN Change nresnal @mplementation of cies
andepeniendiy . Reianviad snail
q
Srucuye Vs closs + Most Important difference
%S Secussity. a
A ersuctube %a mor Secure and cannot Shite
eB Membes function and Variable While class
WB Secuse and can hide %# poe
eae detcfilz. 3
QV
0
local Claszez “in C++ + A class declared “inside @
Punction becomes local to that Runetion ond 8 exiled
\eca\ loss
All. the mernodg of local class must be détined *
Rnside the ctass only
Virtual Funetion and Runtime Poly morphism +
A Virtual function ig a _membérr Puncion “which
16 declared Within a base ‘claag and redefined
(@everriden) by derived clage {
Functions are dectared with Viwtua\ Keyword 17
brov
bage Clase
Exceptton Hon alin caviar { q
ey 2 represer a block of code -that dan Wheew
Qn exception » ion apn
Carey + repvecant a block of cose mihat Ger executed
When error \B) throw, (oi-o) smi
Sow! Use to “HYOW On exception:} Caters)
Tiere (2 QQ Specta\)-carth block 7? = §
At catchez all types et error:
+ Taline Function
= Inline iB a fequest mot command , Yd i
TF 12 function shar {3 expanded An \fneé When tt ig
called. When +ne “inline fnction ts called , whole
Code get “nserted or Substitvted at the pote ort
function’ call,
‘ane vewan-type fund)
ER tb Sey
+
* Function Overloading fe a feature Yn
*wo or more finttions can hove
same
At feren+ porameterz .
areal a }
Void prink (tat 2)
L Gout
Petnt ¢ 142) . sapaeeaecias
5 33S ae
= tS Cae
aguw © Svupportg Procedural er
blw
Cand HHS
@
nf
ansd rate Sioa
ig Krown as hybrid
Crt
i vel Nanqvage , because st So
! bot Proceaura\ and object
iz oriented — preg Yamming .
8 ABC ‘does not support tne [© c+ nas RUpbort Corll poly aor
ooks Concept so % basi no “phism, Encapsulation and
_ Support for polymorphism , Anhefitance as % ig Qn
[ encapsuleton and Sn herttancer|> onfs tanquageny sitet =
ZB CiB a subset of CHY eh Copley Stperser of Cc
3A @ contains 32 keywords + Or+ Contain’) S2 keywords
Covbric, paivate, Rrovected |
o apy) “Barely > ANVOw a)
“5, € ig a finction driven Ciao «Grr tevan Se i
yh v | aaa <
& Function and eperator overloading) + Crt” supperts/ Shute Z
is mot Support nc. opertttor oN er \odi Z
F: © deez not support exception [+ C++ daesdar =a 7%
) gens =H exception rept ust
| ay and etchq Collection (of dissimilor alee
+ Serve in
+ Static «Members in Ct*
eStatic. variable tn a function : When O variable
declared aS. Static , space for ‘Sir ene a\iocated for
Wfekime of othe -progrom - (defaut farialized oe 0)
Even © 4ne fonction i@ caed muitiple times,
Space. for Tt 18 allocated once.
« Sratic variable in @ class .
— Declared ‘inside the class bedy 5
— Also” Known a2 class. member. varidble -
> The “must be defined outside tne class. a
| > Static “voriobe deesn'+ belong to any object , bur
| ane whole Class , N ug
i one: -
DP TMhere will ben only n copy of Static member variable
| for ne Whole claaa . >
Exe class Account
ft private :
int balance;
Stave fre Tot ;
fi
Publics pe
Noid SetBalance Cont b)
ee i babne =b; 9
b
Wintalised ovtside doss
Float — Accoun+ +2 Yo4
= 35F ;
Void mainpbject{ Pea mab “Reser eetared cama a a
eS Stove | Account "jae 7 spay Serena
THSITHS HOD
+ Staric — tunerion in
7 x
a Qtass
Fence sH= oo aie
Static > members €ynanong 1s) are eral wed isto occe
ony ne Static data ~~ membera or
member funciona ,
eConstructorz
qo (a'yalgrvad) Hunts
= Constructore ig an Special_member. yofun chlominjiak the
Class. Tr is avtomoteay Fnveked wnen an by eck |
Ig ~ created: >
> Hr hes n°. return type: K Be
3 Constructor has Same vame_ as; Class Stseth +
5 Xf we do not spedfy , then G++ aompiler generate
| Q@ default Comp@tructor for us. 4
Sabie
as Consweror
fo.
Noi y oct
Defourt Porometerized oa
Claselnamet) © pn@lass_name (paramererg)') 9 aiess_nal
la
Update) 90 TY mvpdare Cte x, aun © of | fupes (
4 YP San
=bd=10, Wooti| seq ou
Altea i rapst269 T zombo sare sie ny VotQuyt 21
fy= 2
Compiler qeneratez — two onstrucror by nESeN =
1 4: Defadir Constructor
fe
Fee 2 Co Constructor
mm
Te 1
ig crated by vs
Bor tt any of the cons +vuctor
mor ee create b
shen “default constructor Will
__ Compiler -
Construction overloading can be done jest Mey
funerion overloading , Se
¥ rrenola
Dejav+ Compiter’s) Copy Constructor Can done only
Shallow haw
om
-—a<— 4 Cfoints +0 Same ~
a. memery leer r
_-e-@e om
op u ob) 2 oe
Deep es is Possible only win user defined cons
| Th vser ¥ defined Py gonstuctor , we make sore ant
gointera of copied object polwts to new memory \e¢
Can we maxe Copy Son structor private & Yes
Why a to Copy Constructor must be passed
os “veference 2 j
Because tf we pass valve ;-+nen Tr woua made
4to ail coy constructor which becomeg Ton—tenminat
Roane
tbs —s—]
obj t oj 2= pestrictor
Deatwetms ig
| a o> member -Qunerlon Which -destructs) or
deleter an object .
=> DesWuctos don'+
: a Seok Rar argument andy don't
if rave any veturn type
S Oniy one destructor ag
2 Desxructor cannot ba
poss ibie
Sorc.
Ey Bebo desivvetor doesn't destroy oblect 19h
ia tne last fonction hat | “nNacenmmeerare object destrey.|
5 |
‘cam ee
“Objecr — Resoure
Destructor ts used, So hat before deletion of dj
we can free space ailoarted for thia Yesoure. Ble
ib ob\ 9e3 deleted then Space Allocated for ob)
Wil be free but resource coean't,
: Operator Overloading
Cae hove Vthe ability to Provide special meaning
45 ane operotor. } “4
Class Complex 5
t oe
erp SORE mcm (Complex Lei)
4 Complex» ves 4
RZD= curd,
TER = C2b;As '' con't add ompex TOR ‘ditectt » Set
= Can detine Q. functon. with Teme e
meeda write operator Keyword ngrene
So, > weG" use @ gu cperator Ave this .
‘
ee as
Friend Class
A friend clags can awess he Private and > pros
members. of omer class tm which Tt ie declared
Friend. y °
cere Gan be friend class and friend function»
Ex: Clase = Box i
4 private 5
doable widin 5
poblic + ‘
i > friend void PrimtWidth CBox box),
Noid Serwidin( deoble wa)
oto
som Nold> Box»? ¢ Serwid+h (Box double Wid)
| 4 width= Wia', y ee
Nola printwicin (Box box) Se
4 Govt << box width } 5
Co Sink > maind
| ma
4 Bor box 5 5 Fe
frox SerW i att ( 4); -
Print Wictin Con) 5
|Toher {ance
k A praesa of nherting
of ete class
_— Base_ class <@lass der.loes tbo.
TEBR,
Car
] 2 “Types of TInnerirance +
a. Single Inheritance :
_
=
z
=
Class B : puetic A
a
Moitileve) Inheritrance +
Class Bt PUb\ic A©). Multiple “Inheritance nf AL
yeodlass FAL ® ae oee
zi
iz
f Class Be public Al, public AZ.
ar
Sy
a). Heirarchia\ | Inheritance
lass Oa Public 74
: —
z ees e2>e
4 oe
fy 7
> Nisibitt Mode
A - bose loss
B= Sub Gass
Prvate
blic1B Sub Clog: =
E Ce
3
and) INISibI ity Mode
oloss A+ [poet 8
q
sb
then _Peb\ic member. wih be pubes tm Bn and. proea
will Protected , OFA
%e publi
A visibility mode is Private then: both protected
and gowiicN memser of A wit be Private member gs
Bs a Relationship — is always Ymplemented aR (a |
povtic &n hert+Gnce .
= Constructor and Destructor % —rheritonce
Firat child class Constructor wit tun during creation
of cbject Of Child (close, bok As Soon ‘ds ob} ig
erated chi ld class. onstructor YUN and %H WI aly
Gnatwers of FER x axe and After +ne execute
of ponent dass constvuctps = StI], BEGVME FH ConBt nor
ection ,
od potent conptiucens Call k
Ba): AC)
A Constructor exer
q a eel
While @n cate bu desexuctos, ONG cons
Par child destwelos exe 5
eq vent dese. KE Cuved i Gane
8 oS} bye
complete,
counpleseis inter 4
; a ccezaz ko itz own Addrerg
Every object in Cre “hop ac
A shia pointer,
Aarough Qn Amportant peinter allied ie
Y G } inter, fe
oo Friend > function doegn’+ Wave a “tnix | goiter, iD f
feerds ave not Member, of a claag. On\y member
funerioy have thi pointer:
Prinote " i
far 2b |
public
Boe yun
b: e@ (S10, Hy;
Meknod Over Ridin
fachieved - am run ay 8) aD
28
Tr etre @ ve defiritioni sot 4
“aaa
derived elas, with Same
return, __ type a4 private +
Ant gearne
pebliten ~
Notd, ehange geo r CBnt ger)
cs ¢
qr
boi > y
Cosa SporiaGr + public Gar
4
Void Change.qearC Ynt gear)
i ML gear > 55)
I COT e+ 5 a
;
{
ant _mdin
q Sports Cac Bex
c+ Change-gear (4) )
ad AJ Co
5 )
fonction of sporis@r Crass will be called. eon
While calling Change-qear©) , first Fe check TF any fon _
With ania. Gnome! exic+ in Laces 28, otnerwi
capip
W goez -yo ake Clacs » 3
Use 2 ike we Nove . Change-qear fer ail excepr_one &
Suinicn Nave Unig oe: mene d oF oaeariG 4BRK “Shigma) “4 Nivtval Function”
. A Nirtwal fonction ig @ member function which Wis
declared with ‘virial KeyWord’ in “Me base
and ve declared Coverridden) in a dered class.
When you refer +o an object of derived Closz us}
pointer to a base clagz,, you con call a \virtvat Foner
Toh tate abject ond execute, the derived claga’g |
Nersion of -tne function,
* Wey are used +o achieve Run time Foly mer phism «
+ Virtual Function Cannot be Static and also cannot be Fri
function of anotrer class.
Compile-time CEarly binding) Vs Run_sime (hate Ginding
VU
Closs base
4 Public +
Nirwal void. Prins ©)
4 Source “ This ia bose print” << Endl)
j |
Noid Show ()
4 Govt ce" Base show fon" << endl;
$
} F
class derived
4 bs igabivesid Viw 220 way ae to aol
ey Noid printd) Yeap _ 19 paisa
Gay 5 vy aR tet <<" Cher ied- Ccinpioctena ay
Neid Showl) 8 dof or
PSE S | yoo} 1ebut =eok_saetied Srechownetanane ae)
Sat a
os, Smsmain ©)
base * beer
derived ‘
ler) 1
Ne der } ota
% ber = Wer San
ie Spt. gainkGy) Gon ime |
e berr —7 showd ; I 5
! Compt |
i t met e me |
Do F c
moet: derived prints es! 1 \axe ind ing
= }
“WHS Base Srow fun 4 Gary binding.
bye
[PAS dering compiler Rime — bptr “behaviour! jysged on
“ine besea! of which cee Ee oper fepreen
ase cloan.
a tre function is Met Virtual then tH wit atiow binding
a comple time and print fun of base ctass wi
ger _binded ble bere meprezent bake eloss ,
\ Bue at yun >os+ime opin pois 40 Ane.
Wiobject ot clase deyiwed, 30 SH Will bf eind funtion
Oo derived at Ton sime.
Working of Nivtual Function CNTabie > 2 NPtr)
ta class) Cahtalns Virtual -fonorton hen Compiler tselé
edoesi) two anings +
, 4 A Nivrval~ pointer Gere) iS. Created everytime ob) 15
creates for -tnat class which Commins _vitral fonction.
object \s CYeated or or, Static
ea\\ed NiTABLE ~ Where” nen Celi polm
fonction \2 creared, im boge
R “Xrespective of
>) array of) pointer
40 each vith
Clase ana aexiNed Class,9 \
amit vd \\ whoa
= N TABLE
P| St a1 han oD NV
at Pure Nivtyal Function
ew and abstract Closs
enibnid yes
pie Sometimes implementation of ail function nner
Al fim Hhes base, claaz , Suen a Qrass ig caned Gasitre
i me = = drawO 22a
7 ere 9 Get sneres alte
4 re implementation y
2
; 1 ST rewe Square | fon 44
ANipore svirtual Haniction sain crs Ba Vanigal
which ¢ we. don't have any “implementation,
eee
YU Abstract Class
Grt9y 2 Chossy rege
VolfanUF IPYUTMY. to VAIO,
Pet stewsh a
Pubic: Qur
a? pao ost Nitta via fen =05
> bSiteSay = tages
cess ie Beers aa Ae ae a at le
2508 pure . virtual > function, ») fa. pea
RESID ‘yr lasb Seve gannot declare “object of 6 ales trace Cac
Et Ries Sieutes Che
hie ot Sep
a