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

List the special characteristics of constructor.

722


Can comments be nested?

619


What is an incomplete type in c++?

762


write a function signature with various number of parameters.

557


How the virtual functions maintain the call up?

604






What is a c++ object?

613


What is jump statement in C++?

613


What are c++ storage classes?

614


What is the use of map in c++?

600


How new/delete differs from malloc()/free?

610


What is c++ in english?

572


Explain about Virtual Function in C++?

605


Is c++ still being used?

559


What is the output of the following program? Why?

618


What are exceptions c++?

588