By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,191 Members | 749 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,191 IT Pros & Developers. It's quick & easy.

Class Confusion

P: n/a
Hello All,

I'm reading The C++ Programming Language and I'm having a
misunderstanding of part of the code in the book. Specifically, I'm not
understanding the line:

Date Date::default_date(16, 12, 1770);

In the following code.

<date.cpp>
#include <iostream>
#include "date.h"

int main(void)
{
Date::set_default(4, 5, 1945);
Date date;

std::cout << date.Month() << "/" << date.Day() << "/" << date.Year()
<< std::endl;

return 0;
}

Date Date::default_date(16, 12, 1770); // Don't understand this line

void Date::set_default(int d, int m, int y)
{
default_date = Date(d, m, y);
}

Date::Date(int dd, int mm, int yy)
{
d = dd ? dd : default_date.d;
m = mm ? mm : default_date.m;
y = yy ? yy : default_date.y;
}

</date.cpp

<date.h>
#ifndef DATE_H
#define DATE_H
class Date
{
int m,
d,
y;
static Date default_date;

public:
Date(int dd = 0, int mm = 0, int yy = 0);
int Day() const { return d; }
int Month() const { return m; }
int Year() const { return y; }
static void set_default(int dd, int mm, int yy);
};
#endif

</date.h>

My guess is that Date::default_date(16, 12, 1770); is initializing the
default_date variable in the Date class by using the constructor for
Date. I basically just want clarification that A) I'm doing this
correctly B) I understand exactly what is occurring.

One thing I am certain of is that if I don't call the set_default()
function, my output displays 12/16/1770.

Thank you for your help,

--
Sean
Jul 22 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a

<Fao>; "Sean" <en**********@yahoo.comI-WANT-NO-SPAM> wrote in message
news:sl*****************@neptune.kpl.com...
Hello All,

I'm reading The C++ Programming Language and I'm having a
misunderstanding of part of the code in the book. Specifically, I'm not
understanding the line:

Date Date::default_date(16, 12, 1770);

In the following code.

<date.cpp>
#include <iostream>
#include "date.h"

int main(void)
{
Date::set_default(4, 5, 1945);
Date date;

std::cout << date.Month() << "/" << date.Day() << "/" << date.Year()
<< std::endl;

return 0;
}

Date Date::default_date(16, 12, 1770); // Don't understand this line

void Date::set_default(int d, int m, int y)
{
default_date = Date(d, m, y);
}

Date::Date(int dd, int mm, int yy)
{
d = dd ? dd : default_date.d;
m = mm ? mm : default_date.m;
y = yy ? yy : default_date.y;
}

</date.cpp

<date.h>
#ifndef DATE_H
#define DATE_H
class Date
{
int m,
d,
y;
static Date default_date;

public:
Date(int dd = 0, int mm = 0, int yy = 0);
int Day() const { return d; }
int Month() const { return m; }
int Year() const { return y; }
static void set_default(int dd, int mm, int yy);
};
#endif

</date.h>

My guess is that Date::default_date(16, 12, 1770); is initializing the
default_date variable in the Date class by using the constructor for
Date. I basically just want clarification that A) I'm doing this
correctly B) I understand exactly what is occurring.

Your understainding is correct. Static member variables (except integral
types, I think) must be defined (initialized) at the global level, not
inside the class declaration. (And yes, it's using the public constructor
that takes three parameters to initialze it.)
One thing I am certain of is that if I don't call the set_default()
function, my output displays 12/16/1770.
Quite right. That's because your declaration of the variable "date" doesn't
pass any parameters the constructor, so all those parameters take the
default value of 0. And the Date constructor says that, for any parameter
that is zero, use the value from default_date instead as the actual value.
Thank you for your help,

--
Sean


-Howard
Jul 22 '05 #2

P: n/a
Fao, Sean <en**********@yahoo.comI-WANT-NO-SPAM> wrote in message news:<sl*****************@neptune.kpl.com>...
My guess is that Date::default_date(16, 12, 1770); is initializing the
default_date variable in the Date class by using the constructor for
Date. I basically just want clarification that A) I'm doing this
correctly B) I understand exactly what is occurring.


Not exactly. It works similar to global variables like this:
extern int foo; // description

int foo = 0; // definition and initialization

so almost the same way in class

class AClass {
static int foo; // description
};

int AClass::foo = 0; // definition and initialization

Regards,
Slava
Jul 22 '05 #3

P: n/a
Vyacheslav Kononenko wrote:
Fao, Sean <en**********@yahoo.comI-WANT-NO-SPAM> wrote in message news:<sl*****************@neptune.kpl.com>...
My guess is that Date::default_date(16, 12, 1770); is initializing the
default_date variable in the Date class by using the constructor for
Date. I basically just want clarification that A) I'm doing this
correctly B) I understand exactly what is occurring.

Not exactly. It works similar to global variables like this:
extern int foo; // description

int foo = 0; // definition and initialization

so almost the same way in class

class AClass {
static int foo; // description
};

int AClass::foo = 0; // definition and initialization


Interesting...That clears up a lot.

Thank you very much for your help.

--
Sean
Jul 22 '05 #4

P: n/a
Howard wrote:
My guess is that Date::default_date(16, 12, 1770); is initializing the
default_date variable in the Date class by using the constructor for
Date. I basically just want clarification that A) I'm doing this
correctly B) I understand exactly what is occurring.


Your understainding is correct. Static member variables (except integral
types, I think) must be defined (initialized) at the global level, not
inside the class declaration. (And yes, it's using the public constructor
that takes three parameters to initialze it.)


That would explain my failed attempts to initialize the static variable
inside the class.

I think that if a static variable were initialized inside of a class
rather than at the global level, a new static variable would be declared
for each definition of an object of that type. That would defeat the
point of a static variable altogether, so C++ prevents this behavior by
enforcing you to declare it as at the global level.

I'm just hypothesizing; I'm not certain of anything I just said :-).

Thanks for your help,

--
Sean
Jul 22 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.