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 |
Who made c++?
Explain the term memory alignment?
Can inline functions have a recursion? Give the reason?
Design a program to input a date from user in the form day/month/year (e.g. 2/6/2000) and report whether it’s a valid date or not. The program should take account of leap years. You will need to know that a leap year is a year that is exactly divisible by 4, except that century years are only leap years if they are divisible by 400.
How do you print a string on the printer?
What is the difference between global int and static int declaration?
How can you tell what shell you are running on unix system?
How much maximum can you allocate in a single call to malloc()?
In java a final class is a class that cannot be derived. How can you make a similar class in C++
What does the nocreate and noreplace flag ensure when they are used for opening a file?
What does I ++ mean in c++?
How c functions prevents rework and therefore saves the programers time as wel as length of the code ?