473,748 Members | 2,426 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1507

pa**********@at t.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():da te(1)
{ set_month(date) ; }

Dec 1 '05 #3
de*********@hot mail.com wrote:
pa**********@at t.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**********@at t.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******@hotmai l.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():da te(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_mon th() whenever I need it.

Thank you,

Paul Epstein

Dec 1 '05 #6
pa**********@at t.net wrote:
wi******@hotmai l.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():da te(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():da te(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**********@ya hoo.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
1407
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* const data; public: MyError(const char* const msg = 0) : data(msg) {} };
5
2846
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
2773
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 src="proceed_off.gif" width="79"
18
1387
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 with http://www.eskimo.com/~scs/C-faq/top.html , but I found no resolution to my first question, which crops up on page 6. I have always called main with an integer that says how many pointers are being passed to it. Is this just a matter of...
4
2471
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 clarify my understanding of it and get some implementation feedback. Im a bit unsure so please correct me. What Im Doing
3
1452
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 dates as integers. It's important to be able to derive the month from the date so I have programmed this using mod 365 arithmetic. I have several classes that use dates as private objects.
1
1124
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 way I would expect, except that setting x to 12 in A using class_setx at the beginning also sets the value for x in B. However, the converse (setting x in B using class_setx), does
5
2534
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 minutes,use 4 digit hex display multiplexing.
10
2018
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 application that I work on will consist of - a mainform with a usual menu and toolbar and not much more. - a number of modal data editing forms with various edit controls, an OK button and a Cancel button
0
8823
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9530
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, 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...
0
9363
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9238
tracyyun
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...
0
8237
agi2029
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...
0
6073
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4864
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3300
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
2
2775
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.