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
Explain binary search.
Why do we need c++?
Can you please explain the difference between overloading and overriding?
What is constructor c++?
What is the use of data hiding?
What happens when the extern "c" char func (char*,waste) executes?
What is the most useful programming language?
Why do we use iterators?
Which is best ide for c++?
Can we use struct in c++?
Why is c++ so fast?
Snake Game: This is normal snake game which you can find in most of the mobiles. You can develop it in Java, C/C++, C# or what ever language you know.
Is java based off c++?
Why do we use classes in c++?
How does com provide language transparency?