Regarding Abstract Class

P

Pranav

Hello All,

I have a simple question regarding the definition of abstract class,
IIRC , Abstract class is one which contains virtual function
declaration and other variables and no object of this class is created
directly.
If this is the case why don't we hide the constructor of abstract
class into protected region? So that only object which inherits it can
call the ctor of abstract class. In this way no one can create the
object of abstract class independently.

Correct Me If I am wrong some where,

Thank You,
Pranav
 
D

diamondback

Hello All,

  I have a simple question regarding the definition of abstract class,
IIRC , Abstract class is one which contains virtual function
declaration and other variables and no object of this class is created
directly.
  If this is the case why don't we hide the constructor of abstract
class into protected region? So that only object which inherits it can
call the ctor of abstract class. In this way no one can create the
object of abstract class independently.

Correct Me If I am wrong some where,

Thank You,
Pranav

This is a great question. The answer is, you can, but you do not have
to, declare the constructor protected. I am sure you know that we
programmers are inherently lazy and really prefer to type as little as
possible. The nature of an abstract class is that it declares at least
one function as a pure virtual. For example:

virtual void PureVirtualFunction( int parameter ) = 0;

This syntax ensures that the compiler will not allow the class to be
instantiated directly. So, there is generally no need to protect the
constructor. In fact, in a lot of cases, there is no need to even
declare the constructor. The default constructor is fine. However, if
you want to be completely explicit and thorough, you can indeed
declare a protected constructor. But, remember that this will prevent
the automatic creation of the default constructor so be sure this is
what you want to do. Also, if you feel it necessary to declare the
default constructor, you probably want to declare (and define?) the
copy constructor and the assignment operator. And, be sure to
explicitly call these constructors/operators in your derived classes.

I hope that helps.
 
P

Pranav

This is a great question. The answer is, you can, but you do not have
to, declare the constructor protected. I am sure you know that we
programmers are inherently lazy and really prefer to type as little as
possible. The nature of an abstract class is that it declares at least
one function as a pure virtual. For example:

virtual void PureVirtualFunction( int parameter ) = 0;

This syntax ensures that the compiler will not allow the class to be
instantiated directly. So, there is generally no need to protect the
constructor. In fact, in a lot of cases, there is no need to even
declare the constructor. The default constructor is fine. However, if
you want to be completely explicit and thorough, you can indeed
declare a protected constructor. But, remember that this will prevent
the automatic creation of the default constructor so be sure this is
what you want to do. Also, if you feel it necessary to declare the
default constructor, you probably want to declare (and define?) the
copy constructor and the assignment operator. And, be sure to
explicitly call these constructors/operators in your derived classes.

I hope that helps.


Thank You diamondback & Obnoxious User, I got it..,
 
J

James Kanze

I have a simple question regarding the definition of abstract
class, IIRC , Abstract class is one which contains virtual
function declaration and other variables and no object of this
class is created directly.

An abstract class is one which has at least one pure virtual
function. Most of the time, it will have more than one virtual
function, and no data, but these aren't formal requirements.
If this is the case why don't we hide the constructor of
abstract class into protected region?

We do, if we have to provide a constructor. Since most abstract
classes have no data, it's frequent not to bother declaring a
constructor at all. Which means that the compiler will generate
a public one. But since the class is abstract, the compiler
won't let you create an instance anyway, it doesn't matter.

In the cases where you have an interface in which none of the
functions are pure virtual, it is usual to declare and define a
protected constructor, even if it is empty. (Such cases aren't
frequent, but sometimes occur in event notifiers and such, where
the interface provides a default implementation which ignores
the event, so client code need only override the functions for
the events it's actually interested in.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,812
Latest member
GracielaWa

Latest Threads

Top