473,395 Members | 1,464 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,395 software developers and data experts.

Question about constructor overloading

Hello!

I am wondering what's the best practice about setting object data using
constructor parameters.

Let's say that I am creating a Person class.

class Person
{
private string name;
private Date dateOfBirth;
public Person()
{
}
public string Name
{
get {return name;}
set {name = value;}
}
public Date DateOfBirth
{
get {return dateOfBirth;}
set {dateOfBirth = value;}
}
}

Now I have options regarding how to set name and dateOfBirth.

1)

public Person(string n, Date d): this()
{
name = n;
dateOfBirth = d;
}

When using the class,

Person person = new Person(name, dob);
2)

Person person = new Person();
person.Name = name;
person.DateOfBirth = dob;
OK.
Now I want to know what's the guideline regarding the above case?
Option 1) seems to be better.
However, if there are many parameters, I need to provide all the overloading
constructors for every combination of parameters.
For example,

public Person(string n): this()
{
name = n;
}

public Person(Date d): this()
{
dateOfBirth = d;
}
Option 2) is simple.

I feel that I need some guidelines about what data should be in the
constructor's parameters.
TIA.
Sam
Nov 16 '05 #1
8 2234
Option 1 is better for the simple reason:

Person p = new Person();
Console.Write(p.Name); // BOOM!

Your worry is a false lead. You don't have to create most of those,
because you *shouldn't* create most of those. You need write just the ones
that will create a valid object. In your example, that's probably just the
one with both parameters. The best way to ensure that you never have an
invalid object, is to make it impossible to create an invalid object.

--
Truth,
James Curran
Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com
(note new day job!)
"Sam Sungshik Kong" <ss*@chol.nospam.net> wrote in message
news:e6**************@tk2msftngp13.phx.gbl...
I am wondering what's the best practice about setting object data using
constructor parameters.

Let's say that I am creating a Person class.

class Person
{
private string name;
private Date dateOfBirth;
public Person()
{
}
public string Name
{
get {return name;}
set {name = value;}
}
public Date DateOfBirth
{
get {return dateOfBirth;}
set {dateOfBirth = value;}
}
}

Now I have options regarding how to set name and dateOfBirth.

1)

public Person(string n, Date d): this()
{
name = n;
dateOfBirth = d;
}

When using the class,

Person person = new Person(name, dob);
2)

Person person = new Person();
person.Name = name;
person.DateOfBirth = dob;
OK.
Now I want to know what's the guideline regarding the above case?
Option 1) seems to be better.
However, if there are many parameters, I need to provide all the overloading constructors for every combination of parameters.
For example,

public Person(string n): this()
{
name = n;
}

public Person(Date d): this()
{
dateOfBirth = d;
}
Option 2) is simple.

I feel that I need some guidelines about what data should be in the
constructor's parameters.
TIA.
Sam

Nov 16 '05 #2
"James Curran" <Ja*********@mvps.org> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
You need write just the ones
that will create a valid object. In your example, that's probably just the one with both parameters. The best way to ensure that you never have an
invalid object, is to make it impossible to create an invalid object.


And also, if the default constructor (no parameters) cannot create a *valid*
object (more info is necessary), then you will want to make that constructor
protected or private so that consumers cannot create an invalid one.

-- Alan

Nov 16 '05 #3
Thanks for the answer.
I have an additional question in-line.

"James Curran" <Ja*********@mvps.org> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
Option 1 is better for the simple reason:

Person p = new Person();
Console.Write(p.Name); // BOOM!


If this is BOOM, then is a constructor Person(string name) a must, not an
option?
Date dateOfBirth is a value type and will be automatically initialize,
right?

To sum up,

name(reference type) should be in the parameter.
dateOfBirth(value type) is an optional.

Am I right?
Sam
Nov 16 '05 #4
"Sam Sungshik Kong" <ss*@chol.nospam.net> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
If this is BOOM, then is a constructor Person(string name) a must, not an
option?
I would say so.
Date dateOfBirth is a value type and will be automatically initialize,
right?


Yes... BUT... It will be initialized, but to something that is
definitely wrong. If you let it default initialize, whenever you want to
use dateOfBirth you will have to check to establish is this is a valid
birthday or a bad value. Remember, our goal here is not merely programs
which don't crash but programs that give the correct answers.
--
Truth,
James Curran
Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com
(note new day job!)
Nov 16 '05 #5
"Sam Sungshik Kong" <ss*@chol.nospam.net> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
If this is BOOM, then is a constructor Person(string name) a must, not an
option?
Date dateOfBirth is a value type and will be automatically initialize,
right?

To sum up,
name(reference type) should be in the parameter.
dateOfBirth(value type) is an optional.

Am I right?


It doesn't matter if you are working with reference or value types.

What matters: If your object is not valid without the name member, then you
want to force the consumer to supply the name. If everything else is
optional, then simply provide the constructor that initializes the name, and
make the default constructor protected or private so that it cannot be
called from the outside. Initialize all of the optional values to
appropriate defaults in your constructor. Then let the consumer override
the default optional values through the properties.

-- Alan
Nov 16 '05 #6
hi
i have an opinion
when we create a class we can provide the details which uniquely
identify(primary attribures )in the cunstructor .the secondary details can be
assigned using properties.

eg in an person calss the primary details are persons identity,name ,what
ever..
the secondary details are salary details ,office phone ....

my approach is purely in a business point of view ..

regards
********
ansil
Technopark
Trivandrum
*************

"James Curran" wrote:
"Sam Sungshik Kong" <ss*@chol.nospam.net> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
If this is BOOM, then is a constructor Person(string name) a must, not an
option?


I would say so.
Date dateOfBirth is a value type and will be automatically initialize,
right?


Yes... BUT... It will be initialized, but to something that is
definitely wrong. If you let it default initialize, whenever you want to
use dateOfBirth you will have to check to establish is this is a valid
birthday or a bad value. Remember, our goal here is not merely programs
which don't crash but programs that give the correct answers.
--
Truth,
James Curran
Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com
(note new day job!)

Nov 16 '05 #7
Alan Pretre <al********@newsgroup.nospam> wrote:
"James Curran" <Ja*********@mvps.org> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
You need write just the ones
that will create a valid object. In your example, that's probably just

the
one with both parameters. The best way to ensure that you never have an
invalid object, is to make it impossible to create an invalid object.


And also, if the default constructor (no parameters) cannot create a *valid*
object (more info is necessary), then you will want to make that constructor
protected or private so that consumers cannot create an invalid one.


No need for that unless you've got no other constructors. The default
constructor is only provided automatically if there are no other
constructors.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #8
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
No need for that unless you've got no other constructors. The default
constructor is only provided automatically if there are no other
constructors.


Thanks.

-- Alan
Nov 16 '05 #9

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

Similar topics

7
by: jesse | last post by:
In java, one constructor can call another constructor through this(...) for instance class foo { public: foo(int k) { this(k,false)}; foo(int k, boolean m){...}; }
2
by: Kamran | last post by:
Hi I have very little experience of C++, nevertheless I have been asked to write a gui using QT/QWT. I know that I should direct the question to the relevant mailing list and I have done that but...
7
by: Eckhard Lehmann | last post by:
Hi, I try to recall some C++ currently. Therefore I read the "Standard C++ Bible" by C. Walnum, A. Stevens and - of course there are chapters about operator overloading. Now I have a class...
26
by: Paul | last post by:
public class A { public A () { // here I would like to call the second version of _ctor, how to accomplish this ? } public A (int a, int b, int c) {
14
by: Klaus Löffelmann | last post by:
Hi, does anybody know, why the second contructor isn't called in this example? Are you able to reproduce this bug? Thanks Klaus Public Class Something
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; }...
3
by: iluvatar | last post by:
Hi all. I have written a 3d-vector class (for 3-dimensional space) and I have overloaded the arihtmetic operators like +, +=, * and so on. Also, the constructor works with doubles and has...
22
by: clicwar | last post by:
A simple program with operator overloading and copy constructor: #include <iostream> #include <string> using namespace std; class Vector { private: float x,y; public: Vector(float u, float...
30
by: none | last post by:
I'm trying to overload the = operator using templates, but I have some problems with one of the overloads, I would like to make something like that: intvariable = fooclass; here's a pseudo...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
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,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.