Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C++ and Visual C++ > C++ Programming
|
C++ Programming General discussions for the C++ language. For questions specific to Microsoft's Visual C++ variant, see the Visual C++ forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C++ Programming section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old May 19th, 2006, 02:43 AM
Registered User
 
Join Date: Jan 2006
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default Why abstract class can't be instantiated?

Hi,
Why an abstract class can't be instantiated? Want to know the exact reason for this.
Thanks & Regards,
Anisur

Reply With Quote
  #2 (permalink)  
Old May 21st, 2006, 01:44 AM
Authorized User
 
Join Date: Mar 2005
Posts: 58
Thanks: 0
Thanked 1 Time in 1 Post
Send a message via MSN to Alan-LB Send a message via Yahoo to Alan-LB
Default

Give us an example of what you mean. Whar exactly are you trying to do?

Alan

Reply With Quote
  #3 (permalink)  
Old May 30th, 2008, 08:25 AM
Registered User
 
Join Date: May 2008
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Alan, I think this is plain english....poor chap just wanna know what goes behind the scene so that we cannot instantiate an abstract class......
Reply With Quote
  #4 (permalink)  
Old July 12th, 2008, 12:00 AM
Authorized User
 
Join Date: Jan 2008
Posts: 24
Thanks: 0
Thanked 0 Times in 0 Posts
Default

because it is a class that is specifically designed to be only derived from.that class has pure virtual functions that must be overriden by the class inherits abstract class.instantiating a class that has pure virtual functions is pointless and might cause compiler errors i guess.for example think of a Bird abstract class that has many pure virtual functions like fly() that have to be defined by the inherting bird kind lets say Sparrow.If you could instantinate Bird class lets say Bird mybird then mybird::fly() and fly function is undefined that would not be logical.besides can you show me a bird that is only a bird? no you can not do that.so instantiating a bird class is not logical.i am not an expert but these are the reasons i can think of.

Reply With Quote
  #5 (permalink)  
Old July 19th, 2008, 09:06 PM
Authorized User
 
Join Date: May 2007
Posts: 28
Thanks: 0
Thanked 1 Time in 1 Post
Send a message via MSN to Peter_APIIT
Default

But even when the abstract class provides definitions for each of its
pure virtual functions, the class still cannot be directly
instantiated. There really is no compelling technical reason why
abstract classes could not be instantiated; after the language rules
could simply be adjusted and have the compiler provide an empty
definition for a pure virtual function - in much the same way that the
compiler provides a default constructor when needed. Instead, the
prohibition against instantiating an abstract class is a deliberate
feature of the language. An abstract class cannot be instantiated
because the definition of an abstract class is one that cannot be
instantiated.

In C++, abstract classes help keep separate class interface from
implementation. An abstract class need specify only an interface.
Because it cannot be instantiated, it has no need no provide any
implementation of its own. Instead the pure virtual mechanism forces
subclasses to provide part or all of the implementation. By keeping the
two separate, the resulting code is more portable, has few dependencies
and is overall "cleaner" than one in which the interface and
implementation are tightly intertwined.

You can create pointer or reference to abstract class but not object.

Linux is the best OS in the world.
Reply With Quote
  #6 (permalink)  
Old July 23rd, 2008, 01:32 AM
Authorized User
 
Join Date: Jan 2008
Posts: 24
Thanks: 0
Thanked 0 Times in 0 Posts
Default

you may be able to create a pointer to abstract class to use polymorphism but you can not create reference to abstract class because it is not an object..and you can not reference something that is not a variable or object as far as i know..correct me if i am wrong.

Reply With Quote





Similar Threads
Thread Thread Starter Forum Replies Last Post
A question about abstract class nf0001384 C# 2005 5 July 22nd, 2013 06:58 AM
interface and abstract class sreenu.pocha C# 2 July 26th, 2006 07:18 AM
question about abstract class/polymorphism csharplearner C# 4 October 11th, 2005 03:27 PM
Class Can't be instantiated enderjs J2EE 0 September 29th, 2004 10:01 AM
Class can't be instantiated? enderjs XML 1 September 29th, 2004 02:05 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.