Can I stop people deriving from my class?

Questions by mehulgala177   answers by mehulgala177

Showing Answers 1 - 15 of 15 Answers

jmj786

  • Sep 23rd, 2011
 

You can make your class as seal even if you want stop overriding any method inside your class just make that method as seal.

Enjoy...!

  Was this answer useful?  Yes

Kali

  • Oct 27th, 2011
 

Yes you can stop deriving the your class from other classes by using the key word 'final'. It does not allow inheritance concept.

ex:

final class Demo{
................
}

  Was this answer useful?  Yes

LordAlex

  • Nov 14th, 2011
 

No.
In Classic C++ you cannot do that.
It is possiible in Microsoft C++/CLI .NET only, but not in Visual C++.
C++/CLI has "seal","abstract" key words mentioned in above incorret answers.

  Was this answer useful?  Yes

Amit

  • Jan 6th, 2012
 

If you make all the constructor and assignment operator private of a class then logically you can prevent any other class to use your base class.

Even your derived class can inherit the base class (that have every thing private).

But you can not instanciate your derived class, so there is no meaning of inheriting a base class that have every thing private(constructor and assignment operator).

Zubair

  • Jan 18th, 2012
 

Yea It is possible in C++. Here is the code snippet.


Code
  1. using namespace std;

  2. class Sample;

  3.  

  4.  

  5. class SampleBase

  6. {

  7.         friend class Sample;

  8. public:

  9. private:

  10.         SampleBase()

  11.         {

  12.         }

  13.         SampleBase(const Sample &sam)

  14.         {

  15.         }

  16.         SampleBase & operator =(const SampleBase &sam)

  17.         {

  18.         }

  19. };

  20.  

  21. class Sample : public virtual SampleBase

  22. {

  23. public:

  24.         Sample():SampleBase()

  25.         {

  26.         }

  27.  

  28. };

  29.  

  30. class Derived: public Sample

  31. {

  32. };

  33.  

  34. int main(int argc, char* argv[])

  35. {

  36.         Sample sma; // Works

  37.         Derived d;  // Error

  38.        

  39.         system("PAUSE");

  40.         return 0;

  41. }

  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