Answered step by step
Verified Expert Solution
Question
1 Approved Answer
write this code in C++ I need this answer complete. Q.No2: a) How can I set up my class so it won't be inherited from?
write this code in C++
Q.No2: a) How can I set up my class so it won't be inherited from? Just declare the class final. But also ask yourself why you want to? There are two common answers: - For efficiency: to avoid your function calls being virtual. - For safety: to ensure that your class is not used as a base class (for example, to be sure that you can copy objects without fear of slicing). In today's usual implementations, calling a virtual function entails fetching the "vptr" (i.e. the pointer to the virtual table) from the object, indexing into it via a constant, and calling the function indirectly via the pointer to function found at that location. A regular call is most often a direct call to a literal address. Although a virtual call seems to be a lot more work, the right way to judge costs is in comparison to the work actually carried by the function. If that work is significant, the cost of the call itself is negligible by comparison and often cannot be measured. If, however, the function body is simple (i.e. an accessor or a forward), the cost of a virtual call can be measurable and sometimes significant. The virtual function call mechanism is typically used only when calling through a pointer or a reference. When calling a function directly for a named object (e.g. one allocated on the stack of the caller), the compiler inserts code for a regular call. Note, however, that frequent such use may indicate other problems with the design - virtual functions work only in tandem with polymorphism and indirect use (pointers and references). Such cases may warrant a design review for overuse of virtual. b) How can I set up my member function so it won't be overridden in a derived class? Just declare the function final I need this answer complete.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started