Is there any way to write a class such that no class can be inherited from it. Please include code

Questions by khurram123

Showing Answers 1 - 25 of 25 Answers

big shrimp

  • Aug 11th, 2006
 

Simple, make all constructors of the class private.

  Was this answer useful?  Yes

Ronny

  • Aug 31st, 2006
 

Yes. Right.
If you make the constructors private, you cannot use this class at all!

  Was this answer useful?  Yes

Amritanshu Agrawal

  • Sep 5th, 2006
 

Hi,

By creating all constructor private we never create any object of that unless untill we provide any static function or friend calss of it. Again if we do so we can only create the object using new(). So have we to manage the memory efficiently. Again Our question was "write a class such that no other class can inherit it" means we can create a object of that class.

Or the other way to your answer is to make the dystructor private. By this way you never create any object of that calss beside creating all constrcutor private.

Again the class

class temp

{

private:

temp();

friend

}

  Was this answer useful?  Yes

Paulson Paul Chambakottukudyil

  • Sep 15th, 2006
 

Unlike java's "final" or .NET's "sealed", C++ doesn't give any inbuilt method to do this. But it can be achieved by some indirect(may be weird ) method. The main disadvantage of this method is that the objects of the class can be created only dynamically. I'm here with attaching the code.

 

class Base

{

        public:

                 static Base* Instantiate()

                  {

                            return (new Base);

                  }

                      //What ever members you want to add

        private:

                     Base()

                     {

                              cout<<endl<<"Object Created Successfully";

                     }

};

//********The following class cause ***error***

class Derived : public Base

{

};

//*********Error causing class ends here***********

int main()

{

              Base *b = NULL;

              b = Base::Instantiate();

              if(b == NULL)

                     cout<<endl<<"Object Creation Failed!";

              return 0;

}

  Was this answer useful?  Yes

Pradeep Kamath

  • Jun 10th, 2007
 

if you want to avoid inheritance for "NoInherit" class (explained in below example).


class NoInherit;

class Cbase
{
public:
        friend  NoInherit;
private:
    Cbase()
    {
    }

};

class NoInherit:virtual public Cbase
{
public:
    NoInherit()
    {
    }

};


class ctest:public NoInherit  //not possible..
{
public:
    ctest()
    {
    }
};


  Was this answer useful?  Yes

Indrajit

  • Jul 22nd, 2007
 

To explains Predeerp's code a bit,

class ctest::public Noinherit is possible but instantiating ctest class is not because ctest::ctest() cannot access CBase::CBase() since a derived class cannot access any private member of any base, whereas Noinherit can access the same being a derived class since Noinherit is friend of CBase.

Thanks/Indrajit

  Was this answer useful?  Yes

saurabh khandelwal

  • Oct 26th, 2007
 

For your kind information
Constructors in the class cannot be declared as private.
The rule for declaring constructor in the class is that it must be declared public.

If constructor is declared private then it is no more a constructor nor a member function.

  Was this answer useful?  Yes

berezleon

  • Dec 1st, 2007
 

Saurabh, I bet you are wrong; definition of ctr is "member function which doesn't return any values and called on object creation". nobody can stop you of declaring ctr private, protected or public --it might be bad design but it's working model. At the last point "Is there a way to write a class such that no class can be inherited from it....." is a very common interview question and trust me answer is "private ctr"

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions