You are on page 1of 24
OOPS in C++ The Main aim of OoP is to bind together the data and the Functions the operate on them so that no other pavt of tne code can access this data_except this function. + [Pogmerntom py | __ [Thhevitance : Tr is @_usey defined data types, which ‘Punctions, 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 jnstantiated_ Ci-e., Object is created ) memovy is 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 niding the details - AbStvaction using classes _ |Abstvaction using Header Gires (math-h > Powd)) => : In_simple words , we can define polymorphism _as_an_abllity 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 + -_Hhie_capabiity of Q class to iv: S and chayacteristics Fyom lanotnaa’ atass ig cauled tnnedtance. Subclass — Ht iS perc ass _| Reusabhity Ta dynamic” binding, the Code to be exectited tn response +o decided at yun time. Function cau is Structoys : _A_constwctoy is a member Function of a |class which initializes objects of @ class - Tn. t+ Constructor is _qutomatically called wthen the object cyeates- oe It nas same name as class itself. —____Conspyucter_ don't nave a_yetuy _Defautt_constwuctoy (NO earaceren 7c _ ‘Pavametyized Constvuctoy ATUL Kom oe LINKEDIAL)- (Dewlved class ecmicioy vail be inveked \fivst , then the base class destyuctoy will be invoked. — (Public = can ireWarcesceal “ae any class. —Lxivate i= can be_accessed only bya function __In_a__class (inaccessible outside tne class ) ___ Protected :- Tr is also inaccessible outside —___the_ciass_put can be accessed by Subclass ar nat class. _ . Note :- TF we do not Specify. _any “access _ modifier _ inside -+he class then by default ithe access modiFiey for the member will be * 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 mAk 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 dvive 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 xe MOPET emcee (TeCEGEAMT) _ Stvuctuye Vs Class —______.Most important difference i - A Srrvucture is not secuye and cannot hide | its mMbey ctio vaylable while class is _secuve and _can_nide its programming _|& designing details. _lpcal_ classes In Crt: — _ “Ta cendie aeclated inside a function becomes ____ | leca) to tnat Function and js called \ocal ____.class - [Al_sthe methods of local class must be ____defined inside the class only. | Miztual function _and Runtime Folymoxphisy : =P) A virtual Function is a member Function which is declaved with a base class and _lyedeFined Covervidéen) by derived class. functions ave declared vulth virtual Keyword _ | base class. - —_ $ -_ =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 vefevence variable of a Superclass to Call fox an overridden Method. i in C++ * bry: wvepyesent_a block ef code -that can throw an exception. _ — (Gatch: represent _a block of code +nat get —___|___ executed wihen_exyox ig +nrown. —_thvows, Used +o throw an_exception . ea ka aed Game mk ee ft catches all types of erxvoy-_ 1 | ae line Bunton a _Intine_is 4 vequest not command. ltr is function _tnat is expanded in line “when lik is called. Wihhen the inline Function is _ ee ae mane Gn Nai get inserted oy SubsHtuxed ___|ar_-the_peint of iniine Function call. inline yeturn-type function ( _) Function “ovesloading is Q Featuve in ctt where two ox more functions can have .Same_name_ but different parameters: Void print Gat i i) a — cout << “Herve is int” Becainee e 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 Crt supports function overloading is not _ & epesatoy _Support in c. — Overloading. .C_does not Support _|e Ctr supports exception exception handling. handling using +vy and catch. _© | Styuctuye is Q collection of dissimilar a elements. —— —— — ATUL coma (LINKED A). ic : ce (NOTES GALLERY (TELEGRAM), I «Static variable in @ | ction: _ —When Q_ Variable is declared q¢ static, _|Dectared inside the class body - y_|Also Known as class member varlable. > [They must be defined outside the class - > bas variable doesn't belong to any object, but to the wihole class. = 3: > There will be only one copy ef static member variable for tne whole class. Class Account paivate + __int batiance; Static Float yoiy public: veld set Balance (int b) < balance =by F 4 | __ILintialised outside class Float Account +: yoi = ‘oid main x Account At; object 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. Th is automatically invoked when an object 1s cyreated- > Tt has _no_yetuyn type. > Constructor has came name as class itself -_ > _IF we do not specify, then c++ Compiler _.Genevates @ default constructor For us. _ Parameterized Se class_name __§ _Jclass_name C Parameters ) class_name Bobi update Cconst ] |Comptier 3 Nerates two constructey_by itselé- it 1°[ Befaun Constyuctoy Copy Constructor 4 But if any of the constyuctey is created by usey, then default constwuctoy willl not be created by Compiler: => Censtyuction overloading can be done just like Functlon overloading . Defauit Ccomptier's) Copy constructor can dene only Shallow copy. (Points to same memory location) _* Deepscopy 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 avqument +0 copy constwuctoy_must be passed @$ a vefevence ¢ Because if we pass value , then it would imade fo call Copy constructor _thich_ becomes non — ee : Obj 2. Deep copy. —Destuuctor, Destvuctoy is a member Function which destyvucts ox deletes an object- Destvuctoy don't take any argument 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) Objeeh _Resouyce esteuctoy iS used , SO +nat before deletion _ ‘OF Obj we can free space allocated for +nis Ce jets deleted then Space | Taureated fax obo} will be free put yesouyce —idoesmtp v. f vovide int main C ) cupena Clelotec. NOTES omtee (TEL ). As'+? can't add Complex no's divectly- So we — —ican 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 —lis_declaved as fyiend. Teve can pe Friend class and Friend na function. Class Box ___t private: double sldth 5 publics - ___ Fviend void d printwiidth (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 ) os _Tnheritance 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 pats Class Spost. Ca¥ t public _ cay ATUL Komnde(UAriceDr NOTES GALLERY (TELEGRAM) | )- [Single Tinevitance; |b). Multilevel Dhevitence: ‘class B public Class B: Pub ic ES. | - L < x j 3. Class C: public B t H | — => Multiple Tnhevitance. Class At Class Az ty; a 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_pyotected will _pyotected . —_ TE visibility mode ig_private the both pwotected and public _membey of A willl be private. eee Membey of Be hip is @lways implemented as Inhevitance. _ | Constructos and Destructor In Thhewitance _ [Eivst cnlid Class constructos wail 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 will xvesume if constyuctey ad pore ~ [constructor exec : ob While in_case of destructor, cnild const eee child destwuctar_exec, L parent Const ‘Complete chitd, | -this_ Pointer Every Object In Crt has access to its own Addxess thyough an important polnter called +this_pointer- ACUL espe NOTES GALLER \Friena “function doesn't have a ‘this’ pointer, because fyfends aye not members of a class - —.Oniy member Function have this pointer: int tmain Cc) —— Box b; b:set(5,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. | wanile_ method Pverteading iS achieved at ‘Compile time: ru cmLeey (reveanary, Class Cay Private : int qeay no 3 Publics Void change gear Cint gear) ports Cay ¢ Public Cox ____void change gear (int gear) {if (gear >5 ) gear try > int main t SportsCar Sc 5 _____S¢. change_ gear(4) 4 — Function of Sposts Cay (sc) Class willl be called [white calling change — —9ear (), 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 « Mistual Function. A__Vistual function is a member function which is _declaved with @ ‘virtual Keyword in _-the base class and yedeclayed_ Covervidden) in a devived class. Athen _you_yefer to 4 object of devived class using —peinter_to a_base class ise. cen calla _viyrual function of tat object an execute tne deyived cigss'S version of _ _tne Function. __ They ave used te achieve Run time npnism « Viytual Function cannot be. _ static and “also “cannot_be friend function of another class. Compi e-+ime Céanly bind pinding )- Vs - ‘Runtime ( Late Binding ) Class base { Public + {cou ee ___Noid show (_) _ Class derived t Public + Void pxint C) — Lcour print); ——_// Run time bety > Show), //_ Compile tim | derived print __//_Late Binding J Base show fun __// Early binding _ As duving compiles time bpty behaviour judge on the bases of which class [t belong, 40. bpty yvepresent pase class. — a Tf tne function is not vivtual -then it with allow binding at compiler time and pyint — fun of base class will ger binded because ____._bptry__yepresent base class - —__—__|_ _ Gur at _xun time bpte points to the object of class devived , So jt willl bind function of deyxived at yun time. _ piley itself dos es two things: 4A Virtual pointer (vPTR) is _cyeated every time Object Is _cyeated fov that class which contains virtual function. 2. _|Trrespective of Object i¢ cyeated oy net, _ Staric ayvay of pointer called (VTABLE _ where each cell point to each virtual _ Function is cyeated , in hase class and derived class. — duzing Eaxly bindin VTABLE for pase class VIABLE FOY “desived class - irtual Function ~and abstvact Class _ Sometimes imptementation of all function 7 cannot be pyvovide in the base class Such a lass js Caled abstwact class. ATUL Koma (ukepin)- MOTE S Gacrery (TELEG Rang) > ( But there can't be any imple menta ~tion of dyaw C) fun in the _ base class) => A_ puve virtual Function In ctt is a Virtual 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 nas ‘at _leact one __pure vivtual function - : We cannot declaye object of abstract class . Exe eeayertec til! snow eyvoy . oe le can nave peintey ox yefevence of Qe iabstvact class. _ =——_- 3-_wle_can_access -the other Function< except _ tee deni ove of its deylved class. —4- {Tf we don't override tne pure virtual _ Function _in_devived class tnen it becomes —___|abstvact- a : — 5: |An abstract class can nave Constyuctors, (Read From GrG ) => [Template in crt template’ check (a, xb ) t if Carb ) __yeturn a, —__{It just help In data type. So that we can write Generic function that can be used fov = different data type. 7 Class qeeks £ Public: ones void fun() { P=newchayl6] >} x int main () geeks 9

You might also like