00 C and C++ Essential Hand Written Notes

Download as pdf or txt
Download as pdf or txt
You are on page 1of 15

Aros

S t r u R

Ote 2.51Tutzoz
Page..-
C and Ct Concept

data tyPe in Congecctue rmemsvy leatc


Araus MainMem oYY
i n t ACST A oI
Hep
Itialz otion Stak
delia

maun
(od e main
a[s1=703 Se
Pvint CaC21) > fo
be cause ato] was initiali zed

>farint r: A) eoth edemert io amau

Daint (o

Stzuduaf Coilection zeloed dota mem beY in_One


name

Struct Rectangle 2 dana lehas mamber leng bza

lot length bute ey Bate SisedaLe


int baeath27 teE 2 0Y A bute

int Dmain O

siat Aedanale 1,deeloratien


Re ct angle 10 S ei,lohi
iihali7chen
Stauctuae
Basics o Cads
oats 2.5/1hozp
Pege---------

oy
G
aLcessing
StY t -
MembeY
(D01) apevodto
lenthz S
1SuSed Maln Memo

Deftrinq Heap
Omplex Dumbex án stxudtuC

Stut Complex Stade


Stak el m0un funt
int e a Lache MoO
i n t img Section

Caxds ACe
Fare L2,3 10,1 K, Q
Shape ,Q42
Club
2
spade

CDloy Black Red

Siz uck axcd


int face
int shape
int coloY

int maio U

Stut COxd C
fare
Shap 0
olT 0
card C21,0,0
stauct
]o-or long un sfgn ed I
Date
Page 25.LUbo

nt Main ()
StrortCd deckIS21

fori0iK52 itt)

Couet fate
Cin 2 deckLi]-Eate, (out<Sendl,
Cout " Shae
in72 dec kl] EaLe
Cout Sendl coloy
ciO >2dec k[il-Co lor

Hoc Ci 0 KS2itt)

Cout <Tdeck 7. fcace <t <S deckEi) ghape <<" " ( dectTl
colr

X t ut Dectangle

ot lenqth
int baeath
Ctd chax X
Stouct variable
declast o4 globa

SLut ectanale X
int m aioO_
FOr long unsran ec
lsizet1));>Prin to 12 in stead ol
printf ho been oJoted bytesosteadC onaO i c h
hatrater
rt scottotmerThi sTOTN SPRdtthTPpateting
oemryisdahelbetz other wo ho been_Qllhted 4 bertlA tberumel
hounoly 0 oullo t byted to Ch ar atter
Pointer ok e 2 byte r9allot t etus n void poirder
Heap memor y
Poin tes Adldoeyvaaiable
PsoqamCsenuenite_inaixurtzcoLin pxoqz.amming 44
aniqw h d _ a Lnthp ncec to exeLl
Can aces) only_code
Star
st ack Section c memosy o aLcess
0 an
extena Heap ard extexnalxeioUaces,poiniea is M ain
Code
rsequided
MeMong
Uses
)Acces SinG Heap
2Accessiog_anu exteana esUBE)
padametes passing

int P:<dedloration
p &ain ttializing
printfhd tp) <deaefoenting
o allocate Heap menms brny tdlib-h~
Ftle
Malloc
fun etiun >

fnt P Co ecting
linttmallocl5*Sze Cint));
p: far heldy n Hef
clement 1t co
Laoy Si2e Cno
int pointe memon
DE newint L5 fox (tt

dealocodtina the memoxy ln heap

for aee Cp)


oY Ctt912 delete C p
Potrcter *Y

only for Ctt e Foente> 4Y


Date
e enCeuse lo p.ax aM eier Dass inG
Refaence: Anothes name gtven toexiStna
vd'siable
int mon )_ Leap

intalo Statk
Íntk=a (S+0dk

COut< , Moun TugTwnM

Yit Code Sethen)


dout < a

Retesence is not like pointex7t does not (on Sume


lony memonyt uses the same memoRy af the
Vaoiable it is ioitializ ed CHese tuses Memar o a

Aressing Stoo ct membes USing poiter

i t maioO_

Stoud Rectanqle ?lo S


stoud Rectangle p :{z
Tlenqth=15
tp).leng th =20 oYplenath= 10
actes Sin 3irUctotC eements USTng PoiNer

object
Creating aaiade cB tupe o stauctuae dunamically
data type
in hea0 DSiog otntea

Staot 3ectangle Kp
0rCp (strut aectarle *)malloc (size c Cstouct Rectoungle J
fo
Ct p new Rectc nGle
p length =10 00 CxP):length =LD
P Doeath S,
mon olit h Modela O
Poot e d u l Prog m
Pro qOCM
int main () fone C)
3
moun

>Functiong

f'boto -lype 0Y Teoder o


nt Ran Cint cint b)
delin athens
int
Eatb
aetuzn CO
3

int main O adol

Moun
'nt Y2

y l0 epervrnsrete
2 addl y);
PYintlz))s I
3
tunati on vabiable dies a soon contaols
setoinsdomfunctioo
tP Pevehenin
Poi nte takes bude
clont (cuhe any yyemor Date
A eence Page2202(

Paxametes passing
uitabte OOetUOTDinu the oesvt
c a l Du values tOamad pabam eter does Ddt affet
actual PaBametev

erc ieloetuaalna-rerethauavaiiable-erthanging-attoe-Prerde
call by acldoesS Aoy change io foamad paramete
atlect actual padcmete
ode Th ptotrdura he
Uthough the SoD
Calbu dPedene_xadham S monolui okile (ormplng
becz aJ Soon as Swcp 15 Cdullecd i becomes Past c rocin hned

Void Swap Cint &x int &)


iot temp
OH bly temP

yEtemP Ma
3
ot maio)
R
al
20
Do? 203
200-2ol
b 20
Suap C b )
prI Ca b)
Pointer in tes (ompilex take) bufe
We mnn ot ufe fbx
sinte
cach loup on Pointex
P212

ZHTTO paxametercanbe daebuucallby addd es


ointerfor an Arna AU 03 A
id int funct (1ot At1)br int funtiittALLD
fcrezliaectaepameter

KetuxD*nq an axa om trunction


Dy o ccd a2a in henp: mermoy n fon d

int x fun Cint n)_

int F_ pP
p Cint mallo nt Size dCnt));
eturn (p)

int main O

int xA
Complet e adoo in Stabctuse con be corn pl elef
PS Cven by Gall by valuc
yDate.
Pege 2h21
Stovctvae a Panamele (Ho func

Cal by valle > Attuel pavarele oort be offed cd


Stut Retacsle G
nt axealstauct Rectangle x1)
yilenqth +t 1Ad lragh
xetun 71-length Y 71h1ecth in becth
3
ot MainO_

(tovut kectanqle K-
C
n t aoRj

a a ea (Y)o
pait(Cl

XCalL by ReBeaence
int main is SCume

inte cLa.ea Cstauzt Rectanqle 2z1)


vqxtable accessin9 structunes P. length
ots.

stauctuDe PSleng 212/21


potntea acce sinq

Call byaddaes

YtaidfenLstauet (Name)
LEYD
VOid funCStsu ct RECTAVaLE

lenqth tti
bae ath ft
3

iot main ()
i stauct Rectangle x=t10,1
funC1)

eatinq struct in h eap mem oxy and seturinq it


addaeasbu tuact
Staut RECTANGLlE fon C)

Stsuct dectanCle 1f
x = nec Re ctcnqle OY e6tret Re ctaungle
Mcultoc Csize t et
Retaqe)]}
new Rectanqle C3J S to orde 3 object
dada type Rect angle0 heap Me o
P Date 3/125

Class and Constouctor

Io C:Class and (00stoucto8 ae nt theae


8ut in t t tue do have them When many fonctions
aoe selated mostly to some stbuctuae it ca be
conueatec to daa
ekosp C
stuct Rectangle >classRectan qle
Lpivatec
nt length at lengh
Int beath int bacalk
_publi2 onstTU(tbYb_
Initicuize
1oinih
Rectangle dos vaiable
cint iktb) cu ay
soon

Notd intiaine(shudt Rectangele 1h1


ength dela ed
boecuth b,
lenqth: |
baeath: b- Method abrect
t a e a C) etang le
3

nt ddecd (Stsud vectan ele v int change lerqhtinttt


Xetusn leng{h YYbiecth;
3

tloid chanGe lenq k(struct rectany le #vd)


lenqthl
include 4iostoram oo 1f 0n

inclode < stelio k for C


(o311,C

2Tnidialize vaxiable altea declaatint


int iEO instead d atiy

Monoluthic Procoampooqaam jut i0 Main fundion


OY Justosing one main finctin

Pooce duaal 0r Modelar Paoavam Main fun bi&n


wfth oth er fuoeions
mcu'n fun chon UStng
o th er funchins

bsiDq staucuae and fuochon


0opStyte CtAin clas con twh
declasiDq aclas.s a s DAMe (vartable name
Fq Reetangle
havinq MOe han One Contotfot
S knOuwn O Constxu to oNez loa dYnG
)Date
Page 312.1bi
Class and (oostauctor

HadudeKiastaeam~
Oinq namespaLe std
ass Bectangle
DiNade:
Int length2data membey
hot boe atb 0-Rectanqle cass 2
publi
cOn Rectanale C)length=bae cuth=1,2 o defob
Constcto Paam stesized (onstr
OveToaoTTU Rectan gle Chnt 1fot b>
ot Cse at)y
aclatotbu
a tcessoY Or nt peaimetex )
gte fundnak cet lenqth ) retua o lenqth,
mtato er tun iuni nt setlength Oi lenqth:3
Deur in Rectancle )
3

Rectan qle Rectanqle Cint LYat bl_


lengthEL
baeathb
LRotur O dupe2
Rectanqe s adea )

setorolenqth Doe ath


Dests octoy: Destaoys objet
o
Destro ctoY is tolied by (ompile Dats.
HO rm ourn f un ltiup

int Rectangle Pesimekex O


detuno 2r Clength bbeoudtt)

Reclanale:: o Rectanqle

ot mauo)

Rectanale 10S)
COutK3 peadmetey )
Setle nath C20)
Cout 4T-qet len ath)
3Wouo Dectvueta w bealledand t detroug
Recd anql object
Scope Rejolutron peratrv Date----.
Pege.3.|al2 b2

lemplade cla
knenenitttatt
henedic Class9 Suppontb_all cl ctta tspe no datatsne
rebtriction)
templake <class I Scope
endu hcuC
classAta Azithametic
2paivate
Ta-
bi
public
Axthmetic CaTb
dd
3
Hemplate < claas 12
Axthametic Ki) * Adith mauicTC b)
tas Auihct

this a a
parrnefer
this b b

n t Main )
Thetis en plate declaxinq

Aith motic kínt>> C1D S),


tout K cdarad d
i thaahc Klaad ax1 0 2S D
COut ayi-add D;_

You might also like