subramanian wrote:
Quote:
I am a beginner in C++.
>
Suppose I want to build a class: I have given below the starting code:
>
class Date {
int day, month, year;
>
static Date default_date;
>
};
>
Someone, kindly, completely tell me how to initialize(ie the definition
of) the static member default_date in the above class and the
constructors needed for this. Also how should I access the members of
default_date ie the syntax to access it. I need the full
implementation. I am asking this is for learning purpose.
#include <iostream>
#include <ostream>
class Date {
int day, month, year;
static Date default_date;
public:
Date() : day(default_date.day),
month(default_date.month),
year(default_date.year) { }
explicit Date(int d, int m, int y)
: day(d), month(m), year(y) { }
};
Date Date::default_date(1,1,2000); // static member
int main()
{
Date date;
Date today(31,12,2006);
}
___
But then wouldn't be simpler do the above in the default ctor directly
instead of using a static member?
class Date {
int day, month, year;
public:
Date() : day(1),
month(1),
year(2000) { }
explicit Date(int d, int m, int y)
: day(d), month(m), year(y) { }
};