Connect with Expertise | Find Experts, Get Answers, Share Insights

What is the Purpose of Constructor in a class?

rvinoth83@gmail.com
 
Posts: n/a
#1: Mar 12 '06
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?.............


benben
 
Posts: n/a
#2: Mar 12 '06

re: What is the Purpose of Constructor in a class?


rvinoth83@gmail.com wrote:[color=blue]
> 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?........[/color]

Initialize members.
[color=blue]
>
> If constructor is used only to Intialize the Instance variable of
> the class, What is the need of Constructor in Abstract
> class?.............
>[/color]

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

Regards,
Ben
Alf P. Steinbach
 
Posts: n/a
#3: Mar 12 '06

re: What is the Purpose of Constructor in a class?


* rvinoth83@gmail.com:[color=blue]
> 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?........[/color]

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.

[color=blue]
> If constructor is used only to Intialize the Instance variable of
> the class, What is the need of Constructor in Abstract
> class?.............[/color]

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

What difference did you think there would be?

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Alf P. Steinbach
 
Posts: n/a
#4: Mar 12 '06

re: What is the Purpose of Constructor in a class?


* benben:[color=blue]
> rvinoth83@gmail.com wrote:[color=green]
>> 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?........[/color]
>
> Initialize members.[/color]

A constructor can and usually will do some allocation.

[color=blue][color=green]
>> If constructor is used only to Intialize the Instance variable of
>> the class, What is the need of Constructor in Abstract
>> class?.............
>>[/color]
>
> No need. You generally don't write one for abstract class.[/color]

I certainly write constructors for abstract classes.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
GB
 
Posts: n/a
#5: Mar 12 '06

re: What is the Purpose of Constructor in a class?


benben wrote:[color=blue]
> rvinoth83@gmail.com wrote:[color=green]
>> 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?........[/color]
>
> Initialize members.
>[color=green]
>>
>> If constructor is used only to Intialize the Instance variable of
>> the class, What is the need of Constructor in Abstract
>> class?.............
>>[/color]
>
> No need. You generally don't write one for abstract class.[/color]

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
benben
 
Posts: n/a
#6: Mar 13 '06

re: What is the Purpose of Constructor in a class?


> 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
Alf P. Steinbach
 
Posts: n/a
#7: Mar 13 '06

re: What is the Purpose of Constructor in a class?


* benben:[color=blue][color=green]
>> A constructor can and usually will do some allocation.[/color]
>
> I didn't say it doesn't. I just said I doesn't allocate members.[/color]

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).

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Closed Thread