GeekInterview.com
  I am new, Sign me up!
 
GeekInterview.com  >  Interview Questions  >  Programming  >  C++
Go To First  |  Previous Question  |  Next Question 
 C++  |  Question 94 of 203    Print  
Is there any way to write a class such that no class can be inherited from it. Please include code

  
Total Answers and Comments: 11 Last Update: July 31, 2009     Asked by: khurram123 
  
 Sponsored Links

 
 Best Rated Answer
Submitted by: Abubucker
 

Please look into the link below

http://www.codeproject.com/cpp/finalclass.asp



Above answer was rated as good by the following members:
zeal_goswami
  Sorting Options  
  Page 1 of 2   « First    1    2    >     Last »  
August 11, 2006 19:45:55   #1  
big shrimp        

RE: Is there any way to write a class such that no cla...
Simple make all constructors of the class private.
 
Is this answer useful? Yes | No
August 31, 2006 06:27:33   #2  
Ronny        

RE: Is there any way to write a class such that no cla...
Yes. Right.
If you make the constructors private you cannot use this class at all!

 
Is this answer useful? Yes | No
September 05, 2006 05:06:47   #3  
Amritanshu Agrawal        

RE: Is there any way to write a class such that no cla...

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

}


 
Is this answer useful? Yes | No
September 15, 2006 05:26:55   #4  
Paulson Paul Chambakottukudyil        

RE: Is there any way to write a class such that no cla...

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;

}


 
Is this answer useful? Yes | No
November 25, 2006 05:19:53   #5  
Abubucker        

RE: Is there any way to write a class such that no cla...

Please look into the link below

http://www.codeproject.com/cpp/finalclass.asp


 
Is this answer useful? Yes | NoAnswer is useful 1   Answer is not useful 0Overall Rating: +1    
June 10, 2007 02:50:17   #6  
Pradeep Kamath        

RE: Is there any way to write a class such that no cla...
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()
{
}
};



 
Is this answer useful? Yes | No
July 22, 2007 11:40:15   #7  
Indrajit        

RE: Is there any way to write a class such that no cla...
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

 
Is this answer useful? Yes | No
October 26, 2007 11:53:22   #8  
saurabh khandelwal        

RE: Is there any way to write a class such that no cla...
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.

 
Is this answer useful? Yes | No
December 01, 2007 18:03:51   #9  
berezleon Member Since: December 2007   Contribution: 6    

RE: Is there any way to write a class such that no class can be inherited from it. Please include code
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
 
Is this answer useful? Yes | No
July 31, 2009 13:41:48   #10  
yzesong Member Since: July 2009   Contribution: 20    

RE: Is there any way to write a class such that no class can be inherited from it. Please include code
That is right. Use final class.
 
Is this answer useful? Yes | No
  Page 1 of 2   « First    1    2    >     Last »  


 
Go To Top


 Sponsored Links

 
About Us -  Privacy Policy -  Terms and Conditions -  Contact -  Ask Question -  Propose Category -  Site Updates 

Copyright © 2005 - 2009 GeekInterview.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape