Please help me with this entire C++ Homework example. thank you so much 8 Polymo
ID: 3868569 • Letter: P
Question
Please help me with this entire C++ Homework example. thank you so much
8 Polymorphism Homework Note: These were all questions that appeared on a recent final exam. 1. How does polymorphism contribute to the design of object-oriented systems? in the of 2the vptr and vtable implementation virtual . Explain the role of functions. 3. What is a pure virtual function and why would you ever use one? 4. What is the purpose of a virtual destructor? 5. Describe a situation where it makes sense for a class to have a destructor, but no constructor 6Without executing the code below, give the output of the following program. . class Base I public: void f coutExplanation / Answer
Q1. How does polymorphism contribute to the design of object-oriented systems?
Ans.:- Polymorphism simply means occurrence of omething in different forms. In object oriented systems, it is used to access different type of object through the base class. For example, There are three classes P.Q and R. Q and R is derived from the base class P, now a pointer which point to base class P can also point to Q and R class. A same object can be access through multiple forms, this is polymorphism.
Q2. Explain the role of vptr and vtable.in the implementation of virtual functions?
Ans.:- In implementation of virtual function, vptr, is a hidden pointer and it is added to the base class by the compiler andthe virtual table of that particular class is pointed by this pointer. This vptr is inherited to all the derived classes. Every object of a class with virtual functions stores this vptr. Call to a virtual function by an object is resolved by following this hidden _vptr. This vptr used to call the virtual function by an object.
Lookup table of function pointers is Virtual table. At run time Virtual Table is used to dynamically bind the virtual functions to objects. The program can not use it directly and there is no standardised way to access it. Every class which use virtual function has its own virtual table. At compile time, compiler set up this table.
Q3.What is a Pure virtual function and why would you ever use them?
Ans.:- In base class pure virtual function has no definition. Derived class contain its definition which is compulsory for the derived class. These function can be equated to zero. Derived class override the pure virtual function.
Purpose behind using the pure virtual function is to inherit the properties of the derived class, to create a base pointer which is required for runtime poymorphism and also to avoid the overwriting of member function.
Q4. What is the purpose of virtual Destructor?
Ans.:- When a object get destroyed explicitly by applying the delete operator in the base-class pointer to the object, then the base class destructor is called on the object. A simple solution to this problem is to delcare a virtual base- class destructor, by doing this all derived class destructor become virtual beside that they don't have the same name as the base class destructor. when, if all objects are in hierachy get destroyed explicitly by the delete operator in the base class pointer to the derived class object then the destructor is called virtual destructor.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.