constructors in an abstract class

T

Tony Johansson

Hello!!

Assume you have two classes one class called Base which is an abstract class
and one
derived class called Derived.

You are not allowed to create an object of class Base like new Base(); or
just Base base;.
But when you instansiate an object of class Derived the constructor in Base
is called implicitly.

Now to my question is it any point to have a constructors in the abstract
class in my example Base.

Many thanks.

//Tony
 
V

Victor Bazarov

Tony said:
Assume you have two classes one class called Base which is an abstract class
and one
derived class called Derived.

You are not allowed to create an object of class Base like new Base(); or
just Base base;.
But when you instansiate an object of class Derived the constructor in Base
is called implicitly.

Now to my question is it any point to have a constructors in the abstract
class in my example Base.

The fact that Base cannot be instantiated by itself and whether it needs
its own c-tor are unrelated. If you have some data members in your Base
class that need to be initialised in a certain way, the only method you
have is to provide a proper constructor. Even if your Base doesn't have
any data members but needs to do something when it is instantiated (like
notify some third party about it), you still need to have a constructor.

If, by some chance, your Base class doesn't need to do anything when it is
instantiated as part of some other object (of a derived class), then the
default (compiler-provided) constructor would undoubtedly be sufficient.

V
 

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,774
Messages
2,569,598
Members
45,149
Latest member
Vinay Kumar Nevatia0
Top