In java a final class is a class that cannot be derived. How
can you make a similar class in C++



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

Post New Answer

More C++ General Interview Questions

Why was c++ created?

0 Answers  


What is a far pointer? where we use it?

0 Answers  


How do you write a function that can reverse a linked-list?

0 Answers  


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

4 Answers   Quark,


What is ios flag in c++?

0 Answers  






what is a class? Explain with an example.

0 Answers  


How a modifier is similar to mutator?

0 Answers  


When should you use multiple inheritance?

2 Answers  


What are the syntactic rules to be avoid ambiguity in multiple inheritance?

0 Answers  


What is std :: flush?

0 Answers  


What doescout<<(0==0) print out a) 0 b) 1 c) Compiler error: Lvalue required

0 Answers  


what is oops and list its features in c++?

0 Answers  


Categories