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

What is data hiding c++?

0 Answers  


What is difference between class and function?

0 Answers  


Is c or c++ more useful?

0 Answers  


Explain what happens when a pointer is deleted twice?

0 Answers  


Should the this pointer can be used in the constructor?

0 Answers  


How does c++ sort work?

0 Answers  


Write a Program for find and replace a character in a string.

0 Answers  


what is upcasting in C++?

0 Answers  


Will this c++ program execute or not?

0 Answers  


What are the differences between the function prototype and the function defi-nition?

0 Answers  


What are the comments in c++?

0 Answers  


how is returning structurs from functions?Show an eg?

1 Answers   GE,


Categories