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 |
Do inline functions improve performance?
What is dev c++ used for?
What does new in c++ do?
How many ways are there to initialize an int with a constant?
What is the best sorting algorithm, when there is a large amount of data, that cannot be fit in the main memory. ?
What programming language should I learn first?
What are the 4 types of library?
Do you know about C++ 11 standard?
0 Answers Agilent, ZS Associates,
What is iomanip c++?
Is c++ a high level language?
How much is size of struct having 1 char & 1 integer?
How do you flush std cout?