CoolInterview.com - World's Largest Collection of Interview Questions
 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


  • INTERVIEW QUESTIONS LANGUAGES C++ DETAILS
    Question :
    Is there any way to write a class such that no class can be inherited from it. Please include code


    Category C++ Interview Questions
    Rating (5.0) By 1 users
    Added on 8/18/2006
    Views 4325
    Rate it!
    Answers:

    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 sachin  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



     Posted by: rinki chauhdary    

    Contact rinki chauhdary  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.
    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 Kaustuv Chatterjee  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.



     Posted by: Arvind Singh    

    Contact Arvind Singh  Contact Arvind Singh

    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 Tornike Kobachishvili  Contact Tornike Kobachishvili


    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

       
    Related Questions
    View Answer
    What is virtual constructors/destructors?


    View Answer
    What do you mean by pure virtual functions?


    View Answer
    What is namespace?


    View Answer
    What is RTTI?


    View Answer
    What is a template?


    View Answer
    What do you mean by inline function?


    View Answer
    What is virtual class and friend class?
    View Answer
    What is function overloading and operator overloading?


    View Answer
    Difference between realloc() and free()?


    View Answer
    What do you mean by binding of data and functions?


    View Answer

    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

    User Options
    Sponsored Links


    Copyright ©2003-2009 CoolInterview.com, All Rights Reserved.
    Privacy Policy | Terms and Conditions
    Page URL: http://www.coolinterview.com/interview/10842/default.asp?cachecommand=bypass


    Download Yahoo Messenger | Placement Papers| FREE SMS | ASP .Net Tutorial | Web Hosting | Dedicated Servers | Joke of the Day

    0.48