Class Confusion

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Fao, Sean

    #1

    Class Confusion

    Hello All,

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

    Date Date::default_d ate(16, 12, 1770);

    In the following code.

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

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

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

    return 0;
    }

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

    void Date::set_defau lt(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_d ate(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

    #2
    Re: Class Confusion


    <Fao>; "Sean" <enceladus311@y ahoo.comI-WANT-NO-SPAM> wrote in message
    news:slrncjbjsf .gpm.sean@neptu ne.kpl.com...[color=blue]
    > Hello All,
    >
    > I'm reading The C++ Programming Language and I'm having a
    > misunderstandin g of part of the code in the book. Specifically, I'm not
    > understanding the line:
    >
    > Date Date::default_d ate(16, 12, 1770);
    >
    > In the following code.
    >
    > <date.cpp>
    > #include <iostream>
    > #include "date.h"
    >
    > int main(void)
    > {
    > Date::set_defau lt(4, 5, 1945);
    > Date date;
    >
    > std::cout << date.Month() << "/" << date.Day() << "/" << date.Year()
    > << std::endl;
    >
    > return 0;
    > }
    >
    > Date Date::default_d ate(16, 12, 1770); // Don't understand this line
    >
    > void Date::set_defau lt(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_d ate(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


    Comment

    • Vyacheslav Kononenko

      #3
      Re: Class Confusion

      Fao, Sean <enceladus311@y ahoo.comI-WANT-NO-SPAM> wrote in message news:<slrncjbjs f.gpm.sean@nept une.kpl.com>...[color=blue]
      > My guess is that Date::default_d ate(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

      Comment

      • Fao, Sean

        #4
        Re: Class Confusion

        Vyacheslav Kononenko wrote:[color=blue]
        > Fao, Sean <enceladus311@y ahoo.comI-WANT-NO-SPAM> wrote in message news:<slrncjbjs f.gpm.sean@nept une.kpl.com>...
        >[color=green]
        >>My guess is that Date::default_d ate(16, 12, 1770); is initializing the
        >>default_dat e 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...T hat clears up a lot.

        Thank you very much for your help.

        --
        Sean

        Comment

        • Fao, Sean

          #5
          Re: Class Confusion

          Howard wrote:[color=blue][color=green]
          >>My guess is that Date::default_d ate(16, 12, 1770); is initializing the
          >>default_dat e 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

          Comment

          Working...