In java a final class is a class that cannot be derived. How
can you make a similar class in C++
Answer Posted / 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 View All Answers
Why we use #include iostream in c++?
What is the use of 'this' pointer?
What is == in programming?
Is c++ a programming language?
What is difference between class and structure in c++?
What do you mean by static variables?
Can I learn c++ in a week?
How can you specify a class in C++?
What is a class template in c++?
Which is best c++ or java?
What is the word you will use when defining a function in base class to allow this function to be a polimorphic function?
How would you use the functions randomize() and random()?
Does c++ cost money?
How long it will take to learn c++?
How can I disable the "echo" feature?