0% found this document useful (0 votes)
27 views24 pages

Oops in C++ INTERVIEW SERIES - 2

C++

Uploaded by

rekhasaini7514
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)
27 views24 pages

Oops in C++ INTERVIEW SERIES - 2

C++

Uploaded by

rekhasaini7514
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/ 24
OOPS in C++ The Main aim of OoP is to bind together the data and the Functions +he operate on them so that no othey pavt of tne code can access this data_except this function. +[Poumerntom py | __ [hhevitance : Th is @_usey defined data types, which ‘Functions, which can be aecessed and used by creating an instance of that class. ect »_ When 4 class is defined no memory is Qulocated but when it is instantiated Ci-e., Object is created ) memovy 1S allocated. => Encapsulation: Th OP, Encapsulation is defined AS binding together tne data and the functions that manipulates them. => _ Abstraction = Abstyaction_ means displaying only essential information and hiding the details - Abstvaction using classes _ [Abstvaction using Header Giles (math-h > Powi)) => : In simple words , we can define polymorphism _as_an_abliity of a message to displayed in move than one form. ____ Operator Overloading Fun in _L int sum Cio ,20, 30) _ __Int_sum C10 , 20 ) _ Thhevitance + = The_capability of 9 class to iv: S and chayacteristics Fyom lanotnes’ ctass ig cauled tnnedtance. Subclass — H Ss perc ass _| Reusabiity Th dynamic” binding, the Code to be jexectited tn response +o decided at wun time. Function cau is Styuctoys : _A_constwctoy is a member Function of a class which initializes objects of @ class - In t+ _Constvuctoy is _Qutomatically called wthen the object cyeates- oe It nas same name as class itseif. —____Conspyucter_don't nave a_yetuyi _Degautt_constwuctoy (NO a aracieres 2 ial _ ‘Pavametyized Constvuctoy ATUL Kom a LINKEDIAL)- (Dewlved class ectnicioy vail be inveked Fivst , then the base class destyuctoy will be invoked. [Public = can ieeWacreseal 7 any class. —Exivate i= can be_accessed only bya function __In_a__class (inaccessible outside tne class ) ___ Protected := Tr is also inaccessible outside —__the_class_put can be accessed by Subclass ar +nat class. : Note :- If we do not Specify. “any “access _ modifier _ inside -+he class then by default tne access modiFiey for the member will be 2 A Priend class can access private and Protected members of Other class in which it is declayed as Friend. Ex :- Friend class B; ATUL Ku mek Th “i c NOTES GALLERY (TELEGRAM). Class Subclass : accessmode . baseclass _ Combination oF one et move type. Operator Overloading. —? Compile time Poty Function OverlOading — => 4 — — Runtime Poly Ly Function Overviding occurs when @ dyive class has q definition of one oy more members of base class. Advantages of Data Abstraction _ Avoid code duplication and inc. yeusabilit _.Can_change inteynal implementation oF class. independently - o ue! OPET come toy (TECEGEAM) __ Stvuctuye Vs Class —______.Most important difference i _ A Srrvucture is not secuye and cannot hide | [Srs mbey ctio vaylable while class if _secuve and _can_nide its programming _|& designing details. _lpca@l_ classes In Crt t — _ [a caate aeclaed inside a function becomes _____ | leca) to tnat Function and js called iocal ____.class - [All_the methods of local class must be ____.defined inside the class only. | Miztual function and Runtime Folymoxphism : => A virtual Function is a member Function which is declaved with a base class and _iyedeFined Covervidén) by derived class. Functions ave declared vulth virtual Keyword _ | base class. - —_ 5 -_ =P Runtime Polymorphism , also Isnown as the Dynamic Method Dispatch, iS @ process that yesolyes Q@ CQ\l to an evervidden Method at yuntime - The process involves the use ©F the wveference variable of a superclass to Call fox an overridden Method. i in'e +h bry: represent a block ef code -that can throw an exception. _ —_ (Gatch: represent _a block of code +nat get —___|____ executed wihen_exyox ig +nrown. __thyow. Used +o throw an_exception . [ia ka eee Game mk ae ft catches all types of ervoy-_ 1 | gee line Eunction a _Intine_is 4 veqsest not command. tr is function _tnat_is expanded in tine “when ik is catled. Wwihen the inline Function is _ eS ae mane oe Web get inserted oy Substituted ___|at_-the peint of iniine Function call. inline yeturn-type function ( _) Function “overloading is Q Featuve in Ctt_ where two ox more functions can have Same _name_ but different parameters: Veid print Gat i i) a — cout <<_ “Here is int” eax end b 4 pyint (float i). cour << “Hexe is float" CC contains 32 Keywords|¢ C+t Contain 52 keywords Cpublic, privare , protected, try , catch, throw * C is a function _dyiven |® Crt is an object language. dxiven language. Function and operator |e C+tr supports function overloading is not _ & operator _Support in c- — Overloading. .C_does not Support _|@ Ctr supports exception exception handling - handling using +vy and catch. _© | Styuctuye is Q collection of dissimilar ee _e€lements. ——— —— —— - ATUL coma (Linke A). ic : ce NOTES GALLERY (TELEGRAM), I «Static variable in @ | ction: _ — When Q_ Variable is declared a¢ static, _|Dectared inside the class body. py Also Known as class member vartable. > [They must be defined outside the class - > bas variable doesn't belong to any object, but so the wihole class. => G > There will be only one copy ef Static member variable for the whole class. Class Account paivate + __int balance; Static Float voi, public: veld setBalance (int b) < balance =bj F 4 | __SMLintialised outside class Float Account +: yoi = ‘oid main x Account At; lObject_can_ also _be declared as Static. ____ Static Account 945 _Static_member Cunctions are_ Allowed to access only the Constructors is an special member function of tne class. Ib is Qutomatically invoked when an object 1s cyeated- > Tt has _no_yetuyn type. > Constructor has came name as class itself -_ > _IF we do not specify, then c++ Compiler —_.genevates @ default conshructoy For us. __ Parameterized a class_name _§_Jclass_name C Parameters ) class-name obj update Cconst ; |Comptier 3 Nerates two constructor by itselé- | 1"[ Befaun Constyuctoy Copy Constructor 4 But if any of the constyuctey is created by usey, then default constwuctoy willl not be cryeated by Compiler. => Ceonstyuction overloading can be done just like Functlon overloading . Defauit Ccomptier's) Copy Constructor can dene only Shallow copy. ( Points to same memory location) _* Deepecopy is possible only with user defined constyuctoys. - {In _usev defined Copy, _constyuctor_, we make_ : pied Object points new memory location - - Can we make copy constructor pyvivate ? Yes. Why arqument +0 copy constwuctoy must be passed @S a yvefevence ¢ Because if we pass value , then it would imade fo call Copy constructoy_thich becomes non - ee : Obj 2. Deep copy. —Destuuctor. Destvuctoy is a member Function which destructs ox deletes an object- Destvuctoy don't take any aygument and don't have any yetuyn_type- Only one destwuctor is possible. Destvuctoy cannot be static. Actually destructor doesn't destyoy object, it is the “last function +hat invoked before — (Object destvoy- _ ATUL NOTES. GALLERY ( (receuermt) et Resource esteuctoy iS used , SO +nat before deletion _ ‘OF Obj we can free space allocated for +nis ce >} gets. deleted then Space | Tauvcated ax Ob} willl be free put yesouyce —doesmtp Nv. f vovide > int main C ) a Clalotec. NOTES omteey (TEL ). As‘+? can't add Complex no's divectly- So we — —.can define @ function with name + but we iNeed write opevator Keyword before it. $0, We can use all operator like this. [A Friend class can access —the_private and} protected members of Other class in which it —is_declaved as fyiend. Theve can pe Friend class and Friend na function . Class Box ___t private: double “ldth 5 publics - ___ Fviend void d_prvintwiidth (Box | bows __Void_setmidth (double wid ) 5 etwidth (Boxdouble wid) _ {width = wid; > Void _printwidth (Box box _) {cout << box- width; > int main (_) Box box; box. Setwidth (14); printwidth Cbox ) 3 os _Tnherxitance oe _ ___It 1s 4 _pyvocess of inhevting properties and _ — -behaviouy OF existing class Into _Q new class . Class Base_ciass _ [Class dex_class : visibility 4 =—Mode ats Class spost. Ca¥ t public cay ATUL Komnde(UariceDr NOTES GALLERY (TELEGRAM) | )- [Single Tanevitance; | _b). Multilevel Bihexitence: ‘class Bs public Class B: Pub ic Ess | - L < x: E 3. Class C: public B t H | — => Multiple Tnhevitance. Class At Class Az ty; ie Class 8: Public Ar, Public Ar Class B2 : Public A A= base class. B = Sub class . IF B is Subclass and visibility Mode is public ciass A: (Public YB ° . 4 x5 then public member of A_ will be public in & and_pyrotected will pyotected . —_ TE visibility mode ig private the both pwotected and public membey of A willl be private. ee Membey of Be hip is @lways implemented as Inhevitance. _ | Constructox and Destructor In Thheritance _ [Fivst cnlid Class constructos yall! yun. during \Cyeation oF Object ef Child class, but as sOon land_it will_call_constwctor of its parent | class and aftrey the execution ef pasent class _lconstructoy_ it vill xvesume ip constyuctey ENERO pore ~ [constructor exec : ob While in_case of destructor, cnild const Sn child destwuctar_exec, L parent Const ‘Complete child, | this Pointer Every Object In Crt has access to its own Addxess thyough an important polnter called +this_pointer- ACU espe NOTES GALLER \Friena “function doesn't have a ‘this’ pointer, because fyfends aye not members of @ class - —.Oniy member Function have this pointer: int tmain Cc —— Box b; b:set(S5,10,4) > | Method OverRiding ( achieved at yun time) _|Tt is -the yvedeFinition of base class Function in_its derived class , wiith same yetuyn type and same _ parameters. | wunile method Pverteading iS achieved at ‘Compile time: ror CmLeRy (reveanary, Class Cay Private : int qeay no 3 Publics Void change gear Cint gear) ports Cay ¢ Public Cox ____void change gear (int gear) {if (qeax >5 ) gear try > int main t SportsCar Sc 5 Se. change_ gear(4) 4 ~ Function of Sposts Cay (sc) Class willl be called [white calling change — —eay (), Fiver it check. iF any Fun with this name exist in_catiin iclass , Otherwise it goes to paseclass. juseful: Like me have change Qeay For al except One Cay which have unique method of qeaychange - istual Function A Vistual function is a member function winich is _declaved with @ ‘virtual Keyword in _-the base class and yedeclaved_ Covervidden) in a devived class. Athen _you_yefer to 4 object of devived class using —peinter_to a_base class seu. &¢ cen calla viyrual function of tat object an execute tne deyived cigss'S version of _ _ tne Function. __ They ave used to achieve Run time npnism « _Viytual Function cannot be. _ static and “also. “cannot_be friend function of another class. |Compi e-+ime Céarly bind binding) Ns - ‘Runtime (Late Binding ) Class base { Public + {cou ee ___Noid show (_) _ Class derived Lt Public + Void pxint C) — Lcout print); ———_// Run time bery > Show), Compile tim | derived print __//_Late Binding J Base show fun __// Early binding _ As duving compiles +ime bpty behaviour judge on the bases of which class [t belong, 40. bpty yvepyesent pase class. — — Tf tne function is not virtual -then it with allow binding at compiler time and pyint — (fun of base class will get binded because ____._bptry__yepresent base class - —_—__|__ Gur at _xun time bpte points to the object of class devived , so jt willl bind function of deyived at yun time. _ piley itself dos es two things: 4 A virtual pointer (vPTR) is _cyeated every time Object Is _cyeated for that class which contains virtual function. 2. _|Trrespective of Object i¢ cyeated oy net, _ Staric ayray of pointer called (VTABLE _ where each cell point to each virtual _ Function is cyeated , In hase class and derived class. — duzing Eaxly bindin VIABLE for pase class VIABLE FOY “derived class - irtual Function ~and abstyact Class _Sometimes imptementation of all function - cannot be pvrovide in the base class Such a lass js Caled abstwact class. AML Koma (ukepin)- MOTE S Gacrery (TELEG Ray) > ( But there can't be any imple menta ~tion of dyawC) fun in the _ base class) =e A puve virtual Function In ctt is a Vixtual Function for which wie don't have any implementation , wie only declare _it- Class Test ft Public : 2 Viytual void func) =O; ¢ Pure Vivtual Func Hon. 4A class is abstvact if it has ‘at _leact one __puye vivtual function - 7 We cannot declaye object of abstract class . Exe eset ne til! snow eyvoy . ae le_ can have pointer ox yefevence of a apstvact class. _ =——_—- 3-_Wle_can_access -the_ other Functionc except _ tee deni ove of its deylved class. —4- Tf we don't override +ne pure virtual _ ‘Function in derived class tnen it becomes ___ jab stvact- a : —e 5S: An abstract class can nove constyuctors, _ (Read From GFG ) => [Template in crt template’ check (a, xb ) t if Carb ) __yeturn a, —___{It just help In data type. So that we can write Genetic function that can be used fo = different data type. - Class qeeks £ Public: ones void fun() { P=newchayl6] >} x int main () geeks 9

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy