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 7 2375
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
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.co m> wrote in message
news:11******** **************@ o13g2000cwo.goo glegroups.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
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
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.goo glegroups.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
gordon <go**********@o ptusnet.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.co m> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
>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.
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.goo glegroups.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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 are
quite a few things that I learned that I did not know before. For
example, while I knew that the new and delete operators can be
overloaded for classes, I did not know that that the global new and
delete operators can also be overloaded. ...
|
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 as
a char rather than incorrectly representing it as an int. I've done this
because I want to develop the class so that it will be able to convert
between values, ie if I add 500 m to 1 km I'd like a correct result given in
metres (1500 m in this...
|
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++ copyconstructor1.cpp
#copyconstructor1.cpp: In function `int main()':
#copyconstructor1.cpp:86: no match for `sample& = sample' operator
#copyconstructor1.cpp:53: candidates are: sample sample::operator=(sample&)
...
|
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 method takes 0 arguments.
#region tViewNodeCollection
public class tViewNodeCollection : TreeNodeCollection
{
private tView _owner;
private tViewNode _parent;
/// <summary>
|
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 object from the calling class, but the compliler doesn't like it when I try to provide and overrided contructor. See the code below:
Do I have to stick with the standard ComboBox constructor?
If so, how else could I share a common reference to...
| |
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:
Dim x as New MyForm(parm01, parm02)
Should I just override new? What is the best way to do this for a form? (The
reason I am asking is it appears that the New for a form does a number of
things, like initialize controls, etc)
|
by: portroe |
last post by:
How do you overload constructors in VB .net?
thanks
Portroe
|
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;
}...
|
by: anand03 |
last post by:
HI frnds,
can any one help about the use of Defualt constructor & Overload constructor with example
Regards
anand
ShantaSoft
|
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 = X(1,2), the elements
1 and 2 of X gets assigned to Y. In this case, the operator ()
overload should create a copy that is unmodifiable. In the 2nd case, I
want do assign values to elements 1 and 2, e.g. X(1,2) = Y. Then in
this case, the values...
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
| |
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
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 we have to send another system
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |