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
What is c++ hiding?
Can a class be static in c++?
What is the most powerful coding language?
What is std namespace in c++?
What is the use of default constructor?
Explain deep copy?
Why ctype h is used in c++?
What is abstraction in c++?
What is else if syntax?
Evaluate !(1&&1||1&&0) a) Error b) False c) True
What is public, protected, private in c++?
What are mutator methods in c++?
Is c++ the best programming language?
What is a lambda function c++?
What operators can you overload in c++?