473,490 Members | 2,592 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Regarding Abstract Class

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
Sep 23 '08 #1
4 2040
On Tue, 23 Sep 2008 07:34:09 -0700, Pranav wrote:
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,
An abstract class is rather by design a class that contains
pure virtual functions, hence no hiding is necessary.

class abstract {
public:
virtual void foo() = 0;
};

--
OU
Remember 18th of June 2008, Democracy died that afternoon.
http://frapedia.se/wiki/Information_in_English
Sep 23 '08 #2
On Sep 23, 7:34*am, Pranav <pranav...@gmail.comwrote:
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.
Sep 23 '08 #3
On Sep 23, 9:26 pm, diamondback <christopher....@gmail.comwrote:
On Sep 23, 7:34 am, Pranav <pranav...@gmail.comwrote:
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.

Thank You diamondback & Obnoxious User, I got it..,
Sep 24 '08 #4
On Sep 23, 4:34 pm, Pranav <pranav...@gmail.comwrote:
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.)

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Sep 24 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
1471
by: He Shiming | last post by:
Hi, I've got a question regarding class inheritance. The following code reproduces the problem I'm dealing with: class IBase { public: virtual void Method(void)=0; };
2
9587
by: Dave Veeneman | last post by:
Is is legal to declare abstract members in non-abstract classes? How about non-abstract members in abstract classes? I am writing a base class with three derived classes. The base class will...
3
1730
by: Anders Borum | last post by:
Hello Jon, et all. I am working on a framework with context bound objects (models). The objects expose common functionality, such as the ability to get a serialized Xml representation of an...
6
5788
by: Dan Sikorsky | last post by:
If we were to define all abstract methods in an abstract class, thereby making that class non-abstract, and then override the heretofore 'abstract' methods in a derived class, wouldn't that remove...
7
4452
by: jason | last post by:
In the microsoft starter kit Time Tracker application, the data access layer code consist of three cs files. DataAccessHelper.cs DataAcess.cs SQLDataAccessLayer.cs DataAcccessHelper appears...
0
2661
by: mailforpr | last post by:
Hi. Let me introduce an iterator to you, the so-called "Abstract Iterator" I developed the other day. I actually have no idea if there's another "Abstract Iterator" out there, as I have never...
0
2808
by: emin.shopper | last post by:
I had a need recently to check if my subclasses properly implemented the desired interface and wished that I could use something like an abstract base class in python. After reading up on metaclass...
4
6556
by: David Zha0 | last post by:
Hi, "when we call a virtual method, the runtime will check the instance who called the method and then choose the suitable override method, this may causes the performance drop down", is this...
6
4007
by: Miguel Guedes | last post by:
Hello, I recently read an interview with Bjarne Stroustrup in which he says that pure abstract classes should *not* contain any data. However, I have found that at times situations are when it...
0
7112
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6974
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7146
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7356
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5448
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4878
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3084
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3074
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1389
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.