Connecting Tech Pros Worldwide Forums | Help | Site Map

Class Confusion

Fao, Sean
Guest
 
Posts: n/a
#1: Jul 22 '05
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

Howard
Guest
 
Posts: n/a
#2: Jul 22 '05

re: Class Confusion



<Fao>; "Sean" <enceladus311@yahoo.comI-WANT-NO-SPAM> wrote in message
news:slrncjbjsf.gpm.sean@neptune.kpl.com...[color=blue]
> 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.
>[/color]

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.)
[color=blue]
> One thing I am certain of is that if I don't call the set_default()
> function, my output displays 12/16/1770.[/color]

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.[color=blue]
>
> Thank you for your help,
>
> --
> Sean[/color]

-Howard


Vyacheslav Kononenko
Guest
 
Posts: n/a
#3: Jul 22 '05

re: Class Confusion


Fao, Sean <enceladus311@yahoo.comI-WANT-NO-SPAM> wrote in message news:<slrncjbjsf.gpm.sean@neptune.kpl.com>...[color=blue]
> 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.[/color]

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
Fao, Sean
Guest
 
Posts: n/a
#4: Jul 22 '05

re: Class Confusion


Vyacheslav Kononenko wrote:[color=blue]
> Fao, Sean <enceladus311@yahoo.comI-WANT-NO-SPAM> wrote in message news:<slrncjbjsf.gpm.sean@neptune.kpl.com>...
>[color=green]
>>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.[/color]
>
>
> 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[/color]

Interesting...That clears up a lot.

Thank you very much for your help.

--
Sean
Fao, Sean
Guest
 
Posts: n/a
#5: Jul 22 '05

re: Class Confusion


Howard wrote:[color=blue][color=green]
>>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.[/color]
>
> 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.)[/color]

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
Closed Thread


Similar C / C++ bytes