Question:
Is there any way to write a class such that no class can be inherited from it. Please include code
Answer:
Simple, make all constructors of the class private. Source: CoolInterview.com
I think above answer is not appropriate<br>If you make all constructor private u cant create the obj of that class but that class can be inherited in to another class and methods of that base class can be accessed by using derived class object. Isn't it? Source: CoolInterview.com
Answered by: sachin | Date:
| Contact sachin
its very simple in c++ aswe can do it by making the use of FINAL keyword ,becoz FINAl Class can nt be inherited by any other class Source: CoolInterview.com
Answered by: rinki chauhdary | Date:
| Contact rinki chauhdary
Before creating a non-inheritable class crate a temorary class whose constructor or destructors are private.Then make your no-inheritable class as friend to this temporary class.Now make the temopoaryu class as virtual base class of your non-inheritable class.<br>Example:<br>class Temp<br>{<br>private:<br> ~Temp() { };<br> friend class FinalClass;<br>};<br><br>class FinalClass : virtual public Temp<br>{<br>. . .<br>};<br> <br>We inherit our FinalClass virtually from Temp class and make Temp as a virtual base class. Now whenever anyone attempts to inherit class from FinalClass and make object of it, then its constructor tries to call the constructor of Temp. But the constructor of Temp is private so compiler complains about this and it gives error during the compilation, because your derived class is not friend of Temp. Remember friendship is not inherited in the derived class. After all your parent's friends are not your friends and your friends are not friends of your children. But when you create object of FinalClass then its constructor can call the Temp’s constructor because FinalClass is friend of Temp. Source: CoolInterview.com
Answered by: Kaustuv Chatterjee | Date:
| Contact Kaustuv Chatterjee
You can make a claas an abstract class which should contain at least one pure virtual function. So that you can't instanciate that class. Source: CoolInterview.com
Answered by: Arvind Singh | Date:
| Contact Arvind Singh
friend class and virtual inheritence did not pass the test (I can explain why)<br>Solution:<br>class Final<br>{<br>private:<br> Final(){}<br>public:<br> static Final* GetFinal()<br> {<br> return new Final();<br> }<br>};<br><br>any attampt to inherit from this class will fail<br>to create an object Final* f = Final::GetFinal(); Source: CoolInterview.com
Answered by: Tornike Kobachishvili | Date:
| Contact Tornike Kobachishvili
Just make constructor, destructor, copy constructor, assignment operator private. so that no other class can inherit.<br>class Base {<br>private:<br>Base() { }<br>~Base() { }<br>Base & operator=(const Base&){}<br>Base(const Base&) { }<br>};<br> Source: CoolInterview.com
Answered by: Vedulla Raghuveer | Date: 11/6/2009
| Contact Vedulla Raghuveer
It is possible in JAVA by using final but not in C++. Inheriting a class and instantiating a class these two are different. Source: CoolInterview.com
Answered by: Subrat | Date: 2/6/2010
| Contact Subrat
All the given above solutions(C++ specifically) are WRONG....<br>They don't work.... Source: CoolInterview.com
Answered by: Johnny | Date: 3/27/2010
| Contact Johnny
Class singleton<br>{<br>static int count;<br><br>singleton()<br>{<br>if(count==1)<br>exit(0);<br>count++<br>}<br>};<br><br>main()<br>{<br>singleton o,o1;<br>} Source: CoolInterview.com
Answered by: Ramakrishna | Date: 6/16/2010
| Contact Ramakrishna
If you have the better answer, then send it to us. We will display your answer after the approval.
Rules to Post Answers in CoolInterview.com:-
- There should not be any Spelling Mistakes.
- There should not be any Gramatical Errors.
- Answers must not contain any bad words.
- Answers should not be the repeat of same answer, already approved.
- Answer should be complete in itself.
|