Oops Class 03 Homework Notes

You might also like

Download as pdf
Download as pdf
You are on page 1of 13
Object Oriented Programming Class 03 Homework | 1. Virtual CTOR Vs Virtual DTOR What 1s virtual in Co? Nay to achieve Runtine polymorphism. vinta! foncsion Figv ounce at Run TIM Bird Parent/sase/super class virtual fly() = @ sleep() [ extnas Sparrow Chitd/Dertved/sub class [ea700) 1. Formed for every class having at Least one virtual. function and for its derived classes. 2. It is static arrays, hence one instance for a class. 3. Wet (a hidden menber pointer) is added by compiler to classes with virtual and its derived classes. 4. Depending upon the object type YPtr is bonded to a Vtable (static array/table class name/virtual table/function Pointer table). 2, Wrebles are created at compile tine. 3. When object of @ particular type is created at runtime. There will be a YPtr which will be initialized to point to a static Viable at the tine of construction. find + en Sparen Ot setae: vom points to MABLE Sprrou Gem pones to vaaue cope because Sparrow constructor called now because Eagle constructor called now ar VIABLE Bird will not create because Bird CTOR did not called in this case virtual fly() = 0; sleep() VATABLE Sparrow VATABLE Eagle 7) sleep() * * Job class ka constructor call hota hal uske bad hi VIABLE menory me compiler ke dvare banaya jata hat and iske baad VPTR VTABLE ko point kama start harta hat. virtual cron? 2. Constructor cannot be virtual, becouse when constructor of a class is executed there is no virtual table in the menory) ineans no virtual pointer defined yet. So, the constructor should always be non-virtual 3. A virtual call is a mechanism to get work done given partial information. In particutar, “virtual” aLtoxs us to call a function knowing only any interfaces and not the exact type of the object, To ereate an object you need complete information In particular, you need to know the exact type of what you want to create, J Consequently, @ “call to a constructor” cannot be virtual. yo tas) CIDR Gut wot Cake Bind Fb eww BidO7 (Can we make virtual CTOR? 1. YES: 2. It is important to handle proper destruction of Derived class Did wot caw grad ourpur: are C wsase CTOR Called © berived CTOR Called wDerived DTOR Called Caase DTOR Called wease CTOR Called “Abstraction in C+ 1. Delivering only essential information to the outer world while masking the background details. 2. It is a design and programming method that separates the interface from the inpLementation. 3. Real Life e.g., various functionalities of AinPods but don't know the actual implenentation/working. Example: To drive a car, one only needs to know the driving process and not the mechar 1. Function's implementation is hidden in header files 2. We could use the same progran without knowing its inside working 3. E.ges Sort), fon example, is used to sort an array, a List, on a collection of items, and we know ‘that if we give a container to sort, it will sort it, but we don't know which sorting algorithm it uses to sort that container 's of the car engine sbi apn ( Syprsruntt Pf te oo conn gw tae WE weton Abstraction using classes? 1. Grouping data menbers and menber functions into classes using access specifiers. 2. A class can choose which data menbers are visible to the outside world and which are hidden. [Abstraction in C++ ex... lacs AbstractionExanple{ private: By making se data i . mementeice LHe ste 1 spc? Paces et ore Y “own eeeable cute he Sas vote getyvtues() ¢ ; coutscrunbers 22! “ccmumc< endl heen wey Coutccrthar ie: "ccehceandls, ‘erves tough > cream > 1. Class that contains at Least one pure virtual function, and these classes cannot be instantiated. 2. It has cone from the idea of Abstraction. virtual votd fun() 1 definition 31 | "Wwe Function Pare Virtual Function + | Aviusttncion eamenbe [pus vital tinction ie a marae fureon apart eas at can be | ueton nape cass, and earn futher dstedin ach cass. |Isgvonin apren cass ara detneg ne nse 7 [Ragan matci at | a dn a tn we 3 | inte osteo, tay mayor may |The cd clases mir donee pre Iota te veal nen’ | vn ht, “> | startin can be ore om the | Kanmotbe intro sf Deore Poise in avira unten, | wr abaras case peri cis, pores. virtual votd fun() = 05 only virtual function pure virtual function % avstract class is also hnow as interface class. oureur: Design Stra 1. Abstraction divides code into tio cotegories: interface and implementation, So, when creating your component, ‘hep the interface separate from the implementation so that if the underlying implementation changes, the interface stays the same. 2. In thts instance, any program that uses these interfaces would remain unaffected and would require reconpitation with the most recent implementation. ‘an abstract cas laos Tot ‘ oma members of nes virtual void show) =; interface implementation ar » | alent oa ae aaa; > » Namal tae pare nace Pi eme omer rae oe ts W [AP 3. Inline Function in C+4] 1. An inline function is a regular function that is defined by the inline keyword. 2. The code for an inline function is inserted directly into the code of the calling function by compiler While compiling, which can result in faster execution and Less overhead conpared to regular function calls. 3. Instead of calling function the statements of functions are pasted in calLing function. 4, Used with small stzed functions. So that executables are small (handled automatically by compiler optimisation levels). iaeiedidhieeintaasht ice seibiieiainelibcnls a) Less overhead to regular function because ga coe dizpalytlun() 13 not Loading tn call stack tnt aainc) compotion | 388 HN) i al ons cat sae [Regular Function working process overhead to regular function because () is Loading in call stack ——— ® [wisi ®t] ink S Y [rina P aa main aan Coll Stack 7 Loi Stack Call Sek

You might also like