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
Give the difference between the type casting and automatic type conversion. Also tell a suitable C++ code to illustrate both.
What relational operators if statements in c++?
What is difference between c++ and c ++ 14?
How the delete operator differs from the delete[]operator?
Explain register storage specifier.
Is there any difference between int [] a and int a [] in c++?
What is near, far and huge pointers? How many bytes are occupied by them?
Am studying basic c++ programming, have been given the following assignment. Design a linear program to calculate the maximum stress a material can withstand given a force and a diameter of a circle. To find the required area pi should be defined. Have most of the program sorted out but am at a loss as to how to show the calculations required. Can anyone help?
Differentiate between the message and method in c++?
What is a float in c++?
Explain storage qualifiers in c++.
Discuss the effects occur, after an exception thrown by a member function is unspecified by an exception specification?
What are the important differences between c++ and java?
What is abstraction c++?
How to defines the function in c++?