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:ate(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,
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:ate(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,