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 |
Will c++ be replaced?
throw Can constructors exceptions?
What is a forward referencing and when should it be used?
what is a class? Explain with an example.
What happens if an exception is throws from an, object's constructor and object's destructor?
What is name mangling?
reading material is provided 3 books for c++ if u need more do let me know thnx i hve lots of material do let me know if u want it
How do you define a class in c++?
What should main() return in c and c++?
What is the difference between inline functions and macros?
What is the purpose of decltype?
Does there exist any other function which can be used to convert an integer or a float to a string?