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

State the difference between delete and delete[].

0 Answers  


Distinguish between new and malloc and delete and free().

0 Answers  


What is a multimap c++?

0 Answers  


difference between macro and function?

3 Answers  


Is c++ low level?

0 Answers  






Why are pointers used?

0 Answers  


List the types of polymorphism in c++?

0 Answers  


What is the difference between the compiler and the preprocessor?

0 Answers  


What is #include c++?

0 Answers  


What is the default width for ouputting a long integer using the insertion operator?

0 Answers  


Write a C/C++ program that connects to a MySQL server and checks if the InnoDB plug-in is installed on it. If so, your program should print the maximum number of concurrent threads that the InnoDB plug-in can create

0 Answers  


What is meant by a delegate?

0 Answers  


Categories