[go: up one dir, main page]

0% found this document useful (0 votes)
38 views30 pages

CPP Notes - 03

Uploaded by

Nikhil Dongre
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
38 views30 pages

CPP Notes - 03

Uploaded by

Nikhil Dongre
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 30
| Pol morephis aa) a A Tub $4 a Reine: | Compile Time iS |___psly morphisha Po mmovphi yw [ A eae aw 1 Eunction | | Operator Vir ual j + ourvlnading Queelosdling Function q Note. Compile ime poly mor phishn Zz m called as carl binding aol yrarphisss 2 Run time palymerphien is qlSq called 7 late binding polyimar phish Pointers to Devived Clases in Cit: \\ 1) cla RaseClass_f public 2 int var. base * id display ¢) £ cout «" Displaying, Base class variable var _ hase ” di play C) | Vivtuel Functions in G++ lass RaseClass { public i iia Orel cus Ca le ite ol ispl £ coul «"1: Displaying Base class variakle var_bese” << var base <4 “end * ‘ Tut 56 i cla CerivedCla public RaseClass at public cs ink var. derived = 2° void display Oo { cout «2 Displaying Base class variable var- base” $< var ~ base erend|' Pa. SED ie plenrincma ber V lass yoriable var devel | they | ane aceemteoeaby objec a = kero friend | \ a Vietual functions cam another las A virtual Puncbion ih the pape clus might net be used. : The ow viktuoll Pubebions iis det ined A base class there iS ps necessity ch eedefining iin the derived clas - Fut) advantage of virtdall funchion is jake, ~ by eae pcihlers | Th We commen mCueienennee! The clisplay 4unctioh Tom clasy VShittovic ee — cla Tneome then buse class = display ©) pun = Ex: Ti Gel@aar Guise class) Income. in tuk S75 then A Tes ited sian a A Rating Me el 5 sfars qj The length of movie | dy AS hou q Helle | i e_Virjual a | i Ab pstract Base. Class and Pu fetta tora game (rns ete ems iy Abstract base class is a class hak has ak lea one pure virtual Surction in it | ly The classes ae are apherriting Fhe (Maceo. Ge lees la: must Lvietual function of oF abstract clas Lothemuise compiler will throte an error 1) Pure virtual function isa Function that aperatioh and the he valu doesh't perform any function jis declared COs, ighing © to of ure virtual’ function Gre declared in abstract classe anmey abe Gallo b. Sat ) just changing che ihe eure virtual Fy* a ok cede fo understand Function TutS3 a The. line of cod@ aS ba tohick chang eck to Miciuel void! display C= 0 After declaring thiS we doesh't hove access Pt domenica display()_ function e mw | _ declared disday C) Pune MOM olathe sclewived classes ial s + class vsMMovie and * class ‘ncome Pure virtual function is also do-nething Function , cajled ES “| (¢ Beng if tream 2: of stream ==> derived at | # include ~? -e ° 4 aa = i j qe tables oa ee x | For opening tiles usihg Pongieucts~ and 1.) fs becom! ae Teall Weds Estreambese Me bream base there are _ Using the. member Punction open ( ge th du ae tae pening Files” using consbructor and rea \ Be int math 0) tf Jrwing $2 og e itshream in ("Vikalpe Ext" ) 4 Il Reading operation | Hie WWeryoe 25) lee acthis loot\l: ip xint Sooty one | actiine (in, 52); word and step otter ‘space’ coub << s2 ¢ Ste. prin} whole line ve }urh 01 t Output! The senlene written io “Vikalp-txt” file iS _pwinted out Note: Ww an Read/Write file jn the sami | preqeum emia) ativan Gal Zee For operih file using open O) ae eot ©) functions g fine @ tp o : Wwe hae feo function to open file open () andl tot ¢) > End of tile otslreasy --> writing operatien : ¥ } ingteCam ~-7 veading operation (* i ee es inten gray ‘el Na pa a ~ a} | declaring an abject of the type sts} ree a dg : | obstream owt | — = | out! pet @ ample 6O-txt ) \a JIL. tolen huge any VawiabiMWateadis ot ouh _ we opel any File which has: PEF WI SSich 1, Spey e out <<" This) is! we Vn | ouk ¢ OM Thin ise eles Wel 2: out -close ©) | losing the Nihon) fais goad peatic rcturn 3 Ex2/ | int main OT = 1 [declawing an abtet cE the Ree ich —~ iistream il i. [declaring Steitsgs variable sm jing sya Si 3 ies a (Pace | et 2 mm opening, One Me, wee igs yee ih -npen C” comple go txt)” i Giving output the steing lines § Pee enn “syq'F until the Tile Sreaches “the end ot it hile Cin: eok () = = 0) SMT Usigay Setline slosh Wevwhale Vhe getline ( iy Agim )y ih Sm cout << 5 Le end] | 1 rt fur Of Output! This is we this is also me eet. met oN ast i. intaiked y 2 Templates is used te eae BR . TE sivtipal CDasii epee yaesse) ay . On 1a: | ne data tye syntax for Templates é Template myVec Coty) 5 = vector mytvec Ceti); j Fx: | #indude using namespace std; template éolass T class vector public + NCE Whos int size ector C int wm) { Tut 64-1 Sica Ln | avr =hew 1 Csizel ° | 4 T dotPreduct Cvector &v) £ ie clEone pee Ceiba | dey = tis See Oi ev ore Lil, j ¥eEGrh diy ri a ee Ot UCL}. | a é vector «float? vite, e J vechor 4 with a float gaye ——__* Vi-arefoe l= 14 Vd: ave fils 3: vi-avrfoj oly “+ veClor v2 C3); | tow 2 wilt au Bela cherie typ V2+ ark fo] 291; V2: caste! GS Meer \2, yee) ec ; ~ | Pyat = Vi-de Product (Wy: , cout <4 ¢ endl | re tourna; 4 4 6:32 Outpuk ey 4 * C++ “Templakes with Mulligle Poramelens * # include uu i Nam espace pda: Spaleice template < cle Ty__cluss T2 7 class name OFC lass £ bod a thE waind) £ N body of main i Ext class | wy Class g public F int data 1 Tubes char lala 2 void di play O i Bee ie lataieen tier pes daram q = as Lemplate 4 cluss Ty class T2 > a class yay Clu 4 | public § | Ti daled ae | eae cleaned a myClige CiW lah ea ie a ii ~ datal=4 i “Ne a dafaz=b° “a t a veid disslay Of im 6S Couf cc this yer IN Ze this» Jair a ik a 4 —¥ - 1 tnt main ©) 2 my Class class Vikalo { puligne [Bk ae Tut 66 (li vio Ws alt a NICO Gie mea tio ye iratire Jee a= x 1 Ee 4 e=2 void displayc) £ cout 44" The syalucl of yo i .s4.9 6<.end] | cout" The value of § is # <¢b << endl: ome «| The Wale as opie 55 <4 & << endl ast to Pe int ity 4 thers “> NVikalp 2 & joel anh = wa wd Se at Avi eal pi 2S LA os Yes die eee it cout << eh 1! \ 7 Vikalp < Heat hare char Wo C46 ( rai ome display) return 0+ a ut The value oun S M Wie : i Uy M Ci SR ian) | The value f Cue ead a y i sl - | || Ceti Ht af 7 Hf | ti i i 7 (* no] 4 ic J _*Funchion Templotes and Functisn Templates wi Parameters L # include Ziogtream » casings namespace sid | template < clogs iy dlaggaiess 7 loaf funcAversae Gime ab al Wet Ploaf avg = Catby/r2 eo o returh ou : Tuk i 7 61-1 ht main) t I lee ancl. a = JuncAverageC at” peintt (" The Average of these number is ‘/et a)” ve Hen ot i — The avtwage of these yumber + js 3:5600 oR template « class T 2 wrod swapp( T 4 a Tid bd # inclucle ~iostream 7 using neim es pac 5b template 4 class T class Howse 4 public « Tt datay Horse (CT 4) i at Tut dala -= a ee! void display O) 4 ie bemplote 4 class > 3 yoid Horse 417 2% display) 4 cou se denies 1 int wacin GUMS Horse 4 Moat? WCS:7) | ft include zioslream » . buns WON ESPAce id void func Cinf ay & Reva daw jal alu had Wl INGE Gym arc chd | \ p | template aecllcs shail 2 void func Cray C coute<” Tam demplatised func c) 1 4¢acéend |) t Lint main © func (4); J) Exact tach takes the highest priseit ve}urh 0 | g ] } Output ASU iled yy nding eae GD: 4 Ani isi sia Daa int Y fe nd \J ond classe - We) ewe these \_ 1 (3 her unc} ener This helps. us : i, ed classes and functions * ompone A tS eee n Sule ieee Wwe have three comeonentS ih STL 1: Container. 1:| Alaovithm Secs " I] 1-| Conjamens | Conbaihers $5 an object ul Stewes dala we have ditferen} confi haviha theik own benefits = 0 i Algorithms: Algopiths ake oqo set of ~ ins teu fen which Wnonipuletes the input — data to arrive alt semel Nedved pecul| —-— | Th stl, we have alkeady written "q & alaveithin Smexamalee 1 sot sow — data, oh scobelil smelislements in acs Thi algovithing use temolate | Wanerichs. A =| 211 ceaisvst eee sl ects hich “echcr Je an element jn a container hand Mier W ime tee Their basic ab Teel cat neenll qlaaathe to az : oO the ontaine and plays a very vital rele in manipulatich of the dala Ex | Container ae ES tia si] t | poinler, which are tree fo move dleng the | conteiner H = | Containers are themselves of three types cee eauence Containers 2: Associative Con{ainers Derived Contaihers | 1 Seas ence Containers ? slores the dafa ih the linear Jashion e&—O— © OOH Elements are addled here oie in the beginning Elements stored in linear Jashion nee__contaihers include Veclor List Yequeue ele these awe mast used 39 | Assaciative Comfainers i stoves theik data ih 4 ! Anes like structuee | | ® uh ear 3 4 © © _ WScins Be the. ~ So atige eo ~ on jain ev. nee “roa ete Se i Mulfisel Vlap- multi Derived Containers 2 odelliv They deal with real ite nacclelline Some cxarroles oh Aewived! containers ave ~ Shack _duewe Priority quene _ehe —— [sm ilementiawetadded as a 16 well as) wemaved from h Stack— |2 aa 38 | They deal wilh real it | T Slack Pellews JTFO Clash ih Sis} out) privcip: aig Lleuit me QO: 9 use! alle FTF oC Fikstih Divst Suk ) principal Nele: Thi bopics stack heap eas ce ope — studied ieee tele jh dois — structure onde algorithms a #| When) To sen cohichs: Confainers 2 \ In_sequence containers, we have Vectors, | which hac alle wing, Sercerea \ pest \ DY Fast RA CRandom Access ito elements ih i 1 CoMpParison jo Qeray \ 27| Slower insertion and deletion ati some random - position, xcept at the end j a) Easter insertion at Th end | Bin Wisis we have 17 Random accessing elemenis is Joo slow because ev. by clements is traversed ore psi ter. 207) Tiksertion andi deletion ah an position velalively faster becouse they hl, use pointers | which can ec thy ke main pulated * Th associative canlaihers every operation ex cept wandem access iS) Fasler ih Com parissh to any other con}ainers , be up| Wncew ling low edelelindnianorclemen’ ih associative. we cannot pecifically tell which opewcitjoh 1s Taster or slower we'll have to inspect ever data Olea cluye seperately whieh. 1S given ih | Wolee detail in DSA | Need to study on Mlemben intializers and Composition ,imere in details ~ Selelecien| ee ein ializerion list climinafes the need _ to place explicit a ig ments inthe Constructor! body. Also othe snitiolizedion | clecs hot ene with cere a yThe member initialization list waa used fore reqvak = Oy Pegular voriubles and muct be used for cons}War = b* ne constant variable, private 4% ink wegvar in ons i var — > | Right way fol do this ==" PIE Vel jn) coecamipecshichummeinlewil amallegia hel constant it make, good sense fo use th member initializes synb an A ni mber variable must, the constructor initializer list Fxiz) Class Person £ public. Persen Cstving ny, Birthday bY | class Myclass £ Muyclass Cinta int} ) & reqvar( a) constvar¢ h) » i 4 | __Private ¢ con, in mst var) nome Cn) b tc { i private % Siving name ~ Binthdey be as, aw FFinclude * Birthd gay = This’ Toate header and wee tiles /where youl cheat ~ Ta file’ like thS and call it ih ansther function GSP nclucheer thew. inf sen Ag Gg int mother Age = 23° \ iE C son Age > mother Age) 1 int son , . ; | | row 99 * iy iE Conf yiwethee ge) L reghain Pah 3 7 senAge Gmather Age --7 fi - row * Wren alice. - ; = | catch Cint x) { i] ei 1 1 oi out << “ ee age alues - Epron ? 22% - Presa: ill un but give, a exception err : 1 wh _songAge 19 greater which EPROM ccc aiteaHl 2 kong age wolues = Foner a | logic "that sonAge is hever gb ater than i mother we Say like that | ele: Th the throw <¥abewment the ope cletewines @ Vype Sew the —This can be ony eyphession & expressions wesulh will The exceptish Throw, oy ieatehplanay SME ond | | mos eat T

You might also like