473,399 Members | 2,478 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

Class Confusion

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
4 1485

<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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Grey Plastic | last post by:
I have several classes that all keep track of static data. However, the manner that they keep track of static data is identical, and so I'm using the template<class Child> class Parent { ... };...
1
by: ypjofficial | last post by:
Dear All, According to OOPs , a base class pointer can to point to derived class object....call this as fact1 But somehow I am not comfortable while understanding this concept. The explanaition...
822
by: Turamnvia Suouriviaskimatta | last post by:
I 'm following various posting in "comp.lang.ada, comp.lang.c++ , comp.realtime, comp.software-eng" groups regarding selection of a programming language of C, C++ or Ada for safety critical...
27
by: djake | last post by:
In the stroustrup C++ programming language (third edition) i can find example like this: string s1= "Hello"; So I imagine string is a standard class. Furthermore the class in the example is...
16
by: christopher diggins | last post by:
It appears that the following is not considered a class: template<typename T> class C { }; ? So officially is this considered: a class, a template, a class template, or a template class? I...
37
by: JohnGoogle | last post by:
Hi, Newbie question... After a recent article in VSJ I had a go at implementing a Fraction class to aid my understanding of operator overloading. After a previous message someone suggested...
70
by: garyusenet | last post by:
I'm using an example piece of code: - namespace Wintellect.Interop.Sound{ using System; using System.Runtime.InteropServices; using System.ComponentModel; sealed class Sound{ public static...
7
by: MR | last post by:
Hello All, I have a question about decorators, and I think an illustration would be helpful. Consider the following simple class: #begin code class Foo: def fooDecorator(f): print...
2
by: Anup Daware | last post by:
Hi Group, I have a little confusion over the use of static class in C#. I have a static method in my static class. This method reads an xml and returns a collection of objects. This collection...
3
by: notnorwegian | last post by:
i have some confusion over this. sure a class is basically a classification, like for example an animal or flower. and an object/instance of that class is then for example a cat. an object is...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.