In java a final class is a class that cannot be derived. How
can you make a similar class in C++
Answer / cpp master
Using virtual base classes. The most derived class has to
initialize all the virtual base class in the inheritance
hierarchy. To make such a class simply create a empty class
with a private constructor and mark the class to be made
non derivable as the friend of that class. Now simply
public virtually derive the non derivable with the empty
class. Below is the example code:
class UnDerivable;
class dummy{
private:
dummy(){}
friend class UnDerivable;
};
class UnDerivable: virtual public dummy
{
};
//try deriving fro the underivable class
class deriveUnderivable:public UnDerivable
{
};
int main()
{
UnDerivable ud;
deriveUnderivable uud; //will give an error
return 0;
}
Is This Answer Correct ? | 1 Yes | 0 No |
Why was c++ created?
What is a far pointer? where we use it?
How do you write a function that can reverse a linked-list?
Evaluate: int fn(int v) { if(v==1 || v==0) return 1; if(v%2==0) return fn(v/2)+2; else return fn(v-1)+3; } for fn(7); a) 10 b) 11 c) 1
What is ios flag in c++?
what is a class? Explain with an example.
How a modifier is similar to mutator?
When should you use multiple inheritance?
What are the syntactic rules to be avoid ambiguity in multiple inheritance?
What is std :: flush?
What doescout<<(0==0) print out a) 0 b) 1 c) Compiler error: Lvalue required
what is oops and list its features in c++?