473,324 Members | 2,214 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

overload constructor

Hi

I am working through a learning module privately where there is discussion
about overloading the constructor.

Could someone please let me know the benefit or purpose of doing this in a
class?

Thanks

doug
Dec 2 '05 #1
7 2355
Doug wrote:
I am working through a learning module privately where there is discussion
about overloading the constructor.

Could someone please let me know the benefit or purpose of doing this in a
class?


Sure - often you don't want to specify the same parameters every time
you construct an object. For instance, StreamWriter has several
overloads - some take a filename, some take a Stream.

Alternatively, look at ArrayList - you don't always want to specify the
initial capacity, but sometimes you do. Removing either of those
constructors would make life harder or less efficient.

Does that help?

Jon

Dec 2 '05 #2
Thanks Jon

So the purpose of the constructor is to create an instance of an object and
therefore allow access to methods that are contained within that object,
And overloading would allow different parameters to be provided to the
method of different instances of that class??
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
Doug wrote:
I am working through a learning module privately where there is
discussion
about overloading the constructor.

Could someone please let me know the benefit or purpose of doing this in
a
class?


Sure - often you don't want to specify the same parameters every time
you construct an object. For instance, StreamWriter has several
overloads - some take a filename, some take a Stream.

Alternatively, look at ArrayList - you don't always want to specify the
initial capacity, but sometimes you do. Removing either of those
constructors would make life harder or less efficient.

Does that help?

Jon

Dec 3 '05 #3
Hi Doug,

Doug wrote:
And overloading would allow different parameters to be provided to the
method of different instances of that class??

This sentence appears difficult to understand.
The constructor is a special method used to initialize the instance.
The parameters of the constructor are used to initialize the object.
Sometimes you wish to specify more values; sometimes you want to
specify less and let the object uses defaults; sometimes you could only
specify some at the time of instantiation, and will set some properties
later. Constructor overloading makes life easier by provide you with
many choices to initialize the object at the time it is created.

Thi

Dec 3 '05 #4
Thanks Thi

Being still at the learning gate of this language I like to get clarity
about the various elements. Most of the books I have read or are reading
about C# seem to assume that the readers understand these concepts and
therefore they skim over a detailed explanation.

Do you know of any website links that cover these concepts? I struggled
with 'state' for a while because the material that I was reading assumed it
was obvious - maybe not to me.

Thanks again to you and Jon.

Doug
"Truong Hong Thi" <th*****@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
Hi Doug,

Doug wrote:
And overloading would allow different parameters to be provided to the
method of different instances of that class??

This sentence appears difficult to understand.
The constructor is a special method used to initialize the instance.
The parameters of the constructor are used to initialize the object.
Sometimes you wish to specify more values; sometimes you want to
specify less and let the object uses defaults; sometimes you could only
specify some at the time of instantiation, and will set some properties
later. Constructor overloading makes life easier by provide you with
many choices to initialize the object at the time it is created.

Thi

Dec 3 '05 #5
gordon <go**********@optusnet.com.au> wrote:
So the purpose of the constructor is to create an instance of an object and
therefore allow access to methods that are contained within that object,
Yes.
And overloading would allow different parameters to be provided to the
method of different instances of that class??


No - the parameters which are provided to the method come later, and
are independent of what you've provided in the constructor. Usually the
different parameters in the constructor allow the object to be created
with different "state" information.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Dec 3 '05 #6
>Most of the books I have read or are reading
about C# seem to assume that the readers understand these concepts and
therefore they skim over a detailed explanation.

They are object-oriented concepts. You'll be familiar with them soon,
don't worry. Just try some tutorials.

Dec 5 '05 #7
Hi Gordon,

Although part of the Vava VM tutorial this link provides some good stuff
on OO topics. Where there are code examples, they are in Java which is
very similar to C#. However, the main thrust is the conceptual side of OO.

http://java.sun.com/docs/books/tutorial/java/concepts/

Cheers

Simon

gordon wrote:
Thanks Thi

Being still at the learning gate of this language I like to get clarity
about the various elements. Most of the books I have read or are reading
about C# seem to assume that the readers understand these concepts and
therefore they skim over a detailed explanation.

Do you know of any website links that cover these concepts? I struggled
with 'state' for a while because the material that I was reading assumed it
was obvious - maybe not to me.

Thanks again to you and Jon.

Doug
"Truong Hong Thi" <th*****@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
Hi Doug,

Doug wrote:
And overloading would allow different parameters to be provided to the
method of different instances of that class??


This sentence appears difficult to understand.
The constructor is a special method used to initialize the instance.
The parameters of the constructor are used to initialize the object.
Sometimes you wish to specify more values; sometimes you want to
specify less and let the object uses defaults; sometimes you could only
specify some at the time of instantiation, and will set some properties
later. Constructor overloading makes life easier by provide you with
many choices to initialize the object at the time it is created.

Thi


Dec 5 '05 #8

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

Similar topics

1
by: Nimmi Srivastav | last post by:
There's a rather nondescript book called "Using Borland C++" by Lee and Mark Atkinson (Que Corporation) which presents an excellent discussion of overloaded new and delete operators. In fact there...
4
by: Chiller | last post by:
Ok, thanks to some good assistance/advice from people in this group I've been able to further develop my Distance class. Since previous posts I've refined my code to accept the unit measurement...
7
by: Sean | last post by:
Can someone help me see why the following "operator=" overloading doesn't work under g++? and the error message is copied here. I see no reason the compiler complain this. Thanks, $ g++...
10
by: Benny Raymond | last post by:
I'm trying to change the way a treeview works just a bit, i'm pretty new to C# and now i'm running into overloading. I tried the following code and it's yelling at me saying that no overload...
6
by: Henry | last post by:
I was trying to derive a class from System.Windows.Forms.ComboBox. My goal was to create a class that loaded its own data. I did not want to create too many objects, so I tried to share a Database...
2
by: Tom | last post by:
How is the best way to overload the New constructor for a form? For instance, I want to be able to pass things to the constructor, or just default with the plain New. Maybe say something like: ...
7
by: portroe | last post by:
How do you overload constructors in VB .net? thanks Portroe
3
by: needin4mation | last post by:
The code is taken from the book Professional C#: abstract class GenericCustomer { private string name; public GenericCustomer(string name) { this.name = name; }...
1
by: anand03 | last post by:
HI frnds, can any one help about the use of Defualt constructor & Overload constructor with example Regards anand ShantaSoft
1
by: fabian.lim | last post by:
Hi all, Im having a problem with my code. Im programming a vector class, and am trying to overload the () operator in 2 different situations. The first situation is to assign values, e.g. Y =...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.