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 |
Differentiate between an inspector and a mutator ?
What is array in c++ example?
What are the uses of c++ in the real world?
What is the use of cmath in c++?
What is the difference between delegation and implemented-in-terms-of?
What is std namespace in c++?
What is the main purpose of c++?
What is abstraction in c++?
How can you quickly find the number of elements stored in a dynamic array? Why is it difficult to store linked list in an array?
write a porgram in c++ that reads an integer and print the biggest digit in the number
Write a corrected statement in c++ so that the statement will work properly. if (x = y) x = 2*z;
Show the application of a dynamic array with the help of an example.