What is the Purpose of Constructor in a class?

R

rvinoth83

Hi,

Constructor of a class is only used to Intialize the Instance
variable of class or it will allocate the member and then Intialize
the variable?........

If constructor is used only to Intialize the Instance variable of
the class, What is the need of Constructor in Abstract
class?.............
 
B

benben

Hi,

Constructor of a class is only used to Intialize the Instance
variable of class or it will allocate the member and then Intialize
the variable?........

Initialize members.
If constructor is used only to Intialize the Instance variable of
the class, What is the need of Constructor in Abstract
class?.............

No need. You generally don't write one for abstract class.

Regards,
Ben
 
A

Alf P. Steinbach

* (e-mail address removed):
Hi,

Constructor of a class is only used to Intialize the Instance
variable of class or it will allocate the member and then Intialize
the variable?........

The question is very unclear, seemingly self-contradictory.

There is no conflict between "initialize the instance" and "allocate the
member" (using a liberal interpretation so as to make the latter
meaningful); the latter is generally a part of the former.

Anyway what initialization a constructor needs to do depends solely on
the class' invariant, which is the set of assumptions that you guarantee
will hold for every instance.

If constructor is used only to Intialize the Instance variable of
the class, What is the need of Constructor in Abstract
class?.............

An abstract class is not different from other classes in this respect.

What difference did you think there would be?
 
G

GB

benben said:
Initialize members.


No need. You generally don't write one for abstract class.

That may be true for the special case of purely abstract base classes
that are not derived from anything, but otherwise it is not true. An
abstract class many have a base class that has a non-default constructor
that requires parameters that have to be passed up. The abstract class
may also have its own data members.

Gregg
 
B

benben

A constructor can and usually will do some allocation.

I didn't say it doesn't. I just said I doesn't allocate members.

Regards,
Ben
 
A

Alf P. Steinbach

* benben:
I didn't say it doesn't. I just said I doesn't allocate members.

I think, with a question that's not framed in perfect English, but
rather imperfect English, one should try to interpret it in a way that's
meaningful, or else just answer that question seems to be meaningless,
or not answer it at all (of course that's just my opinion).
 

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

Forum statistics

Threads
474,436
Messages
2,571,696
Members
48,796
Latest member
Greg L.
Top