Class Confusion

F

Fao, Sean

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,
 
H

Howard

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,

-Howard
 
V

Vyacheslav Kononenko

Fao said:
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
 
F

Fao, Sean

Vyacheslav said:
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.
 
F

Fao, Sean

Howard said:
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,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top