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


Please Help Members By Posting Answers For Below Questions

Give the difference between the type casting and automatic type conversion. Also tell a suitable C++ code to illustrate both.

641


What relational operators if statements in c++?

653


What is difference between c++ and c ++ 14?

586


How the delete operator differs from the delete[]operator?

654


Explain register storage specifier.

609






Is there any difference between int [] a and int a [] in c++?

563


What is near, far and huge pointers? How many bytes are occupied by them?

669


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?

1754


Differentiate between the message and method in c++?

618


What is a float in c++?

552


Explain storage qualifiers in c++.

629


Discuss the effects occur, after an exception thrown by a member function is unspecified by an exception specification?

653


What are the important differences between c++ and java?

619


What is abstraction c++?

599


How to defines the function in c++?

630