473,326 Members | 2,010 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,326 software developers and data experts.

elementary question about setting up class

I am writing a program which looks at nodes which have coordinates
which are time-dependent.

So I have a class called node which contains the private member
declarations int date; int month; (I want to consider the month as
part of the information contained in the node, as well as the date.)

The node class also contains public member function void set_month(int)
which sets the month according to the date given.

The corresponding code is void node::set_month(int given_date)
{ int calendar = given_date % 365;
if(calendar <= 31) month = 0;

etc. etc. }
I want my default constructor to assume some basic assumptions about
the node.

My plan is for the default construction node::node() to call the
set_month member function

In other words, my node::node() function would contain the line of code

set_month(date);

Is this o.k. as a line of code? In fact, would it be o.k to write

node::node()
{ set_month(date); }
Is there a problem with this with regard to either style or legality.
It seems to solve my problem but a default constructor should (I think)
be the most basic type of function and it seems wrong for a default
constructor to call on another member function.

Or is my proposed approach o.k?

Thank you,

Paul Epstein

Dec 1 '05 #1
8 1482

pa**********@att.net wrote:
I am writing a program which looks at nodes which have coordinates
which are time-dependent.

So I have a class called node which contains the private member
declarations int date; int month; (I want to consider the month as
part of the information contained in the node, as well as the date.)

The node class also contains public member function void set_month(int)
which sets the month according to the date given.

The corresponding code is void node::set_month(int given_date)
{ int calendar = given_date % 365;
if(calendar <= 31) month = 0;

etc. etc. }
I want my default constructor to assume some basic assumptions about
the node.

My plan is for the default construction node::node() to call the
set_month member function

In other words, my node::node() function would contain the line of code

set_month(date);

Is this o.k. as a line of code? In fact, would it be o.k to write

node::node()
{ set_month(date); }
Is there a problem with this with regard to either style or legality.
It seems to solve my problem but a default constructor should (I think)
be the most basic type of function and it seems wrong for a default
constructor to call on another member function.

Or is my proposed approach o.k?


By the time the code in the body of your constructor is called, all the
messy behind-the-scenes stuff (allocating memory, constructing data
members and base classes) has completed so it is perfectly safe and
legal to call a member function. A bit more to think about with virtual
member functions, but I assume that's not relevant here.

If you need identical functionality in the constructor and the member
function then it is right that the constructor calls the member
function.

Gavin Deane

Dec 1 '05 #2
You should initialize date with a value, otherwise a call as you
suggest will not initialize month predictably - so make the default
constructor e.g.

node::node():date(1)
{ set_month(date); }

Dec 1 '05 #3
de*********@hotmail.com wrote:
pa**********@att.net wrote:
I am writing a program which looks at nodes which have coordinates
which are time-dependent.

So I have a class called node which contains the private member
declarations int date; int month; (I want to consider the month as
part of the information contained in the node, as well as the date.)

The node class also contains public member function void set_month(int)
which sets the month according to the date given.
<snip>
My plan is for the default construction node::node() to call the
set_month member function

In other words, my node::node() function would contain the line of code

set_month(date);

Is this o.k. as a line of code? In fact, would it be o.k to write

node::node()
{ set_month(date); }

<snip>
By the time the code in the body of your constructor is called, all the
messy behind-the-scenes stuff (allocating memory, constructing data
members and base classes) has completed so it is perfectly safe and
legal to call a member function. A bit more to think about with virtual
member functions, but I assume that's not relevant here.


I was thinking about the principle of what you are doing and missed
some of the detail of your actual code. As somone else has correctly
pointed out, you will need to initialise the member variable date
before the constructor passes it to the set_month member function. The
member variable exists by the time the body of your constructor is
executed, but being a built-in type, it is uninitialised. Passing it to
set_month in that state would be undefined bahaviour.

Gavin Deane

Dec 1 '05 #4
* pa**********@att.net:
I am writing a program which looks at nodes which have coordinates
which are time-dependent.

So I have a class called node which contains the private member
declarations int date; int month; (I want to consider the month as
part of the information contained in the node, as well as the date.)

The node class also contains public member function void set_month(int)
which sets the month according to the date given.

The corresponding code is void node::set_month(int given_date)
{ int calendar = given_date % 365;
if(calendar <= 31) month = 0;

etc. etc. }
If you can compute the month from the date, provide a member function to
extract the month.

Anything else is premature optimization that causes trouble.
I want my default constructor to assume some basic assumptions about
the node.

My plan is for the default construction node::node() to call the
set_month member function

In other words, my node::node() function would contain the line of code

set_month(date);

Is this o.k. as a line of code? In fact, would it be o.k to write

node::node()
{ set_month(date); }
Here date is not yet initialized; see above.
Is there a problem with this with regard to either style or legality.


Yes, see above.

--
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?
Dec 1 '05 #5

wi******@hotmail.com wrote:
You should initialize date with a value, otherwise a call as you
suggest will not initialize month predictably - so make the default
constructor e.g.

node::node():date(1)
{ set_month(date); }


I am not familiar with this code. Is this equivalent to node::node()
{ date = 1;
set_month(date); } ?

Furthermore, if I write my code like this. As I change the date, is
there a danger that set_month(date) will operate on the default date of
1 rather than than the newly changed date?

In other words, suppose my set_month works properly

Suppose I have

node::node()
{date = 1; set_month(date); }

int main (void)

{ example node;

[code here via accessor functions to set the date of example to be
73];

... more code;

}

Will set_month know to operate on 73 instead of 1? (I think so -- I
think that class members behave a bit like static variables in a loop.
It's the last value that's important, not the value declared
initially.)

Am I right on this?

I think someone on the thread is right that I should not use the month
variable at all -- just have a month function that returns an integer,
and then refer to example.set_month() whenever I need it.

Thank you,

Paul Epstein

Dec 1 '05 #6
pa**********@att.net wrote:
wi******@hotmail.com wrote:
You should initialize date with a value, otherwise a call as you
suggest will not initialize month predictably - so make the default
constructor e.g.

node::node():date(1)
{ set_month(date); }
I am not familiar with this code. Is this equivalent to node::node()
{ date = 1;
set_month(date); } ?

No. One is initialization, the other is assignment.

Furthermore, if I write my code like this. As I change the date, is
there a danger that set_month(date) will operate on the default date of
1 rather than than the newly changed date?


I'm confused. Constructors only run at initialization time...if you
call methods after the object is initialized, you don't have to worry
about the constructor body anymore.
Dec 1 '05 #7

Ron Natalie wrote:
node::node():date(1)
{ set_month(date); }


I am not familiar with this code. Is this equivalent to node::node()
{ date = 1;
set_month(date); } ?

No. One is initialization, the other is assignment.


I do understand initialization but not assignment. And yes, I have
tried to google around. If anyone would like to give a web link that
explains this type of assignment (or provide an explanation
themselves), that would be great.

Or I can be reached directly via email at pa**********@yahoo.com or by
yahoo messenger -- ID: pauldepstein

Thank you,

Paul Epstein

Dec 1 '05 #8
For more information on how to initialize members in a constructor see
http://www.parashift.com/c++-faq-lit....html#faq-10.6

Dec 2 '05 #9

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

Similar topics

5
by: Last Timer | last post by:
I encountered the following code in Bruce Eckel's online book. Can you please clarify what "const char* const data;" means? Thanks //: C01:MyError.cpp {RunByHand} class MyError { const char*...
5
by: Lionel B | last post by:
Greetings, I am trying to implement "element-wise" arithmetic operators for a class along the following lines (this is a simplified example): // ----- BEGIN CODE ----- struct X { int a,b;
6
by: KublaiKhan | last post by:
The code is: <html> <head> <title>Test Page</title> </head> <body> <table width="100%" border="0" cellpadding="0" cellspacing="0" align="center"> <tr> <td align="left"><img...
18
by: Merrill & Michele | last post by:
Today is when I sing in the choir, and I was thinking about rehearsal tonight when I realized that in order to be in the C choir, I had to buy K&R to be reading the same "music." I am familiar...
4
by: spamfurnace | last post by:
Hi there. Ive just been reading about the Whidbey Provider Pattern on MSDN, http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnaspnet/html/asp02182004.asp and i wanted to...
3
by: pauldepstein | last post by:
I am writing a program to calculate prices of commodity options as part of my Masters degree in Finance. (Absolutely no prior knowledge of computer programming was assumed.) I am representing...
1
by: Faheem Mitha | last post by:
Hi, Consider the following small script. My understanding of how this works is that, conceptually, class B holds a separate copy of variable x from class A. Nearly everything behaves the...
5
by: bromio | last post by:
can someone help me to make code for digital clock using AT89S52. The specs of clock are 12 hour clock,am-pm display,use timer interrupts to have delay.two external inputs to adjust hours and...
10
by: Descartes | last post by:
Dear All, Coming from another development environment, it appears that I bang my head in the wall on very basic matters, so I hope you can give me a push in the right direction. The...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.