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 4 1499
<Fao>; "Sean" <en**********@y ahoo.comI-WANT-NO-SPAM> wrote in message
news:sl******** *********@neptu ne.kpl.com... 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.
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
Fao, Sean <en**********@y ahoo.comI-WANT-NO-SPAM> wrote in message news:<sl******* **********@nept une.kpl.com>... 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.
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 wrote: Fao, Sean <en**********@y ahoo.comI-WANT-NO-SPAM> wrote in message news:<sl******* **********@nept une.kpl.com>...
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.
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...T hat clears up a lot.
Thank you very much for your help.
--
Sean
Howard wrote: 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.
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 { ... }; idiom (don't
know the name of it, if there is one). The problem is that I don't
want any of my classes to have public constructors. They should be
created by a static member function.
This is what I want to do, save for the fact that this...
|
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 to the fact1 is given as since the derived object
always consists of the base part , the base class pointer will always
point to the base part in the derived object unless otherwise the
function in the base class are declared as virtual and are...
|
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 real-time
applications. The majority of expert/people recommend Ada for safety
critical real-time applications. I've many years of experience in C/C++ (and
Delphi) but no Ada knowledge.
May I ask if it is too difficult to move from C/C++ to Ada?...
|
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 initialized with a
constructor like this
|
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 always thought of it as a parameterized class. What
would the rationale be for not considering it as just a 'class'?
| |
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 that I implement it as a struct rather than a
class which I did and all worked OK.
The simplest declaration for the struct is:
|
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 void MessageBeep(BeepTypes type){
if(!MessageBeep((UInt32) type)){
Int32 err = Marshal.GetLastWin32Error();
|
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 "fooDecorator"
|
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 of objects can be different for different users.
This method uses some non-static variables which are local to this
method.
Following is my static class:
|
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 an instance of a class. that i know, i also know how to
program with classes etc.
i am just confused about the term object-oriented.
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
| |
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |