CoolInterview.com - World's Largest Collection of Interview Questions
Home| Ask Questions| About Us| Feedback| Contact Us|
 Interview Questions  
 Our Services  

Get 9,000 Interview Questions & Answers in an eBook.


  • 9500+ Pages
  • 9000 Question & Answers
  • All Tech. Categories
  • 14 MB Content

    Get it now !!



    Send your Resume to 6000 Companies

  • COOLINTERVIEW.COM LANGUAGES C++ DETAILS



    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.

    I think above answer is not appropriate
    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?

     Posted by: sachin    

    Contact Author  Contact Author
    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

     Posted by: rinki chauhdary    

    Contact Author  Contact Author
    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.
    Example:
    class Temp
    {
    private:
    ~Temp() { };
    friend class FinalClass;
    };

    class FinalClass : virtual public Temp
    {
    . . .
    };

    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.

     Posted by: Kaustuv Chatterjee    

    Contact Author  Contact Author
    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.

     Posted by: Arvind Singh    

    Contact Author  Contact Author
    friend class and virtual inheritence did not pass the test (I can explain why)
    Solution:
    class Final
    {
    private:
    Final(){}
    public:
    static Final* GetFinal()
    {
    return new Final();
    }
    };

    any attampt to inherit from this class will fail
    to create an object Final* f = Final::GetFinal();

     Posted by: Tornike Kobachishvili    

    Contact Author  Contact Author

    © CoolInterview.com
    Today's Special: Get Free Magazine from SAP now!

    A D V E R T I S E M E N T








    If you have the better answer, then send it to us. We will display your answer after the approval.
    Name :*
    Email Id :*
    Answer :*
    Verification Code Code Image - Please contact webmaster if you have problems seeing this image code Not readable? Load New Code
    Process Verification  Enter the above shown code:*
    Inform me about updated answers to this question

       


    Rate the above answer. Help us to know about the answer.
    Category C++ Interview Questions
    Rating (5.0) By 1 users
    Added 8/18/2006
    Views 4143
    Rate it!
    Free Offers 300 Free Magazines for you. No credit card required. No Shipping Charges. Absolutely Free for 1 Year.

    Please Note: We keep on updating better answers to this site. Subscribe to our newsletter to get notified when better answer is posted.

    Notify me when better answer is posted!
    Email:

    View ALL C++ Interview Questions


    Sponsored Links  

    Google Search

    Google

    CoolInterview.com is a part of Vyom Network.
    Copyright ©2003-2009 Vyom Technosoft Pvt. Ltd., All Rights Reserved. Privacy Policy
    Page URL: http://www.coolinterview.com/interview/10842/Default.asp?cachecommand=bypass


    Download Yahoo Messenger | Placement Papers| FREE SMS | ASP .Net Tutorial

    0.94