In java a final class is a class that cannot be derived. How
can you make a similar class in C++



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

Post New Answer

More C++ General Interview Questions

When do you call copy constructors?

0 Answers  


Is c++ proprietary?

0 Answers  


What are references in c++?

0 Answers  


What is the purpose of extern storage specifier?

0 Answers  


How does list r; differs from list r();?

0 Answers  






In which situation the program terminates before reaching the breakpoint set by the user at the beginning of the mainq method?

0 Answers  


What is a buffer c++?

0 Answers  


What is a flag in c++?

0 Answers  


Given an array of size N in which every number is between 1 and N, determine if there are any duplicates in it. You are allowed to destroy the array if you like. [ I ended up giving about 4 or 5 different solutions for this, each supposedly better than the others ].

1 Answers  


How do you declare A pointer to a function which receives nothing and returns nothing

0 Answers  


Evaluate as true or false: !(1 &&0 || !1) a) True b) False c) Invalid statement

0 Answers  


What programming language should I learn first?

0 Answers  


Categories