Class Confusion

Discussion in 'C++' started by Fao, Sean, Sep 1, 2004.

  1. Fao, Sean

    Fao, Sean Guest

    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
    Fao, Sean, Sep 1, 2004
    #1
    1. Advertising

  2. Fao, Sean

    Howard Guest

    <Fao>; "Sean" <-WANT-NO-SPAM> wrote in message
    news:...
    > 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
    Howard, Sep 1, 2004
    #2
    1. Advertising

  3. Fao, Sean <-WANT-NO-SPAM> wrote in message news:<>...
    > 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
    Vyacheslav Kononenko, Sep 1, 2004
    #3
  4. Fao, Sean

    Fao, Sean Guest

    Vyacheslav Kononenko wrote:
    > Fao, Sean <-WANT-NO-SPAM> wrote in message news:<>...
    >
    >>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
    Fao, Sean, Sep 2, 2004
    #4
  5. Fao, Sean

    Fao, Sean Guest

    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
    Fao, Sean, Sep 2, 2004
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. E11
    Replies:
    1
    Views:
    4,736
    Thomas Weidenfeller
    Oct 12, 2005
  2. christopher diggins
    Replies:
    16
    Views:
    744
    Pete Becker
    May 4, 2005
  3. Cndistin
    Replies:
    3
    Views:
    258
    Pierre Quentel
    Jan 6, 2004
  4. dugrocker
    Replies:
    3
    Views:
    467
    Oliver Wong
    Aug 17, 2006
  5. Joseph Turian
    Replies:
    5
    Views:
    585
Loading...

Share This Page